Skip to content

Instantly share code, notes, and snippets.

@snuke
Created April 21, 2015 06:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snuke/81ee9ed9f21811a0e13a to your computer and use it in GitHub Desktop.
Save snuke/81ee9ed9f21811a0e13a to your computer and use it in GitHub Desktop.
#include <iostream>
#include <algorithm>
#include <numeric>
using namespace std;
const int MAX_N = 605;
const int MAX_M = 180305;
const int INF = 1000;
int X[MAX_N];
int dp0[MAX_N][MAX_M];
int dp1[MAX_N][MAX_M];
inline void update(int& a, int b) { a = min(a,b);}
int main(){
int N, K;
// input
cin >> N >> K;
for (int i = 0; i < N; ++i) cin >> X[i];
int m = accumulate(X, X+N, 0);
// initialize
for (int j = K; j <= N; ++j) for(int s = 0; s <= m; ++s) {
dp0[j][s] = dp1[j][s] = INF;
}
dp0[K][accumulate(X, X+K, 0)] = 0;
// DP
for (int j = K; j <= N; ++j) {
for (int s = 0; s <= m; ++s) {
if (dp1[j][s] != INF) {
update(dp1[j+1][s], dp1[j][s]);
int l = dp1[j][s], r = K;
if (j > 0) update(r, dp1[j-1][s]);
for (int i = l; i < r; ++i) {
update(dp0[j][s-X[i]], i+1);
}
}
}
for (int s = 0; s <= m; ++s) {
if (dp0[j][s] != INF) {
update(dp0[j+1][s], dp0[j][s]);
update(dp1[j+1][s+X[j]], dp0[j][s]);
}
}
}
// output
string ans;
for (int s = 0; s <= m; ++s) {
ans += (dp0[N][s] == INF) ? '0' : '1';
}
cout << ans << endl;
return 0;
}
@snuke
Copy link
Author

snuke commented Apr 21, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment