Submission #1255058
Source Code Expand
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)
typedef long long ll;
int N, W, w[101], v[101];
//-----------------------------------------------------------------------------------
vector<vector<vector<vector<ll>>>> dp[2];
int N0, N1, N2, N3;
void pre() {
rep(i, 0, N) {
if (w[i] == w[0]) N0++;
else if (w[i] == w[0] + 1) N1++;
else if (w[i] == w[0] + 2) N2++;
else if (w[i] == w[0] + 3) N3++;
}
N0++; N1++; N2++; N3++;
dp[0] = vector<vector<vector<vector<ll>>>>(N0, vector<vector<vector<ll>>>(N1, vector<vector<ll>>(N2, vector<ll>(N3, -1))));
dp[1] = vector<vector<vector<vector<ll>>>>(N0, vector<vector<vector<ll>>>(N1, vector<vector<ll>>(N2, vector<ll>(N3, -1))));
}
//-----------------------------------------------------------------------------------
int main() {
cin >> N >> W;
rep(i, 0, N) scanf("%d%d", &w[i], &v[i]);
pre();
rep(w0, 0, N0) rep(w1, 0, N1) rep(w2, 0, N2) rep(w3, 0, N3) dp[0][w0][w1][w2][w3] = -1;
dp[0][0][0][0][0] = 0;
rep(i, 0, N) {
int ii = (i + 1) % 2;
rep(w0, 0, N0) rep(w1, 0, N1) rep(w2, 0, N2) rep(w3, 0, N3) dp[ii][w0][w1][w2][w3] = -1;
rep(w0, 0, N0) rep(w1, 0, N1) rep(w2, 0, N2) rep(w3, 0, N3) if (0 <= dp[i % 2][w0][w1][w2][w3]) {
dp[(i + 1) % 2][w0][w1][w2][w3] = max(dp[(i + 1) % 2][w0][w1][w2][w3], dp[i % 2][w0][w1][w2][w3]);
int ii = (i + 1) % 2;
int ww0 = w0;
int ww1 = w1;
int ww2 = w2;
int ww3 = w3;
if (w[i] == w[0]) ww0++;
else if (w[i] == w[0] + 1) ww1++;
else if (w[i] == w[0] + 2) ww2++;
else if (w[i] == w[0] + 3) ww3++;
ll wei = 1LL * w[0] * ww0;
wei += 1LL * (w[0] + 1) * ww1;
wei += 1LL * (w[0] + 2) * ww2;
wei += 1LL * (w[0] + 3) * ww3;
if (W < wei) continue;
dp[ii][ww0][ww1][ww2][ww3] = max(dp[ii][ww0][ww1][ww2][ww3], dp[i % 2][w0][w1][w2][w3] + v[i]);
}
}
ll ans = 0;
rep(w0, 0, N0) rep(w1, 0, N1) rep(w2, 0, N2) rep(w3, 0, N3) ans = max(ans, dp[N % 2][w0][w1][w2][w3]);
cout << ans << endl;
}
Submission Info
Submission Time |
|
Task |
D - Simple Knapsack |
User |
hamayanhamayan |
Language |
C++14 (GCC 5.4.1) |
Score |
400 |
Code Size |
2279 Byte |
Status |
AC |
Exec Time |
113 ms |
Memory |
8960 KB |
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:27:45: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
rep(i, 0, N) scanf("%d%d", &w[i], &v[i]);
^
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 |
1 ms |
256 KB |
antigreedy1 |
AC |
1 ms |
256 KB |
antigreedy2 |
AC |
1 ms |
256 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 |
113 ms |
8960 KB |
quarter1 |
AC |
103 ms |
8960 KB |
quarter2 |
AC |
91 ms |
8960 KB |
rand0 |
AC |
1 ms |
256 KB |
rand1 |
AC |
5 ms |
896 KB |
rand2 |
AC |
1 ms |
256 KB |
smallw0 |
AC |
86 ms |
8448 KB |
smallw1 |
AC |
89 ms |
8960 KB |
smallw2 |
AC |
84 ms |
8192 KB |