D - Knapsack 1


Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 100100 points

Problem Statement

There are NN items, numbered 1,2,…,N1,2,…,N. For each ii (1≤i≤N1≤i≤N), Item ii has a weight of wiwi and a value of vivi.

Taro has decided to choose some of the NN items and carry them home in a knapsack. The capacity of the knapsack is WW, which means that the sum of the weights of items taken must be at most WW.

Find the maximum possible sum of the values of items that Taro takes home.

Constraints

  • All values in input are integers.
  • 1≤N≤1001≤N≤100
  • 1≤W≤1051≤W≤105
  • 1≤wi≤W1≤wi≤W
  • 1≤vi≤1091≤vi≤109

Input

Input is given from Standard Input in the following format:

NN WW
w1w1 v1v1
w2w2 v2v2
::
wNwN vNvN

Output

Print the maximum possible sum of the values of items that Taro takes home.


Sample Input 1 Copy

Copy
3 8
3 30
4 50
5 60

Sample Output 1 Copy

Copy
90

Items 11 and 33 should be taken. Then, the sum of the weights is 3+5=83+5=8, and the sum of the values is 30+60=9030+60=90.


Sample Input 2 Copy

Copy
5 5
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000

Sample Output 2 Copy

Copy
5000000000

The answer may not fit into a 32-bit integer type.


Sample Input 3 Copy

Copy
6 15
6 5
5 6
6 4
6 6
3 5
7 2

Sample Output 3 Copy

Copy
17

Items 2,42,4 and 55 should be taken. Then, the sum of the weights is 5+6+3=145+6+3=14, and the sum of the values is 6+6+5=176+6+5=17.

题目链接:https://atcoder.jp/contests/dp/tasks/dp_d

  题意:有N个团队,每一个团队有w[i]个人,并且有v[i]的贡献值。

现在你有一个W的容量的公司,你需要从这n个团队中挑选出几个(每一个团队只能挑选一次),使之团队人数的总和小于W,并且贡献值的总和尽可能的大。

思路:裸的普通的背包问题。

我的AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define gg(x) getInt(&x)
using namespace std;
typedef long long ll;
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n,m;
int w[maxn];
ll v[maxn];
ll dp[maxn];
int main()
{
gbtb;
cin>>n>>m;
repd(i,,n)
{
cin>>w[i]>>v[i];
}
repd(i,,n)
{
for(int j=m;j>=w[i];j--)
{
dp[j]=max(dp[j],1ll*dp[j-w[i]]+v[i]);
}
}
cout<<dp[m];
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}

最新文章

  1. sql order by 排序多个字段
  2. OpenStack学习
  3. 前端神器 Firebug 2.0 新特性一览
  4. 20160204.CCPP体系详解(0014天)
  5. HTTP 错误 404.2 解决方案
  6. 从bug中学习怎么写代码
  7. 利用merge存储引擎来实现分表
  8. Cocos2dx-Android 之Makefile通用高级写法
  9. Android KeyStore Stack Buffer Overflow (CVE-2014-3100)
  10. jPaginate 一个非常好用的分页插件
  11. C# 调用 C++ DLL方法
  12. iframe之间的postMessage传参
  13. docker3
  14. eclipse的操作
  15. webpack分离打包css和less
  16. [整理]Kadane算法
  17. 查看mysql数据库中的所有用户
  18. JavaScript初探二
  19. Windows Server 2012 R2 创建AD域
  20. python中super的使用方法

热门文章

  1. EasyUI tabs指定要显示的tab
  2. ROS 订阅图像节点(1)
  3. Vxlan学习笔记——原理(转)
  4. Objective-C KVO深入理解
  5. 【Codeforces Round 1114】Codeforces #538 (Div. 2)
  6. MySQL(九)插入、更新和删除
  7. IIS 8的第一次请求不变慢如何配置
  8. 【第196期】Drupal7 Features模块与 Drupal8 Configuration Management 模块对比
  9. IIC双向电平转换电路设计
  10. 3.5《想成为黑客,不知道这些命令行可不行》(Learn Enough Command Line to Be Dangerous)—第三章小结