Submission #3772847
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for(int i=(a);i>=(b);i--)
#define RREP(i, n) RFOR(i, n, 0)
#define MFOR(i, m) for(auto i=(m).begin();i!=(m).end();i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((int)(x).size())
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1 << 30;
template<typename T>
void printv(vector<T> const& s) {
REP(i, SZ(s)) {
cout << s[i] << " ";
}
cout << endl;
}
int main () {
cin.tie(0);
cout << setprecision(10);
int n, W; cin >> n >> W;
vll w(n), v(n);
REP(i, n) {
cin >> w[i] >> v[i];
}
vector<map<int, ll>> dp(n);
dp[0][w[0]] = v[0];
REP(i, n-1) {
MFOR(j, dp[i]) {
dp[i+1][j->first] = max(dp[i+1][j->first], j->second);
if(j->first + w[i+1] <= W) dp[i+1][j->first + w[i+1]] = max(dp[i+1][j->first + w[i+1]], j->second + v[i+1]);
}
dp[i+1][w[i+1]] = max(dp[i+1][w[i+1]], v[i+1]);
}
ll ma = 0;
MFOR(j, dp[n-1]) {
if(j->first > W) break;
ma = max(ma, j->second);
}
cout << ma << endl;
}
Submission Info
Submission Time |
|
Task |
D - Simple Knapsack |
User |
kanra824 |
Language |
C++14 (GCC 5.4.1) |
Score |
400 |
Code Size |
1477 Byte |
Status |
AC |
Exec Time |
23 ms |
Memory |
6400 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
400 / 400 |
Status |
|
|
Set Name |
Test Cases |
Sample |
example0, example1, example2, example3 |
All |
antigreedy0, antigreedy1, antigreedy2, example0, example1, example2, example3, quarter0, quarter1, quarter2, rand0, rand1, rand2, smallw0, smallw1, smallw2 |
Case Name |
Status |
Exec Time |
Memory |
antigreedy0 |
AC |
2 ms |
512 KB |
antigreedy1 |
AC |
2 ms |
512 KB |
antigreedy2 |
AC |
2 ms |
512 KB |
example0 |
AC |
1 ms |
256 KB |
example1 |
AC |
1 ms |
256 KB |
example2 |
AC |
1 ms |
256 KB |
example3 |
AC |
1 ms |
256 KB |
quarter0 |
AC |
23 ms |
6400 KB |
quarter1 |
AC |
18 ms |
5120 KB |
quarter2 |
AC |
6 ms |
1920 KB |
rand0 |
AC |
1 ms |
256 KB |
rand1 |
AC |
1 ms |
256 KB |
rand2 |
AC |
1 ms |
256 KB |
smallw0 |
AC |
1 ms |
256 KB |
smallw1 |
AC |
1 ms |
256 KB |
smallw2 |
AC |
4 ms |
1024 KB |