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

Submission #607513

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";

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

int N,C; cin >> N >> C;

if (false and C<M) { // 部分解答 2
for (int i = 0; i <= C; ++i) {
table[i] = - 1e9;
}
table[0] = 0;
for (int i = 0; i < N; ++i) {
int v, c;  cin >> v >> c;
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;
}

{ // 部分解答 2
for (int i = 0; i < M; ++i) {
table[i] = 1e9;
}
table[0] = 0;
Integer V=0;
for (int i = 0; i < N; ++i) {
int v, c;  cin >> v >> c;
V+=v;
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;
for (int k = 0; k <= V; ++k) {
if (table[k] <= C) ans = max(ans, k);
}
cout << ans << endl;
return 0;
}

return 0;
}
```

Submission Info

Submission Time 2016-01-09 22:04:09+0900 D - ナップサック問題 cympfh C++11 (GCC 4.9.2) 0 2769 Byte WA 350 ms 2468 KB

Test Cases

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