Contest Duration: ~ (local time) (120 minutes) Back to Home

Submission #608226

Source Code Expand

Copy
```#include <bits/stdc++.h>
using namespace std;

#define rep(i,n) for(int i=0;i<(n);++i)
#define loop for(;;)
#define trace(var) cerr<<">>> "<<#var<<" = "<<var<<endl;
#define all(v) begin(v),end(v)
#define pb push_back
#define inf (1e9)
#define eps (1e-9)
using Integer = long long;
using Real = long double;
const Real PI = acosl(-1);
using P = pair<int, int>;

template<class S, class T> inline
ostream& operator<<(ostream&os, pair<S,T> p) {
return os << '(' << p.first << ", " << p.second << ')';
}

template<class T, class U> inline
ostream& operator<<(ostream&os, tuple<T,U> t) {
return os << '('
<< get<0>(t) << ", "
<< get<1>(t) << ')';
}

template<class S, class T, class U> inline
ostream& operator<<(ostream&os, tuple<S,T,U> t) {
return os << '('
<< get<0>(t) << ", "
<< get<1>(t) << ", "
<< get<2>(t) << ')';
}

template<class T> inline
ostream& operator<<(ostream&os, set<T> v) {
os << "(set";
for (T item: v) os << ' ' << item;
os << ")";
return os;
}

template<class T> inline
ostream& operator<<(ostream&os, vector<T> v) {
if (v.size() == 0) { return os << "(empty)"; }
os << v[0];
for (int i=1, len=v.size(); i<len; ++i) os << ' ' << v[i];
return os;
}

template<class T> inline
istream& operator>>(istream&is, vector<T>&v) {
rep (i, v.size()) is >> v[i];
return is;
}

//           ^   >  v   <
int dx[] = { -1, 0, 1,  0 };
int dy[] = {  0, 1, 0, -1 };

using vi = vector<int>;
using vvi = vector<vi>;
using vd = vector<double>;
using vvd = vector<vd>;
using vb = vector<bool>;
const string A = "Takahashi";
const string B = "Aoki";

Integer brute(int N, int C, vector<int>&vs, vector<int>&cs,
int i=0,
Integer vsum=0,
Integer csum=0) {
if (i >= N) {
return vsum;
}
if (csum+cs[i]<=C) {
Integer a = brute(N,C,vs,cs,i+1, vsum+vs[i], csum+cs[i]);
Integer b = brute(N,C,vs,cs,i+1,vsum,csum);
return max(a,b);
} else {
return brute(N,C,vs,cs,i+1,vsum,csum);
}
}

constexpr int M = 200*1000+10;
Integer table[M];
int main() {

int N, C; cin >> N >> C;
vector<int> vs, cs;
Integer vsum=0, csum=0;
rep (i, N) {
int v, c; cin >> v >> c;
vsum += v;
csum += c;
vs.push_back(v);
cs.push_back(c);
}

if (csum <= C) {
cout << vsum << endl;
return 0;
}

if (csum < M) { // DP respect to cost
trace(2);
rep(i, M) table[i] = - 2e9;
table[0] = 0;
for (int i = 0; i < N; ++i) {
int c=cs[i];
int v=vs[i];
for (int k = C; k >= 0; --k) {
if (c+k > C) continue;
table[c+k] = max(table[c+k], table[k] + v);
}
}

Integer ans = table[0];
for (int k = 0; k <= C; ++k) {
ans = max(ans, table[k]);
}
cout << ans << endl;
return 0;
}

if (vsum < M) { // DP respect to value
trace(3);
rep(i,M) table[i] = 2e9;
table[0] = 0;
for (int i = 0; i < N; ++i) {
int c=cs[i];
int v=vs[i];
for (int k = M; k >= 0; --k) {
if (v+k >=M)continue;
table[v+k] = min(table[v+k], table[k] + c);
}
}

int ans = 0;
rep (k,M){
if (table[k] <= C) ans = max(ans, k);
}
cout << ans << endl;
return 0;
}

if (N<=30) {
cout << brute(N, C, vs, cs);
cout << endl;
}

return 0;
}
```

#### Submission Info

Submission Time 2016-01-09 22:49:50+0900 D - ナップサック問題 cympfh C++11 (GCC 4.9.2) 100 3414 Byte AC 154 ms 2604 KB

#### Test Cases

Set Name Score / Max Score Test Cases
Case Name Status Exec Time Memory