裸的01背包。

###Code

POJ 3624
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//By Brickgao
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;
#define out(v) cerr << #v << ": " << (v) << endl
#define SZ(v) ((int)(v).size())
const int maxint = -1u>>1;
template <class T> bool get_max(T& a, const T &b) {return b > a? a = b, 1: 0;}
template <class T> bool get_min(T& a, const T &b) {return b < a? a = b, 1: 0;}

int n, m;
int w[3410], d[3410], s[12890];

int main() {
while(scanf("%d%d", &n, &m) != EOF) {
for(int i = 0; i < n; i ++) {
scanf("%d%d", &w[i], &d[i]);
}
memset(s, 0, sizeof(s));
for(int i = 0; i <= n; i ++) {
for(int j = m; j >= 0; j --) {
//s[j] = (i == n - 1 ? 0 : s[i + 1][j]);
if(j >= w[i])
s[j] = max(s[j - w[i]] + d[i], s[j]);
}
}
printf("%d\n", s[m]);
}
return 0;
}