buggy_code
stringlengths
11
625k
fixed_code
stringlengths
17
625k
bug_type
stringlengths
2
4.45k
language
int64
0
8
token_count
int64
5
200k
#include <stdio.h> int main(void) { double w; while (scanf("%lf", &w) != EOF) { if (w <= 48.00) { printf("light fly\n"); } else if (w > 48.00 && w <= 51.00) { puts("fly"); } else if (w > 51.00 && w <= 54.00) { puts("bantam"); } else if (w > 54.00 && w <= 57.00) { puts("feather"); } else if (w > 57.00 && w <= 60.00) { puts("light"); } else if (w > 60.00 && w <= 64.00) { puts("light welter"); } else if (w > 64.00 && w <= 69.00) { puts("welter"); } else if (w > 69.00 && w <= 75.00) { puts("light middle"); } else if (w > 75.00 && 81.00) { puts("middle"); } else if (w > 81.00 && 91.00) { puts("light heavy"); } else { puts("heavy"); } } return 0; }
#include <stdio.h> int main(void) { double w; while (scanf("%lf", &w) != EOF) { if (w <= 48.00) { printf("light fly\n"); } else if (w > 48.00 && w <= 51.00) { puts("fly"); } else if (w > 51.00 && w <= 54.00) { puts("bantam"); } else if (w > 54.00 && w <= 57.00) { puts("feather"); } else if (w > 57.00 && w <= 60.00) { puts("light"); } else if (w > 60.00 && w <= 64.00) { puts("light welter"); } else if (w > 64.00 && w <= 69.00) { puts("welter"); } else if (w > 69.00 && w <= 75.00) { puts("light middle"); } else if (w > 75.00 && w <= 81.00) { puts("middle"); } else if (w > 81.00 && w <= 91.00) { puts("light heavy"); } else { puts("heavy"); } } return 0; }
[["+", 0, 57, 15, 339, 51, 16, 12, 16, 31, 22], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 17, 19]]
1
233
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <set> #include <string> #include <utility> #include <vector> #define loop(i, a, b) for (int i = a; i < b; i++) #define rep(i, a) loop(i, 0, a) #define pb push_back #define mp make_pair #define it ::iterator #define all(in) in.begin(), in.end() const double PI = acos(-1); const double ESP = 1e-10; using namespace std; int main() { int m; int co = 0; while (cin >> m, m) { co++; int n; cin >> n; int in[1001][1001] = {0}; rep(q, n) { int i = q + 1; int a, b; char d; cin >> a >> d >> b; rep(j, m + 1) { if (j >= b) in[i][j] = max(in[i][j], in[i - 1][j - b] + a); in[i][j] = max(in[i][j], in[i - 1][j]); } } int max1 = 0; rep(i, m + 1) max1 = max(max1, in[n][i]); rep(i, m + 1) if (in[n][i] == max1) { cout << "Case " << co << " " << endl; cout << max1 << endl << i << endl; break; } } } /*int main(){ int m; int co=0; while(cin>>m,m){ co++; int n; char d; cin>>n; vector<int>w(n),v(n); vector<vector<int> >dp(n+1); rep(i,m+1)dp[0].pb(0); rep(q,n){ int i=q+1; int a,b;char d; cin>>a>>d>>b; dp[i].pb(0); } */
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <set> #include <string> #include <utility> #include <vector> #define loop(i, a, b) for (int i = a; i < b; i++) #define rep(i, a) loop(i, 0, a) #define pb push_back #define mp make_pair #define it ::iterator #define all(in) in.begin(), in.end() const double PI = acos(-1); const double ESP = 1e-10; using namespace std; int main() { int m; int co = 0; while (cin >> m, m) { co++; int n; cin >> n; int in[1001][1001] = {0}; rep(q, n) { int i = q + 1; int a, b; char d; cin >> a >> d >> b; rep(j, m + 1) { if (j >= b) in[i][j] = max(in[i][j], in[i - 1][j - b] + a); in[i][j] = max(in[i][j], in[i - 1][j]); } } int max1 = 0; rep(i, m + 1) max1 = max(max1, in[n][i]); rep(i, m + 1) if (in[n][i] == max1) { cout << "Case " << co << ":" << endl; cout << max1 << endl << i << endl; break; } } } /*int main(){ int m; int co=0; while(cin>>m,m){ co++; int n; char d; cin>>n; vector<int>w(n),v(n); vector<vector<int> >dp(n+1); rep(i,m+1)dp[0].pb(0); rep(q,n){ int i=q+1; int a,b;char d; cin>>a>>d>>b; dp[i].pb(0); } */
[["-", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
1
305
#include <algorithm> #include <iostream> #include <map> #include <string.h> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; int dp[1001][1001]; vector<string> split(const string &str, char delim) { vector<string> res; size_t current = 0, found; while ((found = str.find_first_of(delim, current)) != string::npos) { res.push_back(string(str, current, found - current)); current = found + 1; } res.push_back(string(str, current, str.size() - current)); return res; } int main() { int count = 0; int W, n, ans; int w[1001], v[1001]; while (cin >> W && W) { count++; memset(dp, 0, sizeof(dp)); cin >> n; rep(i, n) { string s; cin >> s; vector<string> _s = split(s, ','); v[i] = atoi(_s[0].c_str()); w[i] = atoi(_s[1].c_str()); } for (int i = 0; i < n; i++) { for (int j = 0; j <= W; j++) { if (j < w[i]) { dp[i + 1][j] = dp[i][j]; } else { dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]); } } } for (int j = 0; j <= W; j++) { if (dp[n][j] == dp[n][W]) { ans = j; break; } } cout << "Case " << count << endl; cout << dp[n][W] << endl << ans << endl; } return 0; }
#include <algorithm> #include <iostream> #include <map> #include <string.h> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; int dp[1001][1001]; vector<string> split(const string &str, char delim) { vector<string> res; size_t current = 0, found; while ((found = str.find_first_of(delim, current)) != string::npos) { res.push_back(string(str, current, found - current)); current = found + 1; } res.push_back(string(str, current, str.size() - current)); return res; } int main() { int count = 0; int W, n, ans; int w[1001], v[1001]; while (cin >> W && W) { count++; memset(dp, 0, sizeof(dp)); cin >> n; rep(i, n) { string s; cin >> s; vector<string> _s = split(s, ','); v[i] = atoi(_s[0].c_str()); w[i] = atoi(_s[1].c_str()); } for (int i = 0; i < n; i++) { for (int j = 0; j <= W; j++) { if (j < w[i]) { dp[i + 1][j] = dp[i][j]; } else { dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]); } } } for (int j = 0; j <= W; j++) { if (dp[n][j] == dp[n][W]) { ans = j; break; } } cout << "Case " << count << ":" << endl; cout << dp[n][W] << endl << ans << endl; } return 0; }
[["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 62], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 0, 52, 8, 9, 0, 1, 0, 16, 17, 151]]
1
424
#include <cstdio> #include <iostream> using namespace std; int main() { int dataset = 1, w; while (cin >> w, w) { int dp[1001], n, treas_val[1000], treas_w[1000], ans_val = 0, ans_w; for (int i = 0; i <= w; i++) { dp[i] = -1; } cin >> n; for (int i = 0; i < n; i++) { scanf("%d,%d", &treas_val[i], &treas_w[i]); } dp[0] = 0; for (int i = 0; i < n; i++) { for (int j = w; j >= 0; j--) { if (dp[j] == -1) continue; if (j + treas_w[i] <= w && dp[j + treas_w[i]] < dp[j] + treas_val[i]) dp[j + treas_w[i]] = dp[j] + treas_val[i]; } } for (int i = 0; i <= w; i++) { if (ans_val < dp[i]) { ans_val = dp[i]; ans_w = i; } } printf("Case %d:\n%d\n%d\n", dataset, ans_val, ans_w); } return 0; }
#include <cstdio> #include <iostream> using namespace std; int main() { int dataset = 1, w; while (cin >> w, w) { int dp[1001], n, treas_val[1000], treas_w[1000], ans_val = 0, ans_w; for (int i = 0; i <= w; i++) { dp[i] = -1; } cin >> n; for (int i = 0; i < n; i++) { scanf("%d,%d", &treas_val[i], &treas_w[i]); } dp[0] = 0; for (int i = 0; i < n; i++) { for (int j = w; j >= 0; j--) { if (dp[j] == -1) continue; if (j + treas_w[i] <= w && dp[j + treas_w[i]] < dp[j] + treas_val[i]) dp[j + treas_w[i]] = dp[j] + treas_val[i]; } } for (int i = 0; i <= w; i++) { if (ans_val < dp[i]) { ans_val = dp[i]; ans_w = i; } } printf("Case %d:\n%d\n%d\n", dataset, ans_val, ans_w); dataset++; } return 0; }
[["+", 0, 52, 8, 9, 0, 1, 0, 27, 28, 22], ["+", 0, 52, 8, 9, 0, 1, 0, 27, 17, 29], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
1
277
#include <cstdio> #include <iostream> using namespace std; const int INF = -1; int main() { int W, cnt = 0; while (cin >> W, W) { cnt++; int dp[1001], n; cin >> n; for (int i = 0; i < 1001; i++) dp[i] = INF; dp[0] = 0; for (int i = 0; i < n; i++) { int v, w; scanf("%d,%d", &v, &w); for (int j = W; j >= 0; j--) { if (dp[j] == INF) continue; if (j + w > W) continue; dp[j + w] = max(dp[j + w], dp[j] + v); } } pair<int, int> P; P.first = 0; for (int i = 0; i < W; i++) { if (dp[i] > P.first) { P.first = dp[i]; P.second = i; } } cout << "Case " << cnt << ":" << endl; cout << P.first << endl << P.second << endl; } return 0; }
#include <cstdio> #include <iostream> using namespace std; const int INF = -1; int main() { int W, cnt = 0; while (cin >> W, W) { cnt++; int dp[1001], n; cin >> n; for (int i = 0; i < 1001; i++) dp[i] = INF; dp[0] = 0; for (int i = 0; i < n; i++) { int v, w; scanf("%d,%d", &v, &w); for (int j = W; j >= 0; j--) { if (dp[j] == INF) continue; if (j + w > W) continue; dp[j + w] = max(dp[j + w], dp[j] + v); } } pair<int, int> P; P.first = 0; for (int i = 0; i <= W; i++) { if (dp[i] > P.first) { P.first = dp[i]; P.second = i; } } cout << "Case " << cnt << ":" << endl; cout << P.first << endl << P.second << endl; } return 0; }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 19]]
1
264
#include <stdio.h> int main() { int price[1000], weight[1000]; int weight_MAX; int treasure; int i, j; int ans_p, ans_w, time; int dp[10000]; time = 1; scanf("%d", &weight_MAX); while (weight_MAX != 0) { scanf("%d", &treasure); for (i = 0; i < treasure; i++) { scanf("%d,%d", &price[i], &weight[i]); } for (i = 0; i < weight_MAX; i++) { dp[i] = 0; } for (i = 0; i < treasure; i++) { for (j = weight_MAX; j >= weight[i]; j--) { if (dp[j - weight[i]] + price[i] > dp[j]) { dp[j] = dp[j - weight[i]] + price[i]; } } } ans_p = 0; ans_w = 0; for (i = 0; i < weight_MAX; i++) { if (ans_p < dp[i]) { ans_p = dp[i]; ans_w = i; } } printf("Case %d:\n%d\n%d\n", time, ans_p, ans_w); time++; scanf("%d", &weight_MAX); } return 0; }
#include <stdio.h> int main() { int price[1000], weight[1000]; int weight_MAX; int treasure; int i, j; int ans_p, ans_w, time; int dp[10000]; time = 1; scanf("%d", &weight_MAX); while (weight_MAX != 0) { scanf("%d", &treasure); for (i = 0; i < treasure; i++) { scanf("%d,%d", &price[i], &weight[i]); } for (i = 0; i <= weight_MAX + 1; i++) { dp[i] = 0; } for (i = 0; i < treasure; i++) { for (j = weight_MAX; j >= weight[i]; j--) { if (dp[j - weight[i]] + price[i] > dp[j]) { dp[j] = dp[j - weight[i]] + price[i]; } } } ans_p = 0; ans_w = 0; for (i = 0; i <= weight_MAX; i++) { if (ans_p < dp[i]) { ans_p = dp[i]; ans_w = i; } } printf("Case %d:\n%d\n%d\n", time, ans_p, ans_w); time++; scanf("%d", &weight_MAX); } return 0; }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 19], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 17, 72], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13]]
1
287
#include "bits/stdc++.h" using namespace std; typedef vector<int> vi; typedef pair<int, int> pii; typedef long long ll; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define all(a) (a).begin(), (a).end() #define pb push_back int dp[2000][2000]; int main() { int W; int n; int c = 0; while (cin >> W) { c++; if (W == 0) break; char ch; int w[2000], v[2000]; rep(i, 2000) rep(j, 2000) dp[i][j] = -999999999; cin >> n; rep(i, n) { cin >> v[i] >> ch >> w[i]; } int maxval = -1; int maxwait = -1; dp[0][0] = 0; for (int i = 1; i <= n; i++) { rep(j, W) { dp[i][j] = max(dp[i][j], dp[i - 1][j]); if (j - w[i - 1] >= 0) dp[i][j] = max(dp[i][j], dp[i - 1][j - w[i - 1]] + v[i - 1]); if (maxval < dp[i][j]) { maxval = dp[i][j]; maxwait = j; } if (maxval == dp[i][j] && maxwait > j) { maxval = dp[i][j]; maxwait = j; } } } cout << "Case " << c << ":" << endl; cout << maxval << endl; cout << maxwait << endl; } }
#include "bits/stdc++.h" using namespace std; typedef vector<int> vi; typedef pair<int, int> pii; typedef long long ll; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define all(a) (a).begin(), (a).end() #define pb push_back int dp[2000][2000]; int main() { int W; int n; int c = 0; while (cin >> W) { c++; if (W == 0) break; char ch; int w[2000], v[2000]; rep(i, 2000) rep(j, 2000) dp[i][j] = -999999999; cin >> n; rep(i, n) { cin >> v[i] >> ch >> w[i]; } int maxval = -1; int maxwait = -1; dp[0][0] = 0; for (int i = 1; i <= n; i++) { rep(j, W + 1) { dp[i][j] = max(dp[i][j], dp[i - 1][j]); if (j - w[i - 1] >= 0) dp[i][j] = max(dp[i][j], dp[i - 1][j - w[i - 1]] + v[i - 1]); if (maxval < dp[i][j]) { maxval = dp[i][j]; maxwait = j; } if (maxval == dp[i][j] && maxwait > j) { maxval = dp[i][j]; maxwait = j; } // cout<<i<<" "<<j<<" "<<dp[i][j]<<endl; } } cout << "Case " << c << ":" << endl; cout << maxval << endl; cout << maxwait << endl; } }
[["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 72], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13], ["+", 0, 1, 0, 2, 3, 4, 0, 25, 0, 35]]
1
369
#include <algorithm> #include <cstdio> #include <iostream> #include <vector> using namespace std; int W, N; int dp[1001][1001]; // 宝の数×重さ struct Obj { int price; int weight; } obj[1001]; int main(void) { int cnt = 1; while (cin >> W, W) { // input cin >> N; char buf; for (int i = 0; i < N; i++) { cin >> obj[i].price >> buf >> obj[i].weight; } // init for (int i = 0; i < N; i++) { for (int j = 0; j <= W; j++) { dp[i][j] = 0; } } dp[0][obj[0].weight] = obj[0].price; // calc int wei; for (int i = 0; i < N - 1; i++) { for (int j = 0; j <= W; j++) { if (j != 0 && dp[i][j] == 0) { continue; } // 下 dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]); // 右下 if (wei = j + obj[i + 1].weight, wei <= W) { dp[i + 1][wei] = max(dp[i + 1][wei], dp[i][j] + obj[i + 1].price); } } } struct Obj res = {0, 0}; for (int i = 0; i <= W; i++) { for (int j = 0; j < N; j++) { if (res.price < dp[j][i]) { res.price = dp[j][i]; res.weight = i; } } } // output printf("Case %d:\n%d\n%d\n", cnt, res.price, res.weight); } return 0; }
#include <algorithm> #include <cstdio> #include <iostream> #include <vector> using namespace std; int W, N; int dp[1001][1001]; // 宝の数×重さ struct Obj { int price; int weight; } obj[1001]; int main(void) { int cnt = 1; while (cin >> W, W) { // input cin >> N; char buf; for (int i = 0; i < N; i++) { cin >> obj[i].price >> buf >> obj[i].weight; } // init for (int i = 0; i < N; i++) { for (int j = 0; j <= W; j++) { dp[i][j] = 0; } } dp[0][obj[0].weight] = obj[0].price; // calc int wei; for (int i = 0; i < N - 1; i++) { for (int j = 0; j <= W; j++) { if (j != 0 && dp[i][j] == 0) { continue; } // 下 dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]); // 右下 if (wei = j + obj[i + 1].weight, wei <= W) { dp[i + 1][wei] = max(dp[i + 1][wei], dp[i][j] + obj[i + 1].price); } } } struct Obj res = {0, 0}; for (int i = 0; i <= W; i++) { for (int j = 0; j < N; j++) { if (res.price < dp[j][i]) { res.price = dp[j][i]; res.weight = i; } } } // output printf("Case %d:\n%d\n%d\n", cnt++, res.price, res.weight); } return 0; }
[["+", 0, 1, 0, 2, 3, 4, 0, 27, 17, 29]]
1
423
#include <bits/stdc++.h> using namespace std; #define iota(i, n, b, s) \ for (int i = int(b); i != int((b) + (s) * (n)); i += (s)) #define range(i, n, m) \ iota(i, (((n) > (m)) ? ((n) - (m)) : ((m) - (n))), (n), ((n) > (m) ? -1 : 1)) #define rep(i, n) iota(i, (n), 0, 1) #define loop for (;;) #define INF (1e9) #define EPS (1e-9) #define cons(a, b) (make_pair(a, b)) #define car(a) (a.first) #define cdr(a) (a.second) #define cadr(a) (car(cdr(a))) #define cddr(a) (cdr(cdr(a))) #define all(a) a.begin(), a.end() #define trace(var) cerr << ">>> " << #var << " = " << var << endl; typedef long long Integer; typedef double Real; typedef vector<int> vi; typedef vector<string> vs; typedef map<string, int> Dictionary; const Real PI = acos(-1); template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) { os << '(' << car(p) << ", " << cdr(p) << ')'; return os; } template <class T> ostream &operator<<(ostream &os, vector<T> v) { os << v[0]; for (int i = 1, len = v.size(); i < len; ++i) os << ' ' << v[i]; return os; } int dx[] = {-1, 0, 1, 0}; int dy[] = {0, -1, 0, 1}; int main() { int case_index = 1; loop { int C, n; cin >> C >> n; if (!C) break; cout << "Case " << case_index << endl; ++case_index; vi cs(n); vi xs(n); char comma; rep(i, n) cin >> xs[i] >> comma >> cs[i]; int dp[C + 1]; rep(c, C + 1) dp[c] = 0; rep(i, n) { for (int c = C - cs[i]; c >= 0; --c) { int c2 = c + cs[i]; if (dp[c2] < dp[c] + xs[i]) { dp[c2] = dp[c] + xs[i]; } } } int mx = 0, mc = -1; rep(c, C + 1) { if (mx < dp[c]) { mx = dp[c]; mc = c; } } cout << mx << endl; cout << mc << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define iota(i, n, b, s) \ for (int i = int(b); i != int((b) + (s) * (n)); i += (s)) #define range(i, n, m) \ iota(i, (((n) > (m)) ? ((n) - (m)) : ((m) - (n))), (n), ((n) > (m) ? -1 : 1)) #define rep(i, n) iota(i, (n), 0, 1) #define loop for (;;) #define INF (1e9) #define EPS (1e-9) #define cons(a, b) (make_pair(a, b)) #define car(a) (a.first) #define cdr(a) (a.second) #define cadr(a) (car(cdr(a))) #define cddr(a) (cdr(cdr(a))) #define all(a) a.begin(), a.end() #define trace(var) cerr << ">>> " << #var << " = " << var << endl; typedef long long Integer; typedef double Real; typedef vector<int> vi; typedef vector<string> vs; typedef map<string, int> Dictionary; const Real PI = acos(-1); template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) { os << '(' << car(p) << ", " << cdr(p) << ')'; return os; } template <class T> ostream &operator<<(ostream &os, vector<T> v) { os << v[0]; for (int i = 1, len = v.size(); i < len; ++i) os << ' ' << v[i]; return os; } int dx[] = {-1, 0, 1, 0}; int dy[] = {0, -1, 0, 1}; int main() { int case_index = 1; loop { int C, n; cin >> C >> n; if (!C) break; cout << "Case " << case_index << ':' << endl; ++case_index; vi cs(n); vi xs(n); char comma; rep(i, n) cin >> xs[i] >> comma >> cs[i]; int dp[C + 1]; rep(c, C + 1) dp[c] = 0; rep(i, n) { for (int c = C - cs[i]; c >= 0; --c) { int c2 = c + cs[i]; if (dp[c2] < dp[c] + xs[i]) { dp[c2] = dp[c] + xs[i]; } } } int mx = 0, mc = -1; rep(c, C + 1) { if (mx < dp[c]) { mx = dp[c]; mc = c; } } cout << mx << endl; cout << mc << endl; } return 0; }
[["+", 0, 1, 0, 16, 31, 16, 12, 103, 0, 104], ["+", 0, 1, 0, 16, 31, 16, 12, 103, 0, 125], ["+", 0, 14, 8, 9, 0, 1, 0, 16, 17, 151]]
1
516
#include <bits/stdc++.h> using namespace std; int table[1001]; int main() { int C; for (int i = 1; cin >> C, C; ++i) { for (int k = 0; k < 1001; ++k) table[k] = -1e9; table[0] = 0; int n; cin >> n; for (; n--;) { int v, w; char comma; cin >> v >> comma >> w; for (int k = 1000 - w; k >= 0; --k) { // table の上から更新 table[k + w] = max<int>(table[k + w], table[k] + v); } } int W, V = table[0]; for (int k = 0; k <= C; ++k) { // 最小のインデクスを答える必要 if (V < table[k]) { V = table[k]; W = k; } } printf("Case %d:\n%d\n%d\n", ++i, V, W); } }
#include <bits/stdc++.h> using namespace std; int table[1001]; int main() { int C; for (int i = 1; cin >> C, C; ++i) { for (int k = 0; k < 1001; ++k) table[k] = -1e9; table[0] = 0; int n; cin >> n; for (; n--;) { int v, w; char comma; cin >> v >> comma >> w; for (int k = 1000 - w; k >= 0; --k) { // table の上から更新 table[k + w] = max<int>(table[k + w], table[k] + v); } } int W, V = table[0]; for (int k = 0; k <= C; ++k) { // 最小のインデクスを答える必要 if (V < table[k]) { V = table[k]; W = k; } } printf("Case %d:\n%d\n%d\n", i, V, W); } }
[["-", 0, 1, 0, 2, 3, 4, 0, 27, 17, 29]]
1
213
#include <algorithm> #include <cstring> #include <iostream> using namespace std; int main() { int dp[1001][1001]; int w[1001], v[1001], n, W; char s; int k = 1; while (1) { cin >> W >> n; if (W == 0) break; for (int i = 0; i < n; i++) { cin >> v[i] >> s >> w[i]; } memset(dp, 0, sizeof(dp)); for (int i = 0; i < n; i++) { for (int j = 0; j <= W; j++) { if (w[i] > j) dp[i + 1][j] = dp[i][j]; else dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]); } } cout << "Case " << k << endl; cout << dp[n][W] << endl; for (int i = 0; i <= W; i++) { if (dp[n][W] == dp[n][i]) { cout << i << endl; ; break; } } k++; } }
#include <algorithm> #include <cstring> #include <iostream> using namespace std; int main() { int dp[1001][1001]; int w[1001], v[1001], n, W; char s; int k = 1; while (1) { cin >> W >> n; if (W == 0) break; for (int i = 0; i < n; i++) { cin >> v[i] >> s >> w[i]; } memset(dp, 0, sizeof(dp)); for (int i = 0; i < n; i++) { for (int j = 0; j <= W; j++) { if (w[i] > j) dp[i + 1][j] = dp[i][j]; else dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]); } } cout << "Case " << k << ":" << endl; cout << dp[n][W] << endl; for (int i = 0; i <= W; i++) { if (dp[n][W] == dp[n][i]) { cout << i << endl; ; break; } } k++; } }
[["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 62], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 0, 52, 8, 9, 0, 1, 0, 16, 17, 151]]
1
279
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; int main() { int cas = 0; int anskati = 0; int ansomo; int w, n; int kati[1111], omo[1111]; int memo[1111][1111]; while (1) { cas++; cin >> w; if (w == 0) break; cin >> n; for (int i = 0; i <= w; i++) { memo[0][i] = 0; } for (int i = 0; i < n; i++) { char a; cin >> kati[i] >> a >> omo[i]; } cout << "Case " << cas << ':' << endl; for (int i = 1; i <= n; i++) { for (int j = 1; j <= w; j++) { if (j < omo[i - 1]) { memo[i][j] = memo[i - 1][j]; } else { memo[i][j] = max(memo[i - 1][j], memo[i - 1][j - omo[i - 1]] + kati[i - 1]); } } } for (int i = 0; i <= w; i++) { if (memo[n][i] > anskati) { anskati = memo[n][i]; ansomo = i; } } cout << anskati << endl; cout << ansomo << endl; } }
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; int main() { int cas = 0; int anskati = 0; int ansomo; int w, n; int kati[1111], omo[1111]; int memo[1111][1111]; while (1) { cas++; anskati = 0; cin >> w; if (w == 0) break; cin >> n; for (int i = 0; i <= w; i++) { memo[0][i] = 0; } for (int i = 0; i < n; i++) { char a; cin >> kati[i] >> a >> omo[i]; } cout << "Case " << cas << ':' << endl; for (int i = 1; i <= n; i++) { for (int j = 1; j <= w; j++) { if (j < omo[i - 1]) { memo[i][j] = memo[i - 1][j]; } else { memo[i][j] = max(memo[i - 1][j], memo[i - 1][j - omo[i - 1]] + kati[i - 1]); } } } for (int i = 0; i <= w; i++) { if (memo[n][i] > anskati) { anskati = memo[n][i]; ansomo = i; } } cout << anskati << endl; cout << ansomo << endl; } }
[["+", 0, 52, 8, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 17, 32], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
1
319
#include <cstdio> #include <iostream> #include <vector> using namespace std; int dp[1001][1001] = {0}; int main() { int W, N; int t = 1; while (cin >> W, W) { cin >> N; vector<int> v(N), w(N); for (int i = 0; i < N; ++i) { scanf("%d,%d", &v[i], &w[i]); } fill(&dp[0][0], &dp[0][0] + 1001 * 1001, 0); for (int i = 0; i < N; ++i) { for (int j = 0; j < W; ++j) { if (w[i] + j < W) { dp[i + 1][w[i] + j] = max(dp[i + 1][w[i] + j], dp[i][j] + v[i]); } dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]); } } int aw = 0, av = 0; for (int i = 0; i < W; ++i) { if (dp[N][i] > av) { av = dp[N][i], aw = i; } } cout << "Case " << t << ":" << endl; cout << av << endl; cout << aw << endl; ++t; } }
#include <cstdio> #include <iostream> #include <vector> using namespace std; int dp[1001][1001] = {0}; int main() { int W, N; int t = 1; while (cin >> W, W) { cin >> N; vector<int> v(N), w(N); for (int i = 0; i < N; ++i) { scanf("%d,%d", &v[i], &w[i]); } fill(&dp[0][0], &dp[0][0] + 1001 * 1001, 0); for (int i = 0; i < N; ++i) { for (int j = 0; j <= W; ++j) { if (w[i] + j <= W) { dp[i + 1][w[i] + j] = max(dp[i + 1][w[i] + j], dp[i][j] + v[i]); } dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]); } } int aw = 0, av = 0; for (int i = 0; i <= W; ++i) { if (dp[N][i] > av) { av = dp[N][i], aw = i; } } cout << "Case " << t << ":" << endl; cout << av << endl; cout << aw << endl; ++t; } }
[["-", 0, 7, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 17, 19], ["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 18], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 19], ["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 19]]
1
333
#include <cstdio> #include <cstring> #include <iostream> using namespace std; int main() { int dp[1001][1001]; int n, W; int v[1000], w[1000]; int num = 0; char c; int cnt = 0; while (1) { cin >> W; if (W == 0) break; cin >> n; cnt++; for (int i = 0; i <= n; i++) { for (int j = 0; j <= W; j++) { dp[i][j] = 0; } } for (int i = 0; i < n; i++) { scanf("%d,%d", &v[i], &w[i]); } for (int i = 1; i <= n; i++) { for (int j = 1; j <= W; j++) { if (j < w[i]) { dp[i][j] = dp[i - 1][j]; } else { dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]); } } } int ansv = 0, answ = 0; for (int i = 1; i <= W; i++) { if (ansv < dp[n][i]) { ansv = dp[n][i]; answ = i; } } printf("Case %d:\n%d\n%d\n", cnt, ansv, answ); } }
#include <cstdio> #include <cstring> #include <iostream> using namespace std; int main() { int dp[1001][1001]; int n, W; int v[1000], w[1000]; int num = 0; char c; int cnt = 0; while (1) { cin >> W; if (W == 0) break; cin >> n; cnt++; for (int i = 0; i <= n; i++) { for (int j = 0; j <= W; j++) { dp[i][j] = 0; } } for (int i = 1; i <= n; i++) { scanf("%d,%d", &v[i], &w[i]); } for (int i = 1; i <= n; i++) { for (int j = 1; j <= W; j++) { if (j < w[i]) { dp[i][j] = dp[i - 1][j]; } else { dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]); } } } int ansv = 0, answ = 0; for (int i = 1; i <= W; i++) { if (ansv < dp[n][i]) { ansv = dp[n][i]; answ = i; } } printf("Case %d:\n%d\n%d\n", cnt, ansv, answ); } }
[["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 19]]
1
332
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; int main() { int cnt, N, W, value, weight, v[1001][1001], w[1001][1001]; cnt = 0; while (1) { cin >> W; if (W == 0) break; cnt++; cin >> N; for (int i = 0; i < N; i++) { for (int j = 0; j < W; j++) { v[i][j] = 0; w[i][j] = 0; } } for (int i = 1; i <= N; i++) { scanf("%d, %d", &value, &weight); for (int j = 0; j <= W; j++) { for (int k = 0; k <= j; k++) { if (w[i - 1][k] + weight <= j && v[i - 1][k] + value > v[i][j]) { v[i][j] = v[i - 1][k] + value; w[i][j] = w[i - 1][k] + weight; } else if (v[i - 1][k] > v[i][j]) { v[i][j] = v[i - 1][k]; w[i][j] = w[i - 1][k]; } else if (v[i - 1][k] == v[i][j] && w[i - 1][k] < w[i][j]) { w[i][j] = w[i - 1][k]; } } } } cout << "Case " << cnt << ":" << endl; cout << v[N][W] << endl; cout << w[N][W] << endl; } return 0; }
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; int main() { int cnt, N, W, value, weight, v[1001][1001], w[1001][1001]; cnt = 0; while (1) { cin >> W; if (W == 0) break; cnt++; cin >> N; for (int i = 0; i <= N; i++) { for (int j = 0; j <= W; j++) { v[i][j] = 0; w[i][j] = 0; } } for (int i = 1; i <= N; i++) { scanf("%d, %d", &value, &weight); for (int j = 0; j <= W; j++) { for (int k = 0; k <= j; k++) { if (w[i - 1][k] + weight <= j && v[i - 1][k] + value > v[i][j]) { v[i][j] = v[i - 1][k] + value; w[i][j] = w[i - 1][k] + weight; } else if (v[i - 1][k] > v[i][j]) { v[i][j] = v[i - 1][k]; w[i][j] = w[i - 1][k]; } else if (v[i - 1][k] == v[i][j] && w[i - 1][k] < w[i][j]) { w[i][j] = w[i - 1][k]; } } } } cout << "Case " << cnt << ":" << endl; cout << v[N][W] << endl; cout << w[N][W] << endl; } return 0; }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 19], ["-", 0, 7, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 17, 19]]
1
422
#include <algorithm> #include <cstdio> #include <iostream> #include <map> using namespace std; typedef pair<int, int> P; int main() { int W, N, cou = 0; int tr[1001], we[1001]; while (cin >> W, W) { cou++; cin >> N; for (int i = 0; i < N; i++) { scanf("%d,%d", &tr[i], &we[i]); } int dp[1010]; for (int i = 0; i <= W; i++) { dp[i] = 0; } for (int i = 0; i < N; i++) { for (int j = W; j >= we[i]; j--) { dp[j] = max(dp[i], dp[j - we[i]] + tr[i]); } } int ret = 0; for (int i = 0; i <= W; i++) { if (dp[i] > dp[ret]) ret = i; } cout << "Case " << cou << ":" << endl; cout << dp[ret] << endl; cout << ret << endl; } }
#include <algorithm> #include <cstdio> #include <iostream> #include <map> using namespace std; typedef pair<int, int> P; int main() { int W, N, cou = 0; int tr[1001], we[1001]; while (cin >> W, W) { cou++; cin >> N; for (int i = 0; i < N; i++) { scanf("%d,%d", &tr[i], &we[i]); } int dp[1010]; for (int i = 0; i <= W; i++) { dp[i] = 0; } for (int i = 0; i < N; i++) { for (int j = W; j >= we[i]; j--) { dp[j] = max(dp[j], dp[j - we[i]] + tr[i]); } } int ret = 0; for (int i = 0; i <= W; i++) { if (dp[i] > dp[ret]) ret = i; } cout << "Case " << cou << ":" << endl; cout << dp[ret] << endl; cout << ret << endl; } }
[["-", 12, 2, 3, 4, 0, 69, 341, 342, 0, 22], ["+", 12, 2, 3, 4, 0, 69, 341, 342, 0, 22]]
1
257
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; int dp[1000], w[1000], v[1000], a, n, t = 0; int main() { while (cin >> a, a) { t++; cin >> n; fill(dp, dp + a + 1, 0); for (int i = 0; i < n; i++) scanf("%d,%d", &v[i], &w[i]); for (int i = 0; i < n; i++) { for (int j = a; j >= 0; j--) { if (j + w[i] <= a) dp[j + w[i]] = max(dp[j + w[i]], dp[j] + v[i]); } } int mv = 0; for (int i = 0; i <= a; i++) { if (dp[i] > dp[mv]) mv = i; } cout << "Case" << t << ":" << endl; cout << dp[mv] << endl << mv << endl; } }
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; int dp[1000], w[1000], v[1000], a, n, t = 0; int main() { while (cin >> a, a) { t++; cin >> n; fill(dp, dp + a + 1, 0); for (int i = 0; i < n; i++) scanf("%d,%d", &v[i], &w[i]); for (int i = 0; i < n; i++) { for (int j = a; j >= 0; j--) { if (j + w[i] <= a) dp[j + w[i]] = max(dp[j + w[i]], dp[j] + v[i]); } } int mv = 0; for (int i = 0; i <= a; i++) { if (dp[i] > dp[mv]) mv = i; } cout << "Case " << t << ":" << endl; cout << dp[mv] << endl << mv << endl; } }
[["-", 31, 16, 31, 16, 31, 16, 12, 5, 0, 6], ["+", 31, 16, 31, 16, 31, 16, 12, 5, 0, 6]]
1
243
#include <bits/stdc++.h> #define range(i, a, b) for (int i = (a); i < (b); i++) #define rep(i, b) for (int i = 0; i < (b); i++) #define all(a) (a).begin(), (a).end() #define show(x) cerr << #x << " = " << (x) << endl; using namespace std; int main() { int W; int cnt = 0; while (cin >> W, W) { int n; cin >> n; int v[1005], w[1005]; rep(i, n) { char g; cin >> v[i] >> g >> w[i]; } int dp[2005] = {0}; rep(i, W + 1) dp[i] = -1; dp[0] = 0; rep(i, n) { for (int j = W; j >= 0; j--) { if (j - w[i] < 0) continue; if (dp[j - w[i]] == -1) continue; dp[j] = max(dp[j], dp[j - w[i]] + v[i]); } } int ans_v = 0, ans_w; rep(i, W + 1) { if (ans_v < dp[i]) { ans_v = dp[i]; ans_w = i; } } cout << "Case " << cnt << ":" << endl; cout << ans_v << endl; cout << ans_w << endl; cnt++; } }
#include <bits/stdc++.h> #define range(i, a, b) for (int i = (a); i < (b); i++) #define rep(i, b) for (int i = 0; i < (b); i++) #define all(a) (a).begin(), (a).end() #define show(x) cerr << #x << " = " << (x) << endl; using namespace std; int main() { int W; int cnt = 0; while (cin >> W, W) { int n; cin >> n; int v[1005], w[1005]; rep(i, n) { char g; cin >> v[i] >> g >> w[i]; } int dp[2005] = {0}; rep(i, W + 1) dp[i] = -1; dp[0] = 0; rep(i, n) { for (int j = W; j >= 0; j--) { if (j - w[i] < 0) continue; if (dp[j - w[i]] == -1) continue; dp[j] = max(dp[j], dp[j - w[i]] + v[i]); } } int ans_v = 0, ans_w = 0; rep(i, W + 1) { if (ans_v < dp[i]) { ans_v = dp[i]; ans_w = i; } } cout << "Case " << cnt + 1 << ":" << endl; cout << ans_v << endl; cout << ans_w << endl; cnt++; } }
[["+", 0, 52, 8, 9, 0, 43, 49, 50, 0, 32], ["+", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 16, 31, 16, 31, 16, 12, 16, 17, 72], ["+", 0, 16, 31, 16, 31, 16, 12, 16, 12, 13]]
1
285
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <functional> #include <iostream> #include <map> #include <set> #include <sstream> #include <string> #include <vector> using namespace std; typedef pair<int, int> P; typedef long long ll; const ll MOD = 1000000007; int n, w; int t; int main() { t = 1; while (cin >> w, w) { int dp[1001][1001] = {0}; cin >> n; vector<P> num(n); for (int i = 0; i < n; i++) { char tt; int x, y; cin >> x >> tt >> y; num[i].first = x; num[i].second = y; } for (int i = 0; i < n; i++) { for (int j = 0; j <= w; j++) { if (num[i].second - j <= 0) dp[i + 1][j] = max(dp[i][j], dp[i][j - num[i].second] + num[i].first); else dp[i + 1][j] = dp[i][j]; } } cout << "Case " << t << ":" << endl; ; t++; int mmax = 0; int ans; for (int i = w; i >= 0; i--) { if (mmax <= dp[n - 1][i]) { mmax = dp[n - 1][i]; ans = i; } } cout << mmax << endl; cout << ans - 1 << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <functional> #include <iostream> #include <map> #include <set> #include <sstream> #include <string> #include <vector> using namespace std; typedef pair<int, int> P; typedef long long ll; const ll MOD = 1000000007; int n, w; int t; int main() { t = 1; while (cin >> w, w) { int dp[1001][1001] = {0}; cin >> n; vector<P> num(n); for (int i = 0; i < n; i++) { char tt; int x, y; cin >> x >> tt >> y; num[i].first = x; num[i].second = y; } for (int i = 0; i < n; i++) { for (int j = 0; j <= w; j++) { if (num[i].second - j <= 0) dp[i + 1][j] = max(dp[i][j], dp[i][j - num[i].second] + num[i].first); else dp[i + 1][j] = dp[i][j]; } } cout << "Case " << t << ":" << endl; ; t++; int mmax = 0; int ans; for (int i = w; i >= 0; i--) { if (mmax <= dp[n][i]) { mmax = dp[n][i]; ans = i; } } cout << mmax << endl; cout << ans << endl; } return 0; }
[["-", 12, 69, 28, 69, 341, 342, 0, 16, 17, 33], ["-", 12, 69, 28, 69, 341, 342, 0, 16, 12, 13], ["-", 0, 1, 0, 16, 31, 16, 12, 16, 17, 33], ["-", 0, 1, 0, 16, 31, 16, 12, 16, 12, 13]]
1
348
#include <algorithm> #include <stdio.h> using namespace std; int W, N, v[1000], w[1000], dp[1001][1001], c, r; int main() { while (~scanf("%d", &W)) { if (!W) break; scanf("%d", &N); for (int i = 0; i < N; i++) scanf("%d,%d", &w[i], &v[i]); for (int i = 1; i <= N; i++) { for (int j = 0; j <= W; j++) { dp[i][j] = dp[i - 1][j]; if (j >= w[i - 1]) { dp[i][j] = max(dp[i][j], dp[i - 1][j - w[i - 1]] + v[i - 1]); } } } printf("Case %d:\n", ++c); printf("%d\n", dp[N][W]); printf("%d\n", lower_bound(dp[N], dp[N] + W + 1, dp[N][W]) - dp[N]); } return 0; }
#include <algorithm> #include <stdio.h> using namespace std; int W, N, v[1000], w[1000], dp[1001][1001], c, r; int main() { while (~scanf("%d", &W)) { if (!W) break; scanf("%d", &N); for (int i = 0; i < N; i++) scanf("%d,%d", &v[i], &w[i]); for (int i = 1; i <= N; i++) { for (int j = 0; j <= W; j++) { dp[i][j] = dp[i - 1][j]; if (j >= w[i - 1]) { dp[i][j] = max(dp[i][j], dp[i - 1][j - w[i - 1]] + v[i - 1]); } } } printf("Case %d:\n", ++c); printf("%d\n", dp[N][W]); printf("%d\n", lower_bound(dp[N], dp[N] + W + 1, dp[N][W]) - dp[N]); } return 0; }
[["-", 0, 2, 3, 4, 0, 66, 28, 69, 28, 22], ["+", 0, 2, 3, 4, 0, 66, 28, 69, 28, 22]]
1
280
#include <algorithm> #include <cstdio> #include <iostream> #include <utility> #define N_MAX 1000 // ??????????????° ?????§??? #define W_MAX 1000 // ?¢¨?????????????????????????????? ?????§??? using namespace std; int dp[N_MAX + 1][W_MAX + 1]; int v[N_MAX + 1], w[N_MAX + 1]; int N, W; pair<int, int> knapsack() { int ans_w, ans_v; // ????????? for (int i = 0; i <= N; i++) { for (int j = 0; j <= W; j++) { dp[i][j] = 0; } } // ????????¨??? for (int i = 1; i <= N; i++) { for (int j = 1; j <= W; j++) { if (w[i] <= j) { dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]); } else { dp[i][j] = dp[i - 1][j]; } } } // ???????§£?????¢??? ans_v = -1; ans_w = 0; for (int i = W; i >= 1; i--) { if (ans_v <= dp[N][i]) { ans_v = dp[N][i]; ans_w = i; } } return make_pair(ans_v, ans_w); } int main() { int cont = 1; char c; pair<int, int> ans; while (cin >> W) { if (W == 0) break; cin >> N; for (int i = 1; i <= N; i++) { cin >> v[i] >> c >> w[i]; } ans = knapsack(); printf("Case %d:\n%d\n%d\n", cont, ans.first, ans.second); } return 0; }
#include <algorithm> #include <cstdio> #include <iostream> #include <utility> #define N_MAX 1000 // ??????????????° ?????§??? #define W_MAX 1000 // ?¢¨?????????????????????????????? ?????§??? using namespace std; int dp[N_MAX + 1][W_MAX + 1]; int v[N_MAX + 1], w[N_MAX + 1]; int N, W; pair<int, int> knapsack() { int ans_w, ans_v; // ????????? for (int i = 0; i <= N; i++) { for (int j = 0; j <= W; j++) { dp[i][j] = 0; } } // ????????¨??? for (int i = 1; i <= N; i++) { for (int j = 1; j <= W; j++) { if (w[i] <= j) { dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]); } else { dp[i][j] = dp[i - 1][j]; } } } // ???????§£?????¢??? ans_v = -1; ans_w = 0; for (int i = W; i >= 1; i--) { if (ans_v <= dp[N][i]) { ans_v = dp[N][i]; ans_w = i; } } return make_pair(ans_v, ans_w); } int main() { int cont = 1; char c; pair<int, int> ans; while (cin >> W) { if (W == 0) break; cin >> N; for (int i = 1; i <= N; i++) { cin >> v[i] >> c >> w[i]; } ans = knapsack(); printf("Case %d:\n%d\n%d\n", cont, ans.first, ans.second); cont++; } return 0; }
[["+", 0, 52, 8, 9, 0, 1, 0, 27, 28, 22], ["+", 0, 52, 8, 9, 0, 1, 0, 27, 17, 29], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
1
380
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vl; typedef pair<int, int> pii; #define REP(i, n) for (ll i = 0; i < n; ++i) #define REPR(i, n) for (ll i = 1; i < n; ++i) #define FOR(i, a, b) for (ll i = a; i < b; ++i) #define DEBUG(x) cout << #x << ": " << x << endl #define DEBUG_VEC(v) \ cout << #v << ":"; \ REP(i, v.size()) cout << " " << v[i]; \ cout << endl #define ALL(a) (a).begin(), (a).end() #define MOD (ll)(1e9 + 7) #define ADD(a, b) a = ((a) + (b)) % MOD #define FIX(a) ((a) % MOD + MOD) % MOD #define INF 1000000000 typedef long double Real; typedef complex<Real> P; Real dot(P a, P b) { return real(conj(a) * b); } Real cross(P a, P b) { return imag(conj(a) * b); } int main() { int cnt = 1; while (true) { int w; cin >> w; if (w == 0) break; int n; cin >> n; vi dp(w + 1, -INF); dp[0] = 0; REP(_, n) { int val, wei; scanf("%d,%d", &val, &wei); for (int i = w; i >= 0; --i) { if (i - wei < 0) break; dp[i] = max(dp[i], dp[i - wei] + val); } } int mxval = 0, thenwei = 0; REPR(i, w) { if (dp[i] > mxval) { mxval = dp[i]; thenwei = i; } } printf("Case %d:\n", cnt); cout << mxval << endl << thenwei << endl; ++cnt; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vl; typedef pair<int, int> pii; #define REP(i, n) for (ll i = 0; i < n; ++i) #define REPR(i, n) for (ll i = 1; i < n; ++i) #define FOR(i, a, b) for (ll i = a; i < b; ++i) #define DEBUG(x) cout << #x << ": " << x << endl #define DEBUG_VEC(v) \ cout << #v << ":"; \ REP(i, v.size()) cout << " " << v[i]; \ cout << endl #define ALL(a) (a).begin(), (a).end() #define MOD (ll)(1e9 + 7) #define ADD(a, b) a = ((a) + (b)) % MOD #define FIX(a) ((a) % MOD + MOD) % MOD #define INF 1000000000 typedef long double Real; typedef complex<Real> P; Real dot(P a, P b) { return real(conj(a) * b); } Real cross(P a, P b) { return imag(conj(a) * b); } int main() { int cnt = 1; while (true) { int w; cin >> w; if (w == 0) break; int n; cin >> n; vi dp(w + 1, -INF); dp[0] = 0; REP(_, n) { int val, wei; scanf("%d,%d", &val, &wei); for (int i = w; i >= 0; --i) { if (i - wei < 0) break; dp[i] = max(dp[i], dp[i - wei] + val); } } int mxval = 0, thenwei = 0; REPR(i, w + 1) { if (dp[i] > mxval) { mxval = dp[i]; thenwei = i; } } printf("Case %d:\n", cnt); cout << mxval << endl << thenwei << endl; ++cnt; } return 0; }
[["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 72], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13], ["+", 0, 1, 0, 2, 3, 4, 0, 25, 0, 35]]
1
351
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> using namespace std; #pragma warning(disable : 4996) #define MAX_N 1200 #define MAX_W 1200 int dp[MAX_N][MAX_W]; int W, N, v[MAX_N], w[MAX_N], cnt; int main() { while (true) { cnt++; memset(dp, 0, sizeof(dp)); cin >> W; if (W == 0) { break; } cin >> N; for (int i = 0; i < N; i++) { scanf("%d,%d", &v[i], &w[i]); } for (int i = 0; i < N; i++) { for (int j = 0; j <= W; j++) { dp[i + 1][j] = max(dp[i][j], dp[i + 1][j]); if (j + w[i] < W) { dp[i + 1][j + w[i]] = max(dp[i][j] + v[i], dp[i + 1][j + w[i]]); } } } cout << "Case " << cnt << ":" << endl; int maxn = 0, maxid = 0; for (int j = 0; j <= W; j++) { if (maxn < dp[N][j]) { maxn = dp[N][j]; maxid = j; } else if (maxn == dp[N][j]) { maxid = min(maxid, j); } } cout << maxn << endl; cout << maxid << endl; } }
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> using namespace std; #pragma warning(disable : 4996) #define MAX_N 1200 #define MAX_W 1200 int dp[MAX_N][MAX_W]; int W, N, v[MAX_N], w[MAX_N], cnt; int main() { while (true) { cnt++; memset(dp, 0, sizeof(dp)); cin >> W; if (W == 0) { break; } cin >> N; for (int i = 0; i < N; i++) { scanf("%d,%d", &v[i], &w[i]); } for (int i = 0; i < N; i++) { for (int j = 0; j <= W; j++) { dp[i + 1][j] = max(dp[i][j], dp[i + 1][j]); if (j + w[i] <= W) { dp[i + 1][j + w[i]] = max(dp[i][j] + v[i], dp[i + 1][j + w[i]]); } } } cout << "Case " << cnt << ":" << endl; int maxn = 0, maxid = 0; for (int j = 0; j <= W; j++) { if (maxn < dp[N][j]) { maxn = dp[N][j]; maxid = j; } else if (maxn == dp[N][j]) { maxid = min(maxid, j); } } cout << maxn << endl; cout << maxid << endl; } }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 18], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 19]]
1
351
#include <bits/stdc++.h> using namespace std; #define pb(n) push_back(n) #define fi first #define se second #define all(r) (r).begin(), (r).end() #define gsort(st, en) sort((st), (en), greater<int>()) #define vmax(ary) *max_element(all(ary)) #define vmin(ary) *min_element(all(ary)) #define debug(x) cout << #x << ": " << x << endl #define fcout(n) cout << fixed << setprecision((n)) #define scout(n) cout << setw(n) #define vary(type, name, size, init) vector<type> name(size, init) #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define REP(i, a, b) for (int i = (a); i < (int)(b); ++i) #define repi(it, array) \ for (auto it = array.begin(), end = array.end(); it != end; ++it) #define repa(n, array) for (auto &n : (array)) using ll = long long; using vi = vector<int>; using vl = vector<ll>; using dict = map<string, int>; using pii = pair<int, int>; constexpr int imax = ((1 << 30) - 1) * 2 + 1; constexpr int inf = 100000000; constexpr double PI = acos(-1.0); double eps = 1e-10; const int dy[] = {-1, 0, 1, 0}; const int dx[] = {0, -1, 0, 1}; inline bool value(int x, int y, int w, int h) { return (x >= 0 && x < w && y >= 0 && y < h); } template <typename T> void Unique(vector<T> &v) { sort(all(v)); v.erase(unique(all(v)), v.end()); } template <typename T> T ston(string &str, T n) { istringstream sin(str); T num; sin >> num; return num; } void Ans(bool f) { if (f) cout << "YES" << endl; else cout << "NO" << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); int w, n, v, w2; int cnt = 1; char c; while (cin >> w) { if (w == 0) break; cin >> n; int dp[1001][1001] = {}; // vector<vector<int>> dp2(n+1,vector<int>(w+1,0)); REP(i, 0, n) { cin >> v >> c >> w2; rep(j, w) { dp[i + 1][j] = max(dp[i][j], j - w2 >= 0 ? dp[i][j - w2] + v : 0); } } cout << "Case " + to_string(cnt) + ":" << endl; rep(i, w + 1) { if (dp[n][i] == dp[n][w - 1]) { cout << dp[n][w - 1] << endl << i << endl; break; } } ++cnt; } return 0; }
#include <bits/stdc++.h> using namespace std; #define pb(n) push_back(n) #define fi first #define se second #define all(r) (r).begin(), (r).end() #define gsort(st, en) sort((st), (en), greater<int>()) #define vmax(ary) *max_element(all(ary)) #define vmin(ary) *min_element(all(ary)) #define debug(x) cout << #x << ": " << x << endl #define fcout(n) cout << fixed << setprecision((n)) #define scout(n) cout << setw(n) #define vary(type, name, size, init) vector<type> name(size, init) #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define REP(i, a, b) for (int i = (a); i < (int)(b); ++i) #define repi(it, array) \ for (auto it = array.begin(), end = array.end(); it != end; ++it) #define repa(n, array) for (auto &n : (array)) using ll = long long; using vi = vector<int>; using vl = vector<ll>; using dict = map<string, int>; using pii = pair<int, int>; constexpr int imax = ((1 << 30) - 1) * 2 + 1; constexpr int inf = 100000000; constexpr double PI = acos(-1.0); double eps = 1e-10; const int dy[] = {-1, 0, 1, 0}; const int dx[] = {0, -1, 0, 1}; inline bool value(int x, int y, int w, int h) { return (x >= 0 && x < w && y >= 0 && y < h); } template <typename T> void Unique(vector<T> &v) { sort(all(v)); v.erase(unique(all(v)), v.end()); } template <typename T> T ston(string &str, T n) { istringstream sin(str); T num; sin >> num; return num; } void Ans(bool f) { if (f) cout << "YES" << endl; else cout << "NO" << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); int w, n, v, w2; int cnt = 1; char c; while (cin >> w) { if (w == 0) break; cin >> n; int dp[1001][1001] = {}; REP(i, 0, n) { cin >> v >> c >> w2; rep(j, w + 1) { dp[i + 1][j] = max(dp[i][j], j - w2 >= 0 ? dp[i][j - w2] + v : 0); } } cout << "Case " + to_string(cnt) + ":" << endl; rep(i, w + 1) { if (dp[n][i] == dp[n][w]) { cout << dp[n][w] << endl << i << endl; break; } } ++cnt; } return 0; }
[["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 72], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13], ["+", 0, 1, 0, 2, 3, 4, 0, 25, 0, 35], ["-", 51, 16, 12, 69, 341, 342, 0, 16, 17, 33], ["-", 51, 16, 12, 69, 341, 342, 0, 16, 12, 13], ["-", 31, 16, 12, 69, 341, 342, 0, 16, 17, 33], ["-", 31, 16, 12, 69, 341, 342, 0, 16, 12, 13]]
1
578
#include <algorithm> #include <bitset> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> i_i; typedef pair<long long, int> ll_i; typedef pair<double, int> d_i; typedef pair<long long, long long> ll_ll; typedef pair<double, double> d_d; #define PI 3.141592653589793238462643383279 #define mod 1000000007LL #define rep(i, n) for (i = 0; i < n; ++i) #define rep1(i, n) for (i = 1; i < n; ++i) #define rep2d(i, j, n) \ for (i = 0; i < n; ++i) \ for (j = i + 1; j < n; ++j) #define per(i, n) for (i = n - 1; i > -1; --i) #define int(x) \ int x; \ scanf("%d", &x) #define int2(x, y) \ int x, y; \ scanf("%d%d", &x, &y) #define int3(x, y, z) \ int x, y, z; \ scanf("%d%d%d", &x, &y, &z) #define sc(x) cin >> x #define sc2(x, y) cin >> x >> y #define sc3(x, y, z) cin >> x >> y >> z #define scn(n, a) rep(i, n) cin >> a[i] #define sc2n(n, a, b) rep(i, n) cin >> a[i] >> b[i] #define pri(x) cout << x << "\n" #define pri2(x, y) cout << x << " " << y << "\n" #define pri3(x, y, z) cout << x << " " << y << " " << z << "\n" #define pb push_back #define mp make_pair #define all(a) (a).begin(), (a).end() #define kabe puts("---------------------------") #define kara puts("") #define debug(x) cout << " --- " << x << "\n" #define debug2(x, y) cout << " --- " << x << " " << y << "\n" #define debug3(x, y, z) cout << " --- " << x << " " << y << " " << z << "\n" #define debugn(i, n, a) rep(i, n) cout << " --- " << a[i] << "\n"; #define debugin(i, n, a) rep(i, n) printf(" --- %10d\n", a[i]) #define debugi2n(i, n, a, b) rep(i, n) printf(" --- %10d %10d\n", a[i], b[i]) #define debugiin(i, n, a) \ rep(i, n) printf(" --- %10d %10d\n", a[i].first, a[i].second) #define X first #define Y second #define eps 0.0001 #define prid(x) printf("%.15lf\n", x) signed main(void) { int i, j; for (int testcase = 0;; testcase++) { int(w); if (w == 0) break; int(n); int v[n], d[n]; rep(i, n) scanf("%d,%d", &v[i], &d[i]); ll dp[n + 1][w + 1]; rep(i, n + 1) rep(j, w + 1) dp[n + 1][w + 1] = 0; rep1(i, n + 1) rep(j, w + 1) { dp[i][j] = dp[i - 1][j]; if (j - d[i - 1] >= 0) dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - d[i - 1]] + v[i - 1]); } per(i, w + 1) if (dp[n][w] != dp[n][i]) break; i++; printf("Case %d:\n%lld\n%d\n", testcase + 1, dp[n][w], i); } return 0; }
#include <algorithm> #include <bitset> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> i_i; typedef pair<long long, int> ll_i; typedef pair<double, int> d_i; typedef pair<long long, long long> ll_ll; typedef pair<double, double> d_d; #define PI 3.141592653589793238462643383279 #define mod 1000000007LL #define rep(i, n) for (i = 0; i < n; ++i) #define rep1(i, n) for (i = 1; i < n; ++i) #define rep2d(i, j, n) \ for (i = 0; i < n; ++i) \ for (j = i + 1; j < n; ++j) #define per(i, n) for (i = n - 1; i > -1; --i) #define int(x) \ int x; \ scanf("%d", &x) #define int2(x, y) \ int x, y; \ scanf("%d%d", &x, &y) #define int3(x, y, z) \ int x, y, z; \ scanf("%d%d%d", &x, &y, &z) #define sc(x) cin >> x #define sc2(x, y) cin >> x >> y #define sc3(x, y, z) cin >> x >> y >> z #define scn(n, a) rep(i, n) cin >> a[i] #define sc2n(n, a, b) rep(i, n) cin >> a[i] >> b[i] #define pri(x) cout << x << "\n" #define pri2(x, y) cout << x << " " << y << "\n" #define pri3(x, y, z) cout << x << " " << y << " " << z << "\n" #define pb push_back #define mp make_pair #define all(a) (a).begin(), (a).end() #define kabe puts("---------------------------") #define kara puts("") #define debug(x) cout << " --- " << x << "\n" #define debug2(x, y) cout << " --- " << x << " " << y << "\n" #define debug3(x, y, z) cout << " --- " << x << " " << y << " " << z << "\n" #define debugn(i, n, a) rep(i, n) cout << " --- " << a[i] << "\n"; #define debugin(i, n, a) rep(i, n) printf(" --- %10d\n", a[i]) #define debugi2n(i, n, a, b) rep(i, n) printf(" --- %10d %10d\n", a[i], b[i]) #define debugiin(i, n, a) \ rep(i, n) printf(" --- %10d %10d\n", a[i].first, a[i].second) #define X first #define Y second #define eps 0.0001 #define prid(x) printf("%.15lf\n", x) signed main(void) { int i, j; for (int testcase = 0;; testcase++) { int(w); if (w == 0) break; int(n); int v[n], d[n]; rep(i, n) scanf("%d,%d", &v[i], &d[i]); ll dp[n + 1][w + 1]; rep(i, n + 1) rep(j, w + 1) dp[i][j] = 0; rep1(i, n + 1) rep(j, w + 1) { dp[i][j] = dp[i - 1][j]; if (j - d[i - 1] >= 0) dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - d[i - 1]] + v[i - 1]); } per(i, w + 1) if (dp[n][w] != dp[n][i]) break; i++; printf("Case %d:\n%lld\n%d\n", testcase + 1, dp[n][w], i); } return 0; }
[["-", 31, 69, 28, 69, 341, 342, 0, 16, 31, 22], ["-", 31, 69, 28, 69, 341, 342, 0, 16, 17, 72], ["-", 31, 69, 28, 69, 341, 342, 0, 16, 12, 13], ["+", 0, 11, 31, 69, 28, 69, 341, 342, 0, 22], ["-", 0, 11, 31, 69, 341, 342, 0, 16, 31, 22], ["-", 0, 11, 31, 69, 341, 342, 0, 16, 17, 72], ["-", 0, 11, 31, 69, 341, 342, 0, 16, 12, 13], ["+", 0, 1, 0, 11, 31, 69, 341, 342, 0, 22]]
1
622
#define _USE_MATH_DEFINES #include <algorithm> #include <bitset> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; int main() { int W, N; for (int h = 1; cin >> W && W; h++) { cin >> N; int v, w; int dp[1001] = {}; for (int i = 0; i < N; i++) { char c; cin >> v >> c >> w; for (int j = W; j > 0; j--) { if (j - w < 0) continue; dp[j] = max(dp[j], dp[j - w] + v); } } int a = 0, b = 0; for (int i = 0; i < W; i++) { if (a < dp[i]) { a = dp[i]; b = i; } } cout << "Case " << h << ":" << endl; cout << a << endl; cout << b << endl; } return 0; }
#define _USE_MATH_DEFINES #include <algorithm> #include <bitset> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; int main() { int W, N; for (int h = 1; cin >> W && W; h++) { cin >> N; int v, w; int dp[1001] = {}; for (int i = 0; i < N; i++) { char c; cin >> v >> c >> w; for (int j = W; j > 0; j--) { if (j - w < 0) continue; dp[j] = max(dp[j], dp[j - w] + v); } } int a = 0, b = 0; for (int i = 0; i <= W; i++) { if (a < dp[i]) { a = dp[i]; b = i; } } cout << "Case " << h << ":" << endl; cout << a << endl; cout << b << endl; } return 0; }
[["-", 0, 7, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 17, 19]]
1
240
#include <stdio.h> int a, s, d[1001], f[1001], g[1001][1001], h, z, i, j, x; int main(void) { x = 0; scanf("%d", &a); while (a != 0) { x++; scanf("%d", &s); for (i = 0; i < s; i++) { scanf("%d,%d", &d[i], &f[i]); } for (i = 0; i <= s; i++) { for (j = 0; j <= a; j++) { g[i][j] = 0; } } for (i = 0; i <= s; i++) { for (j = 0; j <= a; j++) { if (g[i + 1][j + f[i]] < g[i][j] + d[i] && j + f[i] <= a) { g[i + 1][j + f[i]] = g[i][j] + d[i]; } if (g[i + 1][j] < g[i][j]) { g[i + 1][j] = g[i][j]; } } } /* for(i=0;i<=s;i++){ for(j=0;j<=a;j++){ printf("%d ",g[i][j]); } printf("\n"); }*/ h = 0; z = 0; for (i = 0; i <= a; i++) { if (h < g[s][i]) { h = g[s][i]; z = i; } } printf("Case %d\n", x); printf("%d\n%d\n", h, z); scanf("%d", &a); } return 0; }
#include <stdio.h> int a, s, d[1001], f[1001], g[1001][1001], h, z, i, j, x; int main(void) { x = 0; scanf("%d", &a); while (a != 0) { x++; scanf("%d", &s); for (i = 0; i < s; i++) { scanf("%d,%d", &d[i], &f[i]); } for (i = 0; i <= s; i++) { for (j = 0; j <= a; j++) { g[i][j] = 0; } } for (i = 0; i <= s; i++) { for (j = 0; j <= a; j++) { if (g[i + 1][j + f[i]] < g[i][j] + d[i] && j + f[i] <= a) { g[i + 1][j + f[i]] = g[i][j] + d[i]; } if (g[i + 1][j] < g[i][j]) { g[i + 1][j] = g[i][j]; } } } /* for(i=0;i<=s;i++){ for(j=0;j<=a;j++){ printf("%d ",g[i][j]); } printf("\n"); }*/ h = 0; z = 0; for (i = 0; i <= a; i++) { if (h < g[s][i]) { h = g[s][i]; z = i; } } printf("Case %d:\n", x); printf("%d\n%d\n", h, z); scanf("%d", &a); } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
379
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <ctime> #include <deque> #include <iostream> #include <limits> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define rep(i, a, n) for (int i = a; i < n; i++) #define repr(i, a, n) for (int i = a; i >= n; i--) #define pb(a) push_back(a) #define fr first #define sc second #define INF 999999999 #define X real() #define Y imag() #define EPS (1e-10) #define EQ(a, b) (abs((a) - (b)) < EPS) #define EQV(a, b) (EQ((a).X, (b).X) && EQ((a).Y, (b).Y)) #define LE(n, m) ((n) < (m) + EPS) #define GE(n, m) ((n) + EPS > (m)) typedef vector<int> VI; typedef vector<VI> MAT; typedef pair<int, int> pii; typedef long long int ll; typedef complex<double> P; typedef pair<P, P> L; typedef pair<P, double> C; namespace std { bool operator<(const P a, const P b) { return a.X != b.X ? a.X < b.X : a.Y < b.Y; } } // namespace std int const MOD = 1000000007; int main() { int W, N; int c = 0; while (cin >> W) { if (W == 0) break; c++; cin >> N; int v[1010], w[1010]; rep(i, 0, N) scanf("%d,%d", &v[i], &w[i]); int dp[1010][1010]; rep(j, 0, W + 1) dp[0][j] = 0; rep(i, 0, N) { rep(j, 0, W + 1) { if (j < w[i]) dp[i + 1][j] = dp[i][j]; else dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]); } } int a = 0, b = 0; rep(i, 0, W + 1) { if (dp[N][i] > a) { a = dp[N][i]; b = i; } } printf("Case %d:", c); cout << a << endl; cout << b << endl; } return 0; }
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <ctime> #include <deque> #include <iostream> #include <limits> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define rep(i, a, n) for (int i = a; i < n; i++) #define repr(i, a, n) for (int i = a; i >= n; i--) #define pb(a) push_back(a) #define fr first #define sc second #define INF 999999999 #define X real() #define Y imag() #define EPS (1e-10) #define EQ(a, b) (abs((a) - (b)) < EPS) #define EQV(a, b) (EQ((a).X, (b).X) && EQ((a).Y, (b).Y)) #define LE(n, m) ((n) < (m) + EPS) #define GE(n, m) ((n) + EPS > (m)) typedef vector<int> VI; typedef vector<VI> MAT; typedef pair<int, int> pii; typedef long long int ll; typedef complex<double> P; typedef pair<P, P> L; typedef pair<P, double> C; namespace std { bool operator<(const P a, const P b) { return a.X != b.X ? a.X < b.X : a.Y < b.Y; } } // namespace std int const MOD = 1000000007; int main() { int W, N; int c = 0; while (cin >> W) { if (W == 0) break; c++; cin >> N; int v[1010], w[1010]; rep(i, 0, N) scanf("%d,%d", &v[i], &w[i]); int dp[1010][1010]; rep(j, 0, W + 1) dp[0][j] = 0; rep(i, 0, N) { rep(j, 0, W + 1) { if (j < w[i]) dp[i + 1][j] = dp[i][j]; else dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]); } } int a = 0, b = 0; rep(i, 0, W + 1) { if (dp[N][i] > a) { a = dp[N][i]; b = i; } } printf("Case %d:\n", c); cout << a << endl; cout << b << endl; } return 0; }
[["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44]]
1
490
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; int parse(string a) { int s = 0; for (char b : a) { s *= 10; s += b - 48; } return s; } vector<int> split(string a) { vector<int> b; string c = ""; for (char d : a) { if (d == ',') { b.push_back(parse(c)); c = ""; } else c += d; } b.push_back(parse(c)); return b; } int main() { int u = 1; int a, b; while (cin >> b, b) { cin >> a; vector<vector<int>> dp(a + 1, vector<int>(b + 1)); for (int c = 0; c < a; c++) { string q; cin >> q; int d, e; d = split(q)[0]; e = split(q)[1]; for (int f = 0; f < b + 1; f++) { if (f < e) dp[c + 1][f] = dp[c][f]; else dp[c + 1][f] = max(dp[c][f], dp[c][f - e] + d); } } cout << "Case" << u << ":" << endl; int y = 0, z = 0; for (int x = 0; x < b + 1; x++) { if (dp[a][x] > y) { y = dp[a][x]; z = x; } } cout << y << endl << z << endl; u++; } }
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; int parse(string a) { int s = 0; for (char b : a) { s *= 10; s += b - 48; } return s; } vector<int> split(string a) { vector<int> b; string c = ""; for (char d : a) { if (d == ',') { b.push_back(parse(c)); c = ""; } else c += d; } b.push_back(parse(c)); return b; } int main() { int u = 1; int a, b; while (cin >> b, b) { cin >> a; vector<vector<int>> dp(a + 1, vector<int>(b + 1)); for (int c = 0; c < a; c++) { string q; cin >> q; int d, e; d = split(q)[0]; e = split(q)[1]; for (int f = 0; f < b + 1; f++) { if (f < e) dp[c + 1][f] = dp[c][f]; else dp[c + 1][f] = max(dp[c][f], dp[c][f - e] + d); } } cout << "Case " << u << ":" << endl; int y = 0, z = 0; for (int x = 0; x < b + 1; x++) { if (dp[a][x] > y) { y = dp[a][x]; z = x; } } cout << y << endl << z << endl; u++; } }
[["-", 31, 16, 31, 16, 31, 16, 12, 5, 0, 6], ["+", 31, 16, 31, 16, 31, 16, 12, 5, 0, 6]]
1
382
#include <algorithm> #include <cstdio> #include <utility> using namespace std; typedef pair<int, int> Pair; int w; int n; int vs[1001]; int ws[1001]; int dp[1001][1001]; int main() { for (int Case = 1;; Case++) { scanf("%d", &w); if (w == 0) return 0; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d,%d", &vs[i], &ws[i]); } for (int j = 0; j <= w; j++) dp[0][j] = 0; for (int i = 0; i < n; i++) { for (int j = 0; j <= w; j++) { dp[i + 1][j] = dp[i][j]; } for (int j = 0; j <= w - ws[i]; j++) { dp[i + 1][j + ws[i]] = max(dp[i + 1][j + ws[i]], dp[i][j] + vs[i]); } } Pair ans(0, 0); for (int j = 0; j <= w; j++) { ans = max(ans, Pair(dp[n][j], j)); } printf("Case %d:\n%d\n%d\n", Case, ans.first, ans.second); } }
#include <algorithm> #include <cstdio> #include <utility> using namespace std; typedef pair<int, int> Pair; int w; int n; int vs[1001]; int ws[1001]; int dp[1001][1001]; int main() { for (int Case = 1;; Case++) { scanf("%d", &w); if (w == 0) return 0; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d,%d", &vs[i], &ws[i]); } for (int j = 0; j <= w; j++) dp[0][j] = 0; for (int i = 0; i < n; i++) { for (int j = 0; j <= w; j++) { dp[i + 1][j] = dp[i][j]; } for (int j = 0; j <= w - ws[i]; j++) { dp[i + 1][j + ws[i]] = max(dp[i + 1][j + ws[i]], dp[i][j] + vs[i]); } } Pair ans(0, 0); for (int j = 0; j <= w; j++) { ans = max(ans, Pair(dp[n][j], -j)); } printf("Case %d:\n%d\n%d\n", Case, ans.first, -ans.second); } }
[["+", 3, 4, 0, 2, 3, 4, 0, 91, 17, 33], ["+", 0, 1, 0, 2, 3, 4, 0, 91, 17, 33]]
1
336
#include <cstdio> #include <iostream> using namespace std; int main() { int n, W; int v[1000], w[1000]; for (int cnt = 0;; cnt++) { cin >> W >> n; if (!W) break; int dp[1001][1001] = {}, ma = 0, mi = 1e+9; for (int i = 0; i < n; i++) { scanf("%d,%d", &v[i], &w[i]); } dp[0][0] = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j <= W; j++) { if (j - w[i - 1] >= 0) dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i - 1]] + v[i - 1]); else dp[i][j] = dp[i - 1][j]; if (ma < dp[i][j]) { ma = dp[i][j]; mi = j; } else if (ma == dp[i][j]) mi = min(mi, j); } } printf("Case %d:\n%d\n%d\n", cnt, ma, mi); } return 0; }
#include <cstdio> #include <iostream> using namespace std; int main() { int n, W; int v[1000], w[1000]; for (int cnt = 0;; cnt++) { cin >> W >> n; if (!W) break; int dp[1001][1001] = {}, ma = 0, mi = 1e+9; for (int i = 0; i < n; i++) { scanf("%d,%d", &v[i], &w[i]); } dp[0][0] = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j <= W; j++) { if (j - w[i - 1] >= 0) dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i - 1]] + v[i - 1]); else dp[i][j] = dp[i - 1][j]; if (ma < dp[i][j]) { ma = dp[i][j]; mi = j; } else if (ma == dp[i][j]) mi = min(mi, j); } } printf("Case %d:\n%d\n%d\n", cnt + 1, ma, mi); } return 0; }
[["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 72], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
1
301
#define _USE_MATH_DEFINES #include "bits/stdc++.h" using namespace std; //#define int long long #define DBG 1 #define dump(o) \ if (DBG) { \ cerr << #o << " " << o << endl; \ } #define dumpc(o) \ if (DBG) { \ cerr << #o; \ for (auto &e : (o)) \ cerr << " " << e; \ cerr << endl; \ } #define rep(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--) #define each(it, c) for (auto it = (c).begin(); it != (c).end(); it++) #define all(c) c.begin(), c.end() const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f; const int MOD = (int)(1e9 + 7); class takara { public: int v, w; takara() {} takara(int x, int y) : v(x), w(y) {} bool operator<(const takara &other) { return this->w < other.w; } }; signed main() { int W; int x(1); while (cin >> W, W) { int N; cin >> N; vector<takara> list; list.push_back(takara(0, 0)); rep(i, 0, N) { int w, v; char c; cin >> v >> c >> w; list.push_back(takara(v, w)); } vector<vector<int>> v(N + 1, vector<int>(W + 1, 0)); rep(i, 1, N + 1) { rep(j, 1, W + 1) { v[i][j] = v[i - 1][j]; if (j - list[i].w >= 0) { v[i][j] = max(v[i - 1][j], v[i - 1][j - list[i].w] + list[i].v); } } } int ret(0); rep(i, 0, W + 1) { if (v[N][ret] < v[N][i]) ret = i; } cout << "Case " << x++ << ":" << endl; cout << v[N][W - 1] << endl << ret << endl; } return 0; }
#define _USE_MATH_DEFINES #include "bits/stdc++.h" using namespace std; //#define int long long #define DBG 1 #define dump(o) \ if (DBG) { \ cerr << #o << " " << o << endl; \ } #define dumpc(o) \ if (DBG) { \ cerr << #o; \ for (auto &e : (o)) \ cerr << " " << e; \ cerr << endl; \ } #define rep(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--) #define each(it, c) for (auto it = (c).begin(); it != (c).end(); it++) #define all(c) c.begin(), c.end() const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f; const int MOD = (int)(1e9 + 7); class takara { public: int v, w; takara() {} takara(int x, int y) : v(x), w(y) {} bool operator<(const takara &other) { return this->w < other.w; } }; signed main() { int W; int x(1); while (cin >> W, W) { int N; cin >> N; vector<takara> list; list.push_back(takara(0, 0)); rep(i, 0, N) { int w, v; char c; cin >> v >> c >> w; list.push_back(takara(v, w)); } vector<vector<int>> v(N + 1, vector<int>(W + 1, 0)); rep(i, 1, N + 1) { rep(j, 1, W + 1) { v[i][j] = v[i - 1][j]; if (j - list[i].w >= 0) { v[i][j] = max(v[i - 1][j], v[i - 1][j - list[i].w] + list[i].v); } } } int ret(0); rep(i, 0, W + 1) { if (v[N][ret] < v[N][i]) ret = i; } cout << "Case " << x++ << ":" << endl; cout << v[N][W] << endl << ret << endl; } return 0; }
[["-", 31, 16, 12, 69, 341, 342, 0, 16, 17, 33], ["-", 31, 16, 12, 69, 341, 342, 0, 16, 12, 13]]
1
447
#include <bits/stdc++.h> using namespace std; #define FOR(i, l, r) for (int i = (int)(l); i < (int)(r); i++) #define ALL(x) x.begin(), x.end() template <typename T> bool chmax(T &a, const T &b) { return a < b ? (a = b, true) : false; } template <typename T> bool chmin(T &a, const T &b) { return b < a ? (a = b, true) : false; } typedef long long ll; int W, N; int dp[1001][1001]; int main() { int testCase = 1; while (scanf("%d", &W), W) { scanf("%d", &N); vector<int> x(N), y(N); FOR(i, 0, N) { scanf("%d,%d", &x[i], &y[i]); } memset(dp, 0, sizeof(dp)); FOR(i, 0, N) FOR(j, 0, W + 1) { chmax(dp[i + 1][j], dp[i][j]); if (j + y[i] <= W) chmax(dp[i + 1][j + y[i]], dp[i][j] + x[i]); } pair<int, int> ans(-1, -1); FOR(j, 0, W + 1) { if (dp[N][j] > ans.first) { ans.first = dp[N][j]; ans.second = j; } } printf("Case %d\n", testCase); printf("%d\n", ans.first); printf("%d\n", ans.second); testCase++; } return 0; }
#include <bits/stdc++.h> using namespace std; #define FOR(i, l, r) for (int i = (int)(l); i < (int)(r); i++) #define ALL(x) x.begin(), x.end() template <typename T> bool chmax(T &a, const T &b) { return a < b ? (a = b, true) : false; } template <typename T> bool chmin(T &a, const T &b) { return b < a ? (a = b, true) : false; } typedef long long ll; int W, N; int dp[1001][1001]; int main() { int testCase = 1; while (scanf("%d", &W), W) { scanf("%d", &N); vector<int> x(N), y(N); FOR(i, 0, N) { scanf("%d,%d", &x[i], &y[i]); } memset(dp, 0, sizeof(dp)); FOR(i, 0, N) FOR(j, 0, W + 1) { chmax(dp[i + 1][j], dp[i][j]); if (j + y[i] <= W) chmax(dp[i + 1][j + y[i]], dp[i][j] + x[i]); } pair<int, int> ans(-1, -1); FOR(j, 0, W + 1) { if (dp[N][j] > ans.first) { ans.first = dp[N][j]; ans.second = j; } } printf("Case %d:\n", testCase); printf("%d\n", ans.first); printf("%d\n", ans.second); testCase++; } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
387
#include <algorithm> #include <cstdio> #include <iostream> #include <string> #include <vector> using namespace std; int mp[1001][1001]; int main() { int W; char c; for (int num = 0; cin >> W && W; num++) { int v[1001], w[1001], n; cin >> n; fill((int *)mp, (int *)(mp + n + 1), 0); for (int i = 0; i < n; i++) cin >> v[i] >> c >> w[i]; for (int i = 0; i < n; i++) { for (int j = 0; j < W + 1; j++) { if (j < w[i]) mp[i + 1][j] = mp[i][j]; else mp[i + 1][j] = max(mp[i][j], mp[i][j - w[i]] + v[i]); } } int ans = 0; for (int i = 1; i < W + 1; i++) { if (mp[n][i] > mp[n][ans]) ans = i; } cout << "case " << num + 1 << ":" << endl; cout << mp[n][ans] << endl << ans << endl; } return 0; } /* 1 y eazqyp pnop pngtg ye obmpngt xmybp mr lygw */
#include <algorithm> #include <cstdio> #include <iostream> #include <string> #include <vector> using namespace std; int mp[1001][1001]; int main() { int W; char c; for (int num = 0; cin >> W && W; num++) { int v[1000], w[1000], n; cin >> n; fill((int *)mp, (int *)(mp + n + 1), 0); for (int i = 0; i < n; i++) cin >> v[i] >> c >> w[i]; for (int i = 0; i < n; i++) { for (int j = 0; j < W + 1; j++) { if (j < w[i]) mp[i + 1][j] = mp[i][j]; else mp[i + 1][j] = max(mp[i][j], mp[i][j - w[i]] + v[i]); } } int ans = 0; for (int i = 1; i < W + 1; i++) { if (mp[n][i] > mp[n][ans]) ans = i; } cout << "Case " << num + 1 << ":" << endl; cout << mp[n][ans] << endl << ans << endl; } return 0; } /* 1 y eazqyp pnop pngtg ye obmpngt xmybp mr lygw */
[["-", 0, 7, 8, 9, 0, 43, 49, 80, 81, 13], ["+", 0, 7, 8, 9, 0, 43, 49, 80, 81, 13], ["-", 31, 16, 31, 16, 31, 16, 12, 5, 0, 6], ["+", 31, 16, 31, 16, 31, 16, 12, 5, 0, 6]]
1
303
#include <bits/stdc++.h> using namespace std; #define req(i, r, k) for (int i = k; i < r; i++) #define rep(i, r) req(i, r, 0) const long long int INF = 100000000000000; long long int dp[1001][1001]; int main() { for (int a = 1; 1; a++) { int w, n; scanf(" %d", &w); if (w == 0) break; scanf(" %d", &n); pair<int, int> t[n]; char c; rep(i, n) { scanf(" %d%c%d", &t[i].first, &c, &t[i].second); } rep(i, w) { dp[0][i] = 0; dp[i][0] = 0; } dp[0][0] = 0; rep(i, n) { rep(j, w) { if (j >= t[i].second) { dp[i + 1][j] = max(dp[i][j], dp[i][j - t[i].second] + t[i].first); } else { dp[i + 1][j] = dp[i][j]; } } } long long int maxvalue = 0, thenweight = 0; rep(i, w + 1) { // cout << dp[i] << " " << i << " " << thenweight << endl; if (maxvalue < dp[n][i]) { maxvalue = dp[n][i]; thenweight = i; } } // printf("Case %d:\n%lld\n%lld\n",a,maxvalue,thenweight); cout << "Case " << a << ':' << endl; cout << maxvalue << endl; cout << thenweight << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define req(i, r, k) for (int i = k; i < r; i++) #define rep(i, r) req(i, r, 0) const long long int INF = 100000000000000; long long int dp[1002][1002]; int main() { for (int a = 1; 1; a++) { int w, n; scanf(" %d", &w); if (w == 0) break; scanf(" %d", &n); pair<int, int> t[n]; char c; rep(i, n) { scanf(" %d%c%d", &t[i].first, &c, &t[i].second); } rep(i, w) { dp[0][i] = 0; dp[i][0] = 0; } dp[0][0] = 0; rep(i, n + 1) { rep(j, w + 1) { if (j >= t[i].second) { dp[i + 1][j] = max(dp[i][j], dp[i][j - t[i].second] + t[i].first); } else { dp[i + 1][j] = dp[i][j]; } } } long long int maxvalue = 0, thenweight = 0; rep(i, w + 1) { // cout << dp[i] << " " << i << " " << thenweight << endl; if (maxvalue < dp[n][i]) { maxvalue = dp[n][i]; thenweight = i; } } // printf("Case %d:\n%lld\n%lld\n",a,maxvalue,thenweight); cout << "Case " << a << ':' << endl; cout << maxvalue << endl; cout << thenweight << endl; } return 0; }
[["-", 0, 30, 0, 43, 49, 80, 49, 80, 81, 13], ["+", 0, 30, 0, 43, 49, 80, 49, 80, 81, 13], ["-", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 72], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13], ["+", 0, 1, 0, 2, 3, 4, 0, 25, 0, 35]]
1
356
#include <bits/stdc++.h> #include <cctype> #include <cmath> #include <queue> #include <stack> #include <string> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; const int MAX_V = 105; int INF = 1000000000; int main() { int n = 1; int W, N, i, j; while (cin >> W, W) { cin >> N; int dp[N + 1][W + 1]; rep(i, N + 1) { rep(j, W + 1) { dp[i][j] = 0; } } rep(i, N) { int v, w; char c; cin >> v >> c >> w; rep(j, W) { dp[i + 1][j] = max(dp[i][j], dp[i + 1][j]); if (j + w < W + 1) { dp[i + 1][j + w] = max(dp[i][j] + v, dp[i + 1][j + w]); } } } int value1 = 0; int weight = 0; /* rep(j,W+1){ if(dp[N][j]>value1){ value1=dp[N][j]; } }*/ rep(j, W + 1) { if (dp[N][j] == dp[N][W]) { weight = j; break; } } cout << "Case" << ' ' << n++ << ":" << endl; cout << dp[N][W] << endl; cout << weight << endl; } return 0; }
#include <bits/stdc++.h> #include <cctype> #include <cmath> #include <queue> #include <stack> #include <string> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; const int MAX_V = 105; int INF = 1000000000; int main() { int n = 1; int W, N, i, j; while (cin >> W, W) { cin >> N; int dp[N + 1][W + 1]; rep(i, N + 1) { rep(j, W + 1) { dp[i][j] = 0; } } rep(i, N) { int v, w; char c; cin >> v >> c >> w; rep(j, W + 1) { dp[i + 1][j] = max(dp[i][j], dp[i + 1][j]); if (j + w < W + 1) { dp[i + 1][j + w] = max(dp[i][j] + v, dp[i + 1][j + w]); } } } int value1 = 0; int weight = 0; /* rep(j,W+1){ if(dp[N][j]>value1){ value1=dp[N][j]; } }*/ rep(j, W + 1) { if (dp[N][j] == dp[N][W]) { weight = j; break; } } cout << "Case" << ' ' << n++ << ":" << endl; cout << dp[N][W] << endl; cout << weight << endl; } return 0; }
[["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 72], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13], ["+", 0, 1, 0, 2, 3, 4, 0, 25, 0, 35]]
1
314
#include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <memory> #include <numeric> #include <queue> #include <set> #include <string> #include <utility> #include <vector> using namespace std; #define ALL(g) (g).begin(), (g).end() #define REP(i, x, n) for (int i = x; i < n; i++) #define rep(i, n) REP(i, 0, n) #define F(i, j, k) fill(i[0], i[0] + j * j, k) #define P(p) cout << (p) << endl; #define INF 1 << 25 typedef long long ll; int dy[8] = {1, 1, 1, 0, 0, -1, -1, -1}; int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; struct S { int a, b, c; }; bool asc(const S &left, const S &right) { return left.c > right.c; } int W, N; int v[1001], w[1001]; int dp[1001][1001]; int main() { int cnt = 1; while (cin >> W) { if (W == 0) break; cin >> N; rep(i, N) cin >> v[i] >> w[i]; REP(i, 1, N + 1) { rep(j, W + 1) { if (w[i - 1] <= j) dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i - 1]] + v[i - 1]); else dp[i][j] = dp[i - 1][j]; } } int ans_v = 0, ans_w = 0; rep(i, W + 1) { if (dp[N][i] > ans_v) { ans_v = dp[N][i]; ans_w = i; } } cout << "Case " << cnt << ":" << endl; cout << ans_v << endl; cout << ans_w << endl; cnt++; } return 0; }
#include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <memory> #include <numeric> #include <queue> #include <set> #include <string> #include <utility> #include <vector> using namespace std; #define ALL(g) (g).begin(), (g).end() #define REP(i, x, n) for (int i = x; i < n; i++) #define rep(i, n) REP(i, 0, n) #define F(i, j, k) fill(i[0], i[0] + j * j, k) #define P(p) cout << (p) << endl; #define INF 1 << 25 typedef long long ll; int dy[8] = {1, 1, 1, 0, 0, -1, -1, -1}; int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; struct S { int a, b, c; }; bool asc(const S &left, const S &right) { return left.c > right.c; } int W, N; int v[1001], w[1001]; int dp[1001][1001]; int main() { int cnt = 1; char tmp; while (cin >> W) { if (W == 0) break; cin >> N; rep(i, N) cin >> v[i] >> tmp >> w[i]; REP(i, 1, N + 1) { rep(j, W + 1) { if (w[i - 1] <= j) dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i - 1]] + v[i - 1]); else dp[i][j] = dp[i - 1][j]; } } int ans_v = 0, ans_w = 0; rep(i, W + 1) { if (dp[N][i] > ans_v) { ans_v = dp[N][i]; ans_w = i; } } cout << "Case " << cnt << ":" << endl; cout << ans_v << endl; cout << ans_w << endl; cnt++; } return 0; }
[["+", 0, 30, 0, 14, 8, 9, 0, 43, 39, 40], ["+", 0, 30, 0, 14, 8, 9, 0, 43, 49, 22], ["+", 0, 30, 0, 14, 8, 9, 0, 43, 0, 35], ["+", 8, 9, 0, 1, 0, 16, 31, 16, 17, 152], ["+", 8, 9, 0, 1, 0, 16, 31, 16, 12, 22]]
1
432
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> #define INF 100000000 #define LINF 9000000000000000000 #define mod 1000000007 #define rep(i, n) for (int i = 0; i < int(n); i++) #define REP(i, a, b) for (int i = (a); i < int(b); i++) #define all(x) (x).begin(), x.end() #define pb push_back #define mp make_pair #define MOD(x) (x % (mod)) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef pair<int, int> pi; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; bool debug = false; /*---------------------------------------------------*/ int W, N; int v[1005], w[1005]; int dp[1005][1005]; void DEBUG() { rep(i, W + 1) cout << i << ": " << dp[N][i] << endl; } void init() { rep(i, 1005) { rep(j, 1005) { dp[i][j] = -INF; } } } void DP() { for (int i = 0; i < 1005; i++) dp[0][i] = 0; for (int i = 0; i < N; i++) { for (int j = 0; j <= W; j++) { if (j >= w[i]) dp[i + 1][j] = max(dp[i][j - w[i]] + v[i], dp[i][j]); else dp[i + 1][j] = dp[i][j]; } } } int main() { int cnt = 1; while (cin >> W) { init(); if (W == 0) break; cin >> N; rep(i, N) scanf("%d,%d", &v[i], &w[i]); DP(); // DEBUG(); int ans_value = 0, ans_weight = 0; rep(i, W + 1) { if (dp[N][i] > ans_value) { ans_value = dp[N][i]; ans_weight = i; } } cout << "Case: " << cnt << endl; cout << ans_value << endl; cout << ans_weight << endl; cnt++; } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> #define INF 100000000 #define LINF 9000000000000000000 #define mod 1000000007 #define rep(i, n) for (int i = 0; i < int(n); i++) #define REP(i, a, b) for (int i = (a); i < int(b); i++) #define all(x) (x).begin(), x.end() #define pb push_back #define mp make_pair #define MOD(x) (x % (mod)) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef pair<int, int> pi; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; bool debug = false; /*---------------------------------------------------*/ int W, N; int v[1005], w[1005]; int dp[1005][1005]; void DEBUG() { rep(i, W + 1) cout << i << ": " << dp[N][i] << endl; } void init() { rep(i, 1005) { rep(j, 1005) { dp[i][j] = -INF; } } } void DP() { for (int i = 0; i < 1005; i++) dp[0][i] = 0; for (int i = 0; i < N; i++) { for (int j = 0; j <= W; j++) { if (j >= w[i]) dp[i + 1][j] = max(dp[i][j - w[i]] + v[i], dp[i][j]); else dp[i + 1][j] = dp[i][j]; } } } int main() { int cnt = 1; while (cin >> W) { init(); if (W == 0) break; cin >> N; rep(i, N) scanf("%d,%d", &v[i], &w[i]); DP(); // DEBUG(); int ans_value = 0, ans_weight = 0; rep(i, W + 1) { if (dp[N][i] > ans_value) { ans_value = dp[N][i]; ans_weight = i; } } cout << "Case " << cnt << ":" << endl; cout << ans_value << endl; cout << ans_weight << endl; cnt++; } return 0; }
[["-", 0, 16, 31, 16, 31, 16, 12, 5, 0, 6], ["+", 31, 16, 31, 16, 31, 16, 12, 5, 0, 6], ["+", 8, 9, 0, 1, 0, 16, 31, 16, 17, 151], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 62], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
1
509
#include <bits/stdc++.h> using namespace std; #define all(vec) vec.begin(), vec.end() typedef long long int ll; typedef pair<int, int> P; const ll MOD = 1234567; const ll INF = 1000000010; const ll LINF = 1000000000000000010LL; const int MAX = 10000010; const double EPS = 1e-3; int dx[4] = {0, 1, 0, 1}; int dy[4] = {0, 0, 1, 1}; int dp[1010][1010]; int main() { int W; int c = 0; while (cin >> W, W) { int n; cin >> n; int v[1010], w[1010]; memset(dp, 0, sizeof(dp)); for (int i = 0; i < n; i++) scanf("%d,%d", &v[i], &w[i]); for (int i = 0; i < n; i++) for (int j = 0; j <= W; j++) dp[i][j] = -INF; for (int i = 0; i < n; i++) dp[i][0] = 0; for (int i = 0; i < n; i++) { for (int j = 0; j <= W; j++) { dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]); if (j + w[i] <= W) { dp[i + 1][j + w[i]] = max(dp[i + 1][j + w[i]], dp[i][j] + v[i]); } } } int ans = 0; int m = 0; for (int i = 0; i <= W; i++) { if (ans < dp[n][i]) { ans = dp[n][i]; m = i; } } cout << "Case " << c << ":" << endl; cout << ans << endl; cout << m << endl; c++; } }
#include <bits/stdc++.h> using namespace std; #define all(vec) vec.begin(), vec.end() typedef long long int ll; typedef pair<int, int> P; const ll MOD = 1234567; const ll INF = 1000000010; const ll LINF = 1000000000000000010LL; const int MAX = 10000010; const double EPS = 1e-3; int dx[4] = {0, 1, 0, 1}; int dy[4] = {0, 0, 1, 1}; int dp[1010][1010]; int main() { int W; int c = 1; while (cin >> W, W) { int n; cin >> n; int v[1010], w[1010]; memset(dp, 0, sizeof(dp)); for (int i = 0; i < n; i++) scanf("%d,%d", &v[i], &w[i]); for (int i = 0; i <= n; i++) for (int j = 0; j <= W; j++) dp[i][j] = -INF; for (int i = 0; i <= n; i++) dp[i][0] = 0; for (int i = 0; i < n; i++) { for (int j = 0; j <= W; j++) { dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]); if (j + w[i] <= W) { dp[i + 1][j + w[i]] = max(dp[i + 1][j + w[i]], dp[i][j] + v[i]); } } } int ans = 0; int m = 0; for (int i = 0; i <= W; i++) { if (ans < dp[n][i]) { ans = dp[n][i]; m = i; } } cout << "Case " << c << ":" << endl; cout << ans << endl; cout << m << endl; c++; } }
[["-", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 19]]
1
453
#include <iostream> #include <vector> int main() { for (int i = 1;; i++) { int W; std::cin >> W; if (W == 0) { break; } int N; std::cin >> N; std::vector<std::vector<int>> table(N + 1, std::vector<int>(W + 1, -1)); table[0][0] = 0; for (int n = 0; n < N; n++) { int vi, wi; char foo; std::cin >> vi >> foo >> wi; for (int w = 0; w <= W; w++) { if (table[n][w] != -1) { if (table[n + 1][w] < table[n][w]) { table[n + 1][w] = table[n][w]; } if (w + wi <= W) { table[n + 1][w + wi] = table[n][w] + vi; } } } } int max_value = 0; int weight; for (int w = 0; w <= W; w++) { if (table[N][w] > max_value) { max_value = table[N][w]; weight = w; } } std::cout << "Case 1:" << std::endl; std::cout << max_value << std::endl; std::cout << weight << std::endl; } }
#include <iostream> #include <vector> int main() { for (int i = 1;; i++) { int W; std::cin >> W; if (W == 0) { break; } int N; std::cin >> N; std::vector<std::vector<int>> table(N + 1, std::vector<int>(W + 1, -1)); table[0][0] = 0; for (int n = 0; n < N; n++) { int vi, wi; char foo; std::cin >> vi >> foo >> wi; for (int w = 0; w <= W; w++) { if (table[n][w] != -1) { if (table[n + 1][w] < table[n][w]) { table[n + 1][w] = table[n][w]; } if (w + wi <= W) { table[n + 1][w + wi] = table[n][w] + vi; } } } } int max_value = 0; int weight; for (int w = 0; w <= W; w++) { if (table[N][w] > max_value) { max_value = table[N][w]; weight = w; } } std::cout << "Case " << i << ":" << std::endl; std::cout << max_value << std::endl; std::cout << weight << std::endl; } }
[["-", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 31, 16, 31, 16, 31, 16, 12, 5, 0, 6], ["+", 31, 16, 31, 16, 31, 16, 12, 5, 0, 62], ["+", 0, 1, 0, 16, 31, 16, 31, 16, 17, 151], ["+", 0, 1, 0, 16, 31, 16, 31, 16, 12, 22], ["+", 8, 9, 0, 1, 0, 16, 31, 16, 17, 151], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 62], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
1
313
#include <algorithm> #include <bitset> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define dump(n) cout << "# " << #n << "=" << (n) << endl #define debug(n) \ cout << __FILE__ << "," << __LINE__ << ": #" << #n << "=" << (n) << endl #define repi(i, a, b) for (int i = int(a); i < int(b); i++) #define rep(i, n) repi(i, 0, n) #define iter(c) __typeof((c).begin()) #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); i++) #define allof(c) (c).begin(), (c).end() #define mp make_pair typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; typedef pair<int, int> pii; int main() { for (int loop = 1, weight_max; cin >> weight_max, weight_max; loop++) { int n; cin >> n; vi value(n), weight(n); rep(i, n) scanf("%d,%d", &value[i], &weight[i]); vi dp(weight_max + 1); rep(i, n) { vi dp2(dp.size()); repi(j, weight[i], dp.size()) dp2[j] = max(dp[j], dp[j - weight[i]] + value[i]); dp = dp2; } int res_value = 0; int res_weight = 0; rep(i, dp.size()) { if (dp[i] > res_value) { res_value = dp[i]; res_weight = i; } } cout << "Case " << loop << ":" << endl; cout << res_value << endl; cout << res_weight << endl; } return 0; }
#include <algorithm> #include <bitset> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define dump(n) cout << "# " << #n << "=" << (n) << endl #define debug(n) \ cout << __FILE__ << "," << __LINE__ << ": #" << #n << "=" << (n) << endl #define repi(i, a, b) for (int i = int(a); i < int(b); i++) #define rep(i, n) repi(i, 0, n) #define iter(c) __typeof((c).begin()) #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); i++) #define allof(c) (c).begin(), (c).end() #define mp make_pair typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; typedef pair<int, int> pii; int main() { for (int loop = 1, weight_max; cin >> weight_max, weight_max; loop++) { int n; cin >> n; vi value(n), weight(n); rep(i, n) scanf("%d,%d", &value[i], &weight[i]); vi dp(weight_max + 1); rep(i, n) { vi dp2 = dp; repi(j, weight[i], dp.size()) dp2[j] = max(dp[j], dp[j - weight[i]] + value[i]); dp = dp2; } int res_value = 0; int res_weight = 0; rep(i, dp.size()) { if (dp[i] > res_value) { res_value = dp[i]; res_weight = i; } } cout << "Case " << loop << ":" << endl; cout << res_value << endl; cout << res_weight << endl; } return 0; }
[["-", 8, 9, 0, 43, 49, 50, 51, 4, 0, 24], ["+", 0, 14, 8, 9, 0, 43, 49, 50, 0, 32], ["-", 49, 50, 51, 4, 0, 2, 63, 118, 17, 131], ["-", 49, 50, 51, 4, 0, 2, 63, 118, 119, 120], ["-", 49, 50, 51, 4, 0, 2, 3, 4, 0, 24], ["-", 49, 50, 51, 4, 0, 2, 3, 4, 0, 25], ["-", 8, 9, 0, 43, 49, 50, 51, 4, 0, 25]]
1
352
#include <cstdio> #include <cstring> #include <iostream> using namespace std; int dp[1005]; int main() { int n, w, t = 1; while (scanf("%d", &w), w) { memset(dp, -1, sizeof(dp)); scanf("%d", &n); dp[0] = 0; int v, x; for (int i = 0; i < n; ++i) { scanf("%d,%d", &v, &x); for (int j = w; j >= 0; --j) { if (dp[j] == -1 || j + x >= w) continue; dp[j + x] = max(dp[j + x], dp[j] + v); } } int ans = 0, ai = 0; for (int j = 0; j <= w; ++j) { if (ans > dp[j]) ai = j; ans = max(ans, dp[j]); } printf("Case %d:\n%d\n%d\n", t++, ans, ai + 1); } }
#include <cstdio> #include <cstring> #include <iostream> using namespace std; int dp[1005]; int main() { int n, w, t = 1; while (scanf("%d", &w), w) { memset(dp, -1, sizeof(dp)); scanf("%d", &n); dp[0] = 0; int v, x; for (int i = 0; i < n; ++i) { scanf("%d,%d", &v, &x); for (int j = w; j >= 0; --j) { if (dp[j] == -1 || j + x > w) continue; dp[j + x] = max(dp[j + x], dp[j] + v); } } int ans = 0, ai = 0; for (int j = 0; j <= w; ++j) { if (ans < dp[j]) ai = j; ans = max(ans, dp[j]); } printf("Case %d:\n%d\n%d\n", t++, ans, ai); } }
[["-", 0, 57, 15, 339, 51, 16, 12, 16, 17, 20], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 17, 47], ["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 47], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 18], ["-", 0, 1, 0, 2, 3, 4, 0, 16, 17, 72], ["-", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
1
238
#include <iostream> #include <vector> using namespace std; int main() { int W, N, v, w, i, j; int cnt = 0, maxv, maxw; vector<vector<int>> dp; while (cin >> W >> N, W) { vector<vector<int>> dp(N + 2, vector<int>(W + 1)); for (i = 1; i <= N; i++) { scanf("%d,%d", &v, &w); for (j = 0; j <= W; j++) { if (j >= w && dp[i][j] < dp[i - 1][j - w] + v) dp[i][j] = dp[i - 1][j - w] + v; else dp[i][j] = dp[i - 1][j]; } } maxv = maxw = 0; for (j = 0; j <= W; j++) { if (maxv < dp[N][j]) { maxv = dp[N][j]; maxw = j; } } printf("Case %d:\n%d\n%d\n", ++cnt, maxv, maxw); } }
#include <iostream> #include <vector> using namespace std; int main() { int W, N, v, w, i, j; int cnt = 0, maxv, maxw; vector<vector<int>> dp; while (cin >> W >> N, W) { vector<vector<int>> dp(N + 2, vector<int>(W + 1)); for (i = 1; i <= N; i++) { scanf("%d,%d", &v, &w); for (j = 0; j <= W; j++) { if (j >= w && dp[i - 1][j] < dp[i - 1][j - w] + v) dp[i][j] = dp[i - 1][j - w] + v; else dp[i][j] = dp[i - 1][j]; } } maxv = maxw = 0; for (j = 0; j <= W; j++) { if (maxv < dp[N][j]) { maxv = dp[N][j]; maxw = j; } } printf("Case %d:\n%d\n%d\n", ++cnt, maxv, maxw); } }
[["+", 31, 69, 28, 69, 341, 342, 0, 16, 17, 33], ["+", 31, 69, 28, 69, 341, 342, 0, 16, 12, 13]]
1
261
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <fstream> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; const double EPS = 1e-9; typedef vector<int> vint; typedef pair<int, int> pint; #define rep(i, n) REP(i, 0, n) #define ALL(v) v.begin(), v.end() #define MSG(a) cout << #a << " " << a << endl; #define REP(i, x, n) for (int i = x; i < n; i++) template <class T> T RoundOff(T a) { return int(a + .5 - (a < 0)); } template <class T, class C> void chmax(T &a, C b) { if (a < b) a = b; } template <class T, class C> void chmin(T &a, C b) { if (b < a) a = b; } template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); } int main() { int W; for (int caseNum = 1; cin >> W && W; caseNum++) { int N; cin >> N; vint prv(W + 1, 0); rep(i, N) { vint nxt(W + 1, 0); int val, wei; char k; cin >> val >> k >> wei; rep(j, W - wei + 1) { if (j == 0 || prv[j] != 0) { chmax(nxt[j + wei], prv[j] + val); } } prv = nxt; } int val = 0, wei; rep(i, W + 1) { if (val < prv[i]) { wei = i; val = prv[i]; } } cout << "Case " << caseNum << ":\n" << val << endl << wei << endl; } }
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <fstream> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; const double EPS = 1e-9; typedef vector<int> vint; typedef pair<int, int> pint; #define rep(i, n) REP(i, 0, n) #define ALL(v) v.begin(), v.end() #define MSG(a) cout << #a << " " << a << endl; #define REP(i, x, n) for (int i = x; i < n; i++) template <class T> T RoundOff(T a) { return int(a + .5 - (a < 0)); } template <class T, class C> void chmax(T &a, C b) { if (a < b) a = b; } template <class T, class C> void chmin(T &a, C b) { if (b < a) a = b; } template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); } int main() { int W; for (int caseNum = 1; cin >> W && W; caseNum++) { int N; cin >> N; vint prv(W + 1, 0); rep(i, N) { vint nxt(prv); int val, wei; char k; cin >> val >> k >> wei; rep(j, W - wei + 1) { if (j == 0 || prv[j] != 0) { chmax(nxt[j + wei], prv[j] + val); } } prv = nxt; } int val = 0, wei; rep(i, W + 1) { if (val < prv[i]) { wei = i; val = prv[i]; } } cout << "Case " << caseNum << ":\n" << val << endl << wei << endl; } }
[["-", 0, 43, 49, 50, 51, 4, 0, 16, 31, 22], ["-", 0, 43, 49, 50, 51, 4, 0, 16, 17, 72], ["-", 0, 43, 49, 50, 51, 4, 0, 16, 12, 13], ["-", 8, 9, 0, 43, 49, 50, 51, 4, 0, 21], ["-", 8, 9, 0, 43, 49, 50, 51, 4, 0, 13], ["+", 0, 43, 49, 53, 54, 55, 0, 56, 39, 78]]
1
414
#include <iostream> using namespace std; #define SAFE_DELETE(x) \ { \ if (x) { \ delete x; \ x = NULL; \ } \ } struct tCell { tCell() : value(0), weight(0), unitV(0.0), prev(NULL), next(NULL) {} int value, weight; double unitV; tCell *prev, *next; }; class MyArray { public: MyArray() : mSize(0), head(NULL), tail(NULL) { head = new tCell; tail = new tCell; head->next = tail; tail->prev = head; } ~MyArray() { init(); SAFE_DELETE(tail); SAFE_DELETE(head); } void init() { for (int i = mSize; i > 0; --i) { tCell *cell = tail->prev; cell->prev->next = cell->next; cell->next->prev = cell->prev; SAFE_DELETE(cell); } mSize = 0; } void pushBack(int value, int weight) { ++mSize; tCell *cell = new tCell; cell->value = value; cell->weight = weight; cell->unitV = static_cast<double>(value) / weight; cell->prev = tail->prev; cell->next = tail; cell->prev->next = cell; cell->next->prev = cell; } int size() { return mSize; } tCell operator[](int index) { tCell *cell = head->next; for (int i = 0; i < index; ++i) { cell = cell->next; } return *cell; } private: int mSize; tCell *head, *tail; }; int main() { MyArray treList; int time = 1, c; int v = 0, w = 0; char d; while (cin >> c) { if (!c) break; int n; treList.init(); cin >> n; for (int i = 0; i < n; ++i) { cin >> v >> d >> w; treList.pushBack(v, w); } int maxV = 0, maxW = 0; int *table = new int[(n + 1) * (c + 1)]; for (int i = 0; i < n + 1; ++i) { if (i != 0) { v = treList[i - 1].value; w = treList[i - 1].weight; } for (int j = 0; j < c + 1; ++j) { if (i == 0 || j == 0) { table[i * (c + 1) + j] = 0; continue; } table[i * (c + 1) + j] = table[(i - 1) * (c + 1) + j]; if ((j - w >= 0) && (table[(i - 1) * (c + 1) + (j - w)] + v > table[(i - 1) * (c + 1) + j])) { table[i * (c + 1) + j] = table[(i - 1) * (c + 1) + (j - w)] + v; if (table[i * (c + 1) + j] > maxV) { maxV = table[i * (c + 1) + j]; maxW = j; } else if (table[i * (c + 1) + j] == maxV && i < maxW) { maxW = j; } } } } cout << "Case " << time << ":" << endl; cout << maxV << endl << maxW << endl; ++time; delete[] table; } return 0; }
#include <iostream> using namespace std; #define SAFE_DELETE(x) \ { \ if (x) { \ delete x; \ x = NULL; \ } \ } struct tCell { tCell() : value(0), weight(0), unitV(0.0), prev(NULL), next(NULL) {} int value, weight; double unitV; tCell *prev, *next; }; class MyArray { public: MyArray() : mSize(0), head(NULL), tail(NULL) { head = new tCell; tail = new tCell; head->next = tail; tail->prev = head; } ~MyArray() { init(); SAFE_DELETE(tail); SAFE_DELETE(head); } void init() { for (int i = mSize; i > 0; --i) { tCell *cell = tail->prev; cell->prev->next = cell->next; cell->next->prev = cell->prev; SAFE_DELETE(cell); } mSize = 0; } void pushBack(int value, int weight) { ++mSize; tCell *cell = new tCell; cell->value = value; cell->weight = weight; cell->unitV = static_cast<double>(value) / weight; cell->prev = tail->prev; cell->next = tail; cell->prev->next = cell; cell->next->prev = cell; } int size() { return mSize; } tCell operator[](int index) { tCell *cell = head->next; for (int i = 0; i < index; ++i) { cell = cell->next; } return *cell; } private: int mSize; tCell *head, *tail; }; int main() { MyArray treList; int time = 1, c; int v = 0, w = 0; char d; while (cin >> c) { if (!c) break; int n; treList.init(); cin >> n; for (int i = 0; i < n; ++i) { cin >> v >> d >> w; treList.pushBack(v, w); } int maxV = 0, maxW = 0; int *table = new int[(n + 1) * (c + 1)]; for (int i = 0; i < n + 1; ++i) { if (i != 0) { v = treList[i - 1].value; w = treList[i - 1].weight; } for (int j = 0; j < c + 1; ++j) { if (i == 0 || j == 0) { table[i * (c + 1) + j] = 0; continue; } table[i * (c + 1) + j] = table[(i - 1) * (c + 1) + j]; if ((j - w >= 0) && (table[(i - 1) * (c + 1) + (j - w)] + v > table[(i - 1) * (c + 1) + j])) { table[i * (c + 1) + j] = table[(i - 1) * (c + 1) + (j - w)] + v; if (table[i * (c + 1) + j] > maxV) { maxV = table[i * (c + 1) + j]; maxW = j; } else if (table[i * (c + 1) + j] == maxV && j < maxW) { maxW = j; } } } } cout << "Case " << time << ":" << endl; cout << maxV << endl << maxW << endl; ++time; delete[] table; } return 0; }
[["-", 0, 57, 15, 339, 51, 16, 12, 16, 31, 22], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 31, 22]]
1
766
#include <algorithm> #include <cstring> #include <iostream> #include <queue> using namespace std; typedef pair<int, int> P; typedef pair<P, int> status; int main() { int W, N, cnt = 1; while (true) { cin >> W; if (W == 0) return 0; cin >> N; int val[1001], weight[1001]; memset(val, 0, sizeof(val)); memset(weight, 0, sizeof(weight)); for (int i = 0; i < N; i++) { cin >> val[i]; cin.get(); cin >> weight[i]; } int dp[1003]; memset(dp, 0, sizeof(dp)); for (int i = 0; i < N; i++) { for (int j = W; j >= weight[i]; j--) { if (j - weight[i] < 0) continue; dp[j] = max(dp[j], dp[j - weight[i]] + val[i]); } } int mini; for (int i = W; dp[i] == dp[W]; i--) mini = i; cout << "Case " << cnt << ":" << endl; cout << dp[W - 1] << endl; cout << mini << endl; cnt++; } return 0; }
#include <algorithm> #include <cstring> #include <iostream> #include <queue> using namespace std; typedef pair<int, int> P; typedef pair<P, int> status; int main() { int W, N, cnt = 1; while (true) { cin >> W; if (W == 0) return 0; cin >> N; int val[1001], weight[1001]; memset(val, 0, sizeof(val)); memset(weight, 0, sizeof(weight)); for (int i = 0; i < N; i++) { cin >> val[i]; cin.get(); cin >> weight[i]; } int dp[1003]; memset(dp, 0, sizeof(dp)); for (int i = 0; i < N; i++) { for (int j = W; j >= weight[i]; j--) { if (j - weight[i] < 0) continue; dp[j] = max(dp[j], dp[j - weight[i]] + val[i]); } } int mini; for (int i = W; dp[i] == dp[W]; i--) mini = i; cout << "Case " << cnt << ":" << endl; cout << dp[W] << endl; cout << mini << endl; cnt++; } return 0; }
[["-", 31, 16, 12, 69, 341, 342, 0, 16, 17, 33], ["-", 31, 16, 12, 69, 341, 342, 0, 16, 12, 13]]
1
297
#include <algorithm> #include <iostream> using namespace std; int main(void) { int map[1001], save[1001]; int w, count; count = 1; while (cin >> w, w) { ++w; for (int i = 0; i < w; ++i) map[i] = save[i] = 0; int n, value, weight; cin >> n; scanf("%d,%d", &value, &weight); save[weight] = map[weight] = max(save[weight], value); for (int i = 1; i < n; ++i) { scanf("%d,%d", &value, &weight); save[weight] = max(save[weight], value); for (int j = 0; j < w; ++j) { if (map[j]) { int ww = j + weight; if (ww < w) save[ww] = max(save[ww], map[j] + value); else save[j] = max(save[j], map[j]); } } for (int j = 0; j < w; ++j) { map[j] = save[j]; save[j] = 0; } } int Max = 0, idx; for (int i = 0; i < w; ++i) { if (Max < map[i]) { Max = map[i]; idx = i; } } cout << "Case " << count++ << ':' << endl; cout << Max << endl; cout << idx << endl; } return 0; }
#include <algorithm> #include <iostream> using namespace std; int main(void) { int map[1001], save[1001]; int w; long long count; count = 1; while (cin >> w, w) { ++w; for (int i = 0; i < w; ++i) map[i] = save[i] = 0; int n, value, weight; cin >> n; scanf("%d,%d", &value, &weight); save[weight] = map[weight] = max(save[weight], value); for (int i = 1; i < n; ++i) { scanf("%d,%d", &value, &weight); save[weight] = max(save[weight], value); for (int j = 0; j < w; ++j) { if (map[j]) { int ww = j + weight; if (ww < w) save[ww] = max(save[ww], map[j] + value); save[j] = max(save[j], map[j]); } } for (int j = 0; j < w; ++j) { map[j] = save[j]; save[j] = 0; } } int Max = 0, idx; for (int i = 1; i < w; ++i) { if (Max < map[i]) { Max = map[i]; idx = i; } } cout << "Case " << count++ << ':' << endl; cout << Max << endl; cout << idx << endl; } return 0; }
[["-", 0, 30, 0, 14, 8, 9, 0, 43, 0, 21], ["+", 0, 30, 0, 14, 8, 9, 0, 43, 0, 35], ["+", 0, 14, 8, 9, 0, 43, 39, 86, 0, 96], ["-", 0, 57, 64, 9, 0, 57, 75, 76, 0, 95], ["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13]]
1
347
#include <algorithm> #include <bitset> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef unsigned int UI; typedef long long LL; typedef unsigned long long ULL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef pair<int, int> PII; typedef istringstream ISS; typedef ostringstream OSS; #define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i) #define EACH(v, c) for (auto &v : c) #define ITER(c) __typeof((c).begin()) #define IREP(c, it) for (ITER(c) it = c.begin(); it != c.end(); ++it) #define ALL(c) (c).begin(), (c).end() #define PB(n) push_back(n) #define MP(a, b) make_pair((a), (b)) #define EXIST(c, e) ((c).find(e) != (c).end()) #define fst first #define snd second #define DUMP(x) cerr << #x << " = " << (x) << endl #define DEBUG(x) \ cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << (x) << endl int main() { cin.tie(0); ios::sync_with_stdio(false); int casenum = 1; while (true) { int w; cin >> w; if (!w) { break; } int n; cin >> n; vector<PII> tres(n); EACH(tre, tres) { char dummy; cin >> tre.fst >> dummy >> tre.snd; } VVI dp(n + 1, VI(w + 1, 0)); REP(i, 0, n) { REP(j, 0, w) { dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]); if (j + tres[i].snd <= w) { dp[i + 1][j + tres[i].snd] = max(dp[i + 1][j + tres[i].snd], dp[i][j] + tres[i].fst); } } } int resnum = -1, resmax = 0; REP(i, 0, w + 1) { if (resmax < dp[n][i]) { resnum = i; resmax = dp[n][i]; } } cout << "Case " << casenum++ << ':' << endl; cout << resmax << endl; cout << resnum << endl; } return 0; }
#include <algorithm> #include <bitset> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef unsigned int UI; typedef long long LL; typedef unsigned long long ULL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef pair<int, int> PII; typedef istringstream ISS; typedef ostringstream OSS; #define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i) #define EACH(v, c) for (auto &v : c) #define ITER(c) __typeof((c).begin()) #define IREP(c, it) for (ITER(c) it = c.begin(); it != c.end(); ++it) #define ALL(c) (c).begin(), (c).end() #define PB(n) push_back(n) #define MP(a, b) make_pair((a), (b)) #define EXIST(c, e) ((c).find(e) != (c).end()) #define fst first #define snd second #define DUMP(x) cerr << #x << " = " << (x) << endl #define DEBUG(x) \ cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << (x) << endl int main() { cin.tie(0); ios::sync_with_stdio(false); int casenum = 1; while (true) { int w; cin >> w; if (!w) { break; } int n; cin >> n; vector<PII> tres(n); EACH(tre, tres) { char dummy; cin >> tre.fst >> dummy >> tre.snd; } VVI dp(n + 1, VI(w + 1, 0)); REP(i, 0, n) { REP(j, 0, w + 1) { dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]); if (j + tres[i].snd <= w) { dp[i + 1][j + tres[i].snd] = max(dp[i + 1][j + tres[i].snd], dp[i][j] + tres[i].fst); } } } int resnum = -1, resmax = 0; REP(i, 0, w + 1) { if (resmax < dp[n][i]) { resnum = i; resmax = dp[n][i]; } } cout << "Case " << casenum++ << ':' << endl; cout << resmax << endl; cout << resnum << endl; } return 0; }
[["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 72], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
1
480
#include <cstdio> #include <iostream> using namespace std; int main(void) { int W, n, v[1000], w[1000], dp[10000][10000], t = 1; while (cin >> W, W) { cin >> n; for (int i = 0; i < n; i++) scanf("%d,%d", &v[i], &w[i]); for (int i = 0; i < 1000; i++) for (int j = 0; j < 1000; j++) dp[i][j] = -1; for (int i = 0; i < 1000; i++) { dp[0][i] = 0; } for (int i = 0; i < n; i++) for (int j = 0; j <= W; j++) if (j < w[i]) dp[i + 1][j] = dp[i][j]; else dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]); cout << "Case " << t << ":" << endl; for (int i = W; i >= 0; i--) { if (dp[n][i] != dp[n][W]) { cout << dp[n][W] << endl << i + 1 << endl; break; } } t++; } return 0; }
#include <cstdio> #include <iostream> using namespace std; int main(void) { int W, n, v[1000], w[1000], dp[1001][1001], t = 1; while (cin >> W, W) { cin >> n; for (int i = 0; i < n; i++) scanf("%d,%d", &v[i], &w[i]); for (int i = 0; i < 1000; i++) for (int j = 0; j < 1000; j++) dp[i][j] = -1; for (int i = 0; i < 1000; i++) { dp[0][i] = 0; } for (int i = 0; i < n; i++) for (int j = 0; j <= W; j++) if (j < w[i]) dp[i + 1][j] = dp[i][j]; else dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]); cout << "Case " << t << ":" << endl; for (int i = W; i >= 0; i--) { if (dp[n][i] != dp[n][W]) { cout << dp[n][W] << endl << i + 1 << endl; break; } } t++; } return 0; }
[["-", 8, 9, 0, 43, 49, 80, 49, 80, 81, 13], ["+", 8, 9, 0, 43, 49, 80, 49, 80, 81, 13], ["-", 0, 14, 8, 9, 0, 43, 49, 80, 81, 13], ["+", 0, 14, 8, 9, 0, 43, 49, 80, 81, 13]]
1
324
#include <algorithm> #include <cstring> #include <iostream> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) const int maxw = 1001; const int maxn = 1001; const int maxp = 10001; int dp[maxn][maxw] = {}; int w[maxn] = {}; int p[maxn] = {}; int W, N; int main() { int casenum = 0; while (cin >> W) { if (W == 0) break; casenum++; cout << "Case " << casenum << ":" << endl; cin >> N; char t; rep(i, N) cin >> p[i] >> t >> w[i]; memset(dp, 0, sizeof(dp)); rep(i, N) rep(j, W + 1) { if (j - w[i] >= 0) dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + p[i]); else dp[i + 1][j] = dp[i][j]; } int ansp = 0, answ = 0; for (int j = W; j != 0; j--) if (ansp < dp[N][j]) { ansp = dp[N][j]; answ = j; } cout << ansp << endl; cout << answ - 1 << endl; } return 0; }
#include <algorithm> #include <cstring> #include <iostream> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) const int maxw = 1001; const int maxn = 1001; const int maxp = 10001; int dp[maxn][maxw] = {}; int w[maxn] = {}; int p[maxn] = {}; int W, N; int main() { int casenum = 0; while (cin >> W) { if (W == 0) break; casenum++; cout << "Case " << casenum << ":" << endl; cin >> N; char t; rep(i, N) cin >> p[i] >> t >> w[i]; memset(dp, 0, sizeof(dp)); rep(i, N) rep(j, W + 1) { if (j - w[i] >= 0) dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + p[i]); else dp[i + 1][j] = dp[i][j]; } int ansp = 0, answ = 0; for (int j = W; j != 0; j--) if (ansp <= dp[N][j]) { ansp = dp[N][j]; answ = j; } cout << ansp << endl; cout << answ << endl; } return 0; }
[["-", 0, 7, 8, 57, 15, 339, 51, 16, 17, 18], ["+", 0, 7, 8, 57, 15, 339, 51, 16, 17, 19], ["-", 0, 1, 0, 16, 31, 16, 12, 16, 17, 33], ["-", 0, 1, 0, 16, 31, 16, 12, 16, 12, 13]]
1
310
#include <algorithm> #include <cstring> #include <iostream> using namespace std; int W, N, casenum = 0, ans, wmin; int v[1000], w[1000]; int dp[1001][1001]; int rec_dp(int, int); void solve(); int main() { int i; char c; while (1) { cin >> W >> N; if (W == 0) break; casenum++; for (i = 0; i < N; i++) cin >> v[i] >> c >> w[i]; memset(dp, -1, sizeof(dp)); ans = rec_dp(0, W); for (i = 0; i < W; i++) { if (rec_dp(0, i) == ans) { wmin = i; break; } } solve(); } return 0; } int rec_dp(int i, int j) { if (dp[i][j] != -1) return dp[i][j]; int res; if (i == N) res = 0; else if (j < w[i]) res = rec_dp(i + 1, j); else res = max(rec_dp(i + 1, j), rec_dp(i + 1, j - w[i]) + v[i]); return dp[i][j] = res; } void solve() { cout << "Case" << " " << casenum << ":" << endl; cout << ans << endl; cout << wmin << endl; }
#include <algorithm> #include <cstring> #include <iostream> using namespace std; int W, N, casenum = 0, ans, wmin; int v[1000], w[1000]; int dp[1001][1001]; int rec_dp(int, int); void solve(); int main() { int i; char c; while (1) { cin >> W >> N; if (W == 0) break; casenum++; for (i = 0; i < N; i++) cin >> v[i] >> c >> w[i]; memset(dp, -1, sizeof(dp)); ans = rec_dp(0, W); for (i = 0; i <= W; i++) { if (rec_dp(0, i) == ans) { wmin = i; break; } } solve(); } return 0; } int rec_dp(int i, int j) { if (dp[i][j] != -1) return dp[i][j]; int res; if (i == N) res = 0; else if (j < w[i]) res = rec_dp(i + 1, j); else res = max(rec_dp(i + 1, j), rec_dp(i + 1, j - w[i]) + v[i]); return dp[i][j] = res; } void solve() { cout << "Case" << " " << casenum << ":" << endl; cout << ans << endl; cout << wmin << endl; }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 19]]
1
327
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <numeric> #include <queue> #include <set> #include <string> #include <utility> #include <vector> using namespace std; template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } int W, N; int c[1000], w[1000]; int t[10000 + 10]; int main() { for (int i = 1;; i++) { cin >> W; if (W == 0) break; cin >> N; for (int j = 0; j < N; j++) scanf("%d,%d", c + j, w + j); printf("Case%d:\n", i); memset(t, -1, sizeof(t)); t[0] = 0; for (int i = 0; i < N; i++) { for (int j = W - w[i]; j >= 0; j--) { if (t[j] == -1) continue; t[j + w[i]] = max(t[j + w[i]], t[j] + c[i]); } } int ans_id = max_element(t, t + W + 1) - t; printf("%d\n%d\n", t[ans_id], ans_id); } }
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <numeric> #include <queue> #include <set> #include <string> #include <utility> #include <vector> using namespace std; template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } int W, N; int c[1000], w[1000]; int t[10000 + 10]; int main() { for (int i = 1;; i++) { cin >> W; if (W == 0) break; cin >> N; for (int j = 0; j < N; j++) scanf("%d,%d", c + j, w + j); printf("Case %d:\n", i); memset(t, -1, sizeof(t)); t[0] = 0; for (int i = 0; i < N; i++) { for (int j = W - w[i]; j >= 0; j--) { if (t[j] == -1) continue; t[j + w[i]] = max(t[j + w[i]], t[j] + c[i]); } } int ans_id = max_element(t, t + W + 1) - t; printf("%d\n%d\n", t[ans_id], ans_id); } }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
285
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; int main() { int w, n, tre[1001][2], dp[1001][1001] = {0}, x; for (int c = 1; cin >> w, w; c++) { cin >> n; for (int i = 1; i <= n; i++) { scanf("%d,%d", &tre[i][0], &tre[i][1]); } for (int i = 1; i <= n; i++) { for (int j = 1; j <= w; j++) { dp[i][j] = dp[i - 1][j]; if (j >= tre[i][1]) { dp[i][j] = max(dp[i][j], dp[i - 1][j - tre[i][1]] + tre[i][0]); } } } for (int j = 0; j < w; j++) { if (dp[n][j] == dp[n][w]) { x = j; break; } } cout << "Case " << c << ":" << endl << dp[n][w] << endl << x << endl; } return 0; }
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; int main() { int w, n, tre[1001][2], dp[1001][1001] = {0}, x; for (int c = 1; cin >> w, w; c++) { cin >> n; for (int i = 1; i <= n; i++) { scanf("%d,%d", &tre[i][0], &tre[i][1]); } for (int i = 1; i <= n; i++) { for (int j = 1; j <= w; j++) { dp[i][j] = dp[i - 1][j]; if (j >= tre[i][1]) { dp[i][j] = max(dp[i][j], dp[i - 1][j - tre[i][1]] + tre[i][0]); } } } for (int j = 0; j <= w; j++) { if (dp[n][j] == dp[n][w]) { x = j; break; } } cout << "Case " << c << ":" << endl << dp[n][w] << endl << x << endl; } return 0; }
[["-", 0, 7, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 17, 19]]
1
288
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; int tr[1100][2] = {0}, dp[1100][1100] = {0}; int main() { for (int c = 1;; c++) { int w, n, ans = 0, q; cin >> w; if (w == 0) break; cin >> n; for (int i = 0; i < 1100; i++) { tr[i][0] = tr[i][1] = 0; for (int j = 0; j < 1100; j++) dp[i][j] = 0; } for (int i = 1; i <= n; i++) scanf("%d,%d", &tr[i][0], &tr[i][1]); for (int i = 1; i <= n; i++) { for (int j = 0; j <= w; j++) { if (j == 0) dp[i][tr[i][1]] = max(dp[i][tr[i][1]], tr[i][0]); if (dp[i - 1][j] != 0) { if (j + tr[i][1] <= w) dp[i][j + tr[i][1]] = max(dp[i][j + tr[i][1]], dp[i - 1][j] + tr[i][0]); else dp[i][j] = max(dp[i][j], dp[i - 1][j]); } } } for (int i = 0; i <= w; i++) { if (ans < dp[n][i]) { ans = dp[n][i]; q = i; } } cout << "Case " << c << ":" << endl << ans << endl << q << endl; } return 0; }
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; int tr[1100][2] = {0}, dp[1100][1100] = {0}; int main() { for (int c = 1;; c++) { int w, n, ans = 0, q; cin >> w; if (w == 0) break; cin >> n; for (int i = 0; i < 1100; i++) { tr[i][0] = tr[i][1] = 0; for (int j = 0; j < 1100; j++) dp[i][j] = 0; } for (int i = 1; i <= n; i++) scanf("%d,%d", &tr[i][0], &tr[i][1]); for (int i = 1; i <= n; i++) { for (int j = 0; j <= w; j++) { if (j == 0) dp[i][tr[i][1]] = max(dp[i][tr[i][1]], tr[i][0]); if (dp[i - 1][j] != 0) { if (j + tr[i][1] <= w) dp[i][j + tr[i][1]] = max(dp[i][j + tr[i][1]], dp[i - 1][j] + tr[i][0]); dp[i][j] = max(dp[i][j], dp[i - 1][j]); } } } for (int i = 0; i <= w; i++) { if (ans < dp[n][i]) { ans = dp[n][i]; q = i; } } cout << "Case " << c << ":" << endl << ans << endl << q << endl; } return 0; }
[["-", 0, 57, 64, 9, 0, 57, 75, 76, 0, 95]]
1
437
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> using namespace std; int main() { int W, N, cs = 0; int cost[1001], weight[1001]; int dp[1001]; while (cin >> W, W) { cin >> N; for (int i = 0; i < N; i++) { scanf("%d,%d", &cost[i], &weight[i]); } fill(dp, dp + 1001, -1); dp[0] = 0; for (int i = 0; i < N; i++) { for (int j = W - weight[i]; j >= 0; j--) { if (dp[j] >= 0) dp[j + weight[i]] = max(dp[j + weight[i]], dp[j] + cost[i]); } } int Cost = 0, Weight = 0; for (int i = 0; i < W; i++) { if (dp[i] > Weight) { Cost = dp[i]; Weight = i; } } printf("Case %d:\n%d\n%d\n", ++cs, Cost, Weight); } return 0; }
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> using namespace std; int main() { int W, N, cs = 0; int cost[1001], weight[1001]; int dp[1001]; while (cin >> W, W) { cin >> N; for (int i = 0; i < N; i++) { scanf("%d,%d", &cost[i], &weight[i]); } fill(dp, dp + 1001, -1); dp[0] = 0; for (int i = 0; i < N; i++) { for (int j = W - weight[i]; j >= 0; j--) { if (dp[j] >= 0) dp[j + weight[i]] = max(dp[j + weight[i]], dp[j] + cost[i]); } } int Cost = 0, Weight = 0; for (int i = 0; i <= W; i++) { if (dp[i] > Cost) { Cost = dp[i]; Weight = i; } } printf("Case %d:\n%d\n%d\n", ++cs, Cost, Weight); } return 0; }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 19], ["-", 8, 9, 0, 57, 15, 339, 51, 16, 12, 22], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 12, 22]]
1
259
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) int dp[1001][1001]; int main() { int W, cnt = 1; while (cin >> W, W) { int n, v[1000], w[1000]; memset(dp, -1, sizeof(dp)); cin >> n; rep(i, n) { scanf("%d,%d", &v[i], &w[i]); } dp[0][0] = 0; rep(i, n) { rep(j, W + 1) { if (j < w[i]) dp[i + 1][j] = dp[i][j]; else dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]); } } int mw = 0; rep(i, W) { if (dp[n][i] > dp[n][mw]) mw = i; } cout << "Case " << cnt << ":" << endl; cnt++; cout << dp[n][mw] << endl; cout << mw << endl; } }
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) int dp[1001][1001]; int main() { int W, cnt = 1; while (cin >> W, W) { int n, v[1000], w[1000]; memset(dp, -1, sizeof(dp)); cin >> n; rep(i, n) { scanf("%d,%d", &v[i], &w[i]); } dp[0][0] = 0; rep(i, n) { rep(j, W + 1) { if (j < w[i]) dp[i + 1][j] = dp[i][j]; else dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]); } } int mw = 0; rep(i, W + 1) { if (dp[n][i] > dp[n][mw]) mw = i; } cout << "Case " << cnt << ":" << endl; cnt++; cout << dp[n][mw] << endl; cout << mw << endl; } }
[["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 72], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13], ["+", 0, 1, 0, 2, 3, 4, 0, 25, 0, 35]]
1
274
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; int main() { int n, W; char dummy; int count = 1; while (true) { cin >> W; if (W == 0) { break; } cin >> n; vector<int> item_val(n); vector<int> item_wei(n); vector<vector<int>> nap(n + 1, vector<int>(W + 1, 0)); for (int i = 0; i < n; i++) { cin >> item_val[i] >> dummy >> item_wei[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j <= W; j++) { if (j + item_wei[i] <= W) { nap[i + 1][j + item_wei[i]] = max(nap[i + 1][j + item_wei[i]], nap[i][j] + item_val[i]); } nap[i + 1][j] = max(nap[i + 1][j], nap[i][j]); } } int max_weight = W; int max_value = 0; for (int j = 0; j <= W; j++) { if (max_value < nap[n][j]) { max_value = nap[n][j]; max_weight = j; } } cout << "Case :" << count << endl; cout << max_value << "\n" << max_weight << endl; count++; } }
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; int main() { int n, W; char dummy; int count = 1; while (true) { cin >> W; if (W == 0) { break; } cin >> n; vector<int> item_val(n); vector<int> item_wei(n); vector<vector<int>> nap(n + 1, vector<int>(W + 1, 0)); for (int i = 0; i < n; i++) { cin >> item_val[i] >> dummy >> item_wei[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j <= W; j++) { if (j + item_wei[i] <= W) { nap[i + 1][j + item_wei[i]] = max(nap[i + 1][j + item_wei[i]], nap[i][j] + item_val[i]); } nap[i + 1][j] = max(nap[i + 1][j], nap[i][j]); } } int max_weight = W; int max_value = 0; for (int j = 0; j <= W; j++) { if (max_value < nap[n][j]) { max_value = nap[n][j]; max_weight = j; } } cout << "Case " << count << ":" << endl; cout << max_value << "\n" << max_weight << endl; count++; } }
[["-", 0, 16, 31, 16, 31, 16, 12, 5, 0, 6], ["+", 31, 16, 31, 16, 31, 16, 12, 5, 0, 6], ["+", 8, 9, 0, 1, 0, 16, 31, 16, 17, 151], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 62], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
1
330
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #define ll long long using namespace std; int dp[1111][1111]; int w[1111], v[1111]; int main() { int W, N; int c = 1; while (1) { cin >> W; if (!W) break; cin >> N; for (int i = 0; i < N; i++) { scanf("%d,%d", &v[i], &w[i]); } memset(dp, 0, sizeof(dp)); for (int i = 0; i < N; i++) { for (int j = 0; j <= W; j++) { if (j < w[i]) { dp[i + 1][j] = dp[i][j]; } else { dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]); } } } int ret = 0, value = 0; for (int i = 0; i <= W; i++) { if (value < dp[N][i]) { ret = i; value = dp[N][i]; } } cout << "Case" << c++ << ":" << endl; cout << dp[N][W] << endl; cout << ret << endl; } }
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #define ll long long using namespace std; int dp[1111][1111]; int w[1111], v[1111]; int main() { int W, N; int c = 1; while (1) { cin >> W; if (!W) break; cin >> N; for (int i = 0; i < N; i++) { scanf("%d,%d", &v[i], &w[i]); } memset(dp, 0, sizeof(dp)); for (int i = 0; i < N; i++) { for (int j = 0; j <= W; j++) { if (j < w[i]) { dp[i + 1][j] = dp[i][j]; } else { dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]); } } } int ret = 0, value = 0; for (int i = 0; i <= W; i++) { if (value < dp[N][i]) { ret = i; value = dp[N][i]; } } cout << "Case " << c++ << ":" << endl; cout << dp[N][W] << endl; cout << ret << endl; } }
[["-", 31, 16, 31, 16, 31, 16, 12, 5, 0, 6], ["+", 31, 16, 31, 16, 31, 16, 12, 5, 0, 6]]
1
308
#include <algorithm> #include <iostream> using namespace std; const int NUM_MAX = 1001; int dp[NUM_MAX][NUM_MAX]; int we[NUM_MAX], v[NUM_MAX]; int w, n; void solve() { int max_idx; for (int i = 0; i < n; i++) { for (int j = 0; j <= w; j++) { if (j < we[i]) dp[i + 1][j] = dp[i][j]; else dp[i + 1][j] = max(dp[i][j], dp[i][j - we[i]] + v[i]); } } max_idx = 0; for (int j = 0; j <= w; j++) if (dp[n][j] == dp[n][w]) max_idx = j; cout << dp[n][w] << '\n' << max_idx << endl; } int main() { int cas = 1; while (cin >> w, w) { char ch; cin >> n; for (int i = 0; i < n; i++) cin >> v[i] >> ch >> we[i]; for (int i = 0; i <= n; i++) for (int j = 0; j <= w; j++) dp[i][j] = 0; cout << "Case " << cas++ << ":\n"; solve(); } return 0; }
#include <algorithm> #include <iostream> using namespace std; const int NUM_MAX = 1001; int dp[NUM_MAX][NUM_MAX]; int we[NUM_MAX], v[NUM_MAX]; int w, n; void solve() { int max_idx; for (int i = 0; i < n; i++) { for (int j = 0; j <= w; j++) { if (j < we[i]) dp[i + 1][j] = dp[i][j]; else dp[i + 1][j] = max(dp[i][j], dp[i][j - we[i]] + v[i]); } } max_idx = 0; for (int j = 0; j <= w; j++) if (dp[n][j] == dp[n][w]) { max_idx = j; break; } cout << dp[n][w] << '\n' << max_idx << endl; } int main() { int cas = 1; while (cin >> w, w) { char ch; cin >> n; for (int i = 0; i < n; i++) cin >> v[i] >> ch >> we[i]; for (int i = 0; i <= n; i++) for (int j = 0; j <= w; j++) dp[i][j] = 0; cout << "Case " << cas++ << ":\n"; solve(); } return 0; }
[["+", 8, 9, 0, 7, 8, 57, 64, 9, 0, 45], ["+", 0, 7, 8, 57, 64, 9, 0, 1, 0, 35], ["+", 0, 7, 8, 57, 64, 9, 0, 93, 0, 94], ["+", 8, 9, 0, 7, 8, 57, 64, 9, 0, 46]]
1
320
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < n; i++) #define ALL(T) T.begin(), T.end() #define mp make_pair #define pb push_back using namespace std; typedef pair<int, int> pii; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<string> vs; int main(void) { for (int w, n, cnt = 0; cin >> w >> n, w;) { vi dp(w + 1); fill(ALL(dp), -1); dp[0] = 0; char c; while (n--) { int tv, tw; cin >> tv >> c >> tw; for (int i = w; 0 <= i; i--) if (i + tw <= w && dp[i] >= 0) dp[i + tw] = max(dp[i + tw], dp[i] + tv); } vi::iterator it = max_element(ALL(dp)); cout << "Case " << ++cnt << endl << *it << endl << it - dp.begin() << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < n; i++) #define ALL(T) T.begin(), T.end() #define mp make_pair #define pb push_back using namespace std; typedef pair<int, int> pii; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<string> vs; int main(void) { for (int w, n, cnt = 0; cin >> w >> n, w;) { vi dp(w + 1); fill(ALL(dp), -1); dp[0] = 0; char c; while (n--) { int tv, tw; cin >> tv >> c >> tw; for (int i = w; 0 <= i; i--) if (i + tw <= w && dp[i] >= 0) dp[i + tw] = max(dp[i + tw], dp[i] + tv); } vi::iterator it = max_element(ALL(dp)); cout << "Case " << ++cnt << ":" << endl << *it << endl << it - dp.begin() << endl; } return 0; }
[["+", 31, 16, 31, 16, 31, 16, 12, 5, 0, 62], ["+", 31, 16, 31, 16, 31, 16, 12, 5, 0, 6], ["+", 31, 16, 31, 16, 31, 16, 31, 16, 17, 151]]
1
247
#include <algorithm> #include <iostream> #include <math.h> #include <string> #include <utility> #include <vector> using namespace std; int main() { int n, w; for (int num = 1;; num++) { cin >> w; if (w == 0) break; cin >> n; int input1, input2; char input3; vector<int> value; vector<int> weight; for (int i = 0; i < n; i++) { cin >> input1 >> input3 >> input2; value.push_back(input1); weight.push_back(input2); } vector<int> check; check.push_back(0); for (int i = 0; i < w - 1; i++) { check.push_back(-1); } int value_sum = 0; int weight_sum = w + 1; for (int i = 0; i < n; i++) { for (int j = w - weight[i] - 1; j >= 0; j--) { if (check[j] != -1) { check[j + weight[i]] = max(check[j + weight[i]], check[j] + value[i]); if (check[j + weight[i]] > value_sum) { value_sum = check[j + weight[i]]; weight_sum = j + weight[i]; } else if (check[j + weight[i]] == value_sum) { weight_sum = min(weight_sum, j + weight[i]); } } } } cout << "Case " << num << ":" << endl << value_sum << endl << weight_sum << endl; } return 0; }
#include <algorithm> #include <iostream> #include <math.h> #include <string> #include <utility> #include <vector> using namespace std; int main() { int n, w; for (int num = 1;; num++) { cin >> w; if (w == 0) break; cin >> n; int input1, input2; char input3; vector<int> value; vector<int> weight; for (int i = 0; i < n; i++) { cin >> input1 >> input3 >> input2; value.push_back(input1); weight.push_back(input2); } vector<int> check; check.push_back(0); for (int i = 0; i < w; i++) { check.push_back(-1); } int value_sum = 0; int weight_sum = w + 1; for (int i = 0; i < n; i++) { for (int j = w - weight[i]; j >= 0; j--) { if (check[j] != -1) { check[j + weight[i]] = max(check[j + weight[i]], check[j] + value[i]); if (check[j + weight[i]] > value_sum) { value_sum = check[j + weight[i]]; weight_sum = j + weight[i]; } else if (check[j + weight[i]] == value_sum) { weight_sum = min(weight_sum, j + weight[i]); } } } } cout << "Case " << num << ":" << endl << value_sum << endl << weight_sum << endl; } return 0; }
[["-", 8, 9, 0, 7, 15, 16, 12, 16, 17, 33], ["-", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13], ["-", 0, 7, 10, 43, 49, 50, 51, 16, 17, 33], ["-", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13]]
1
340
#include <iostream> #include <stdio.h> using namespace std; int main(void) { int c = 1, W; while (cin >> W, W) { int n, mx = 0, w[1000], d[1000]; long dp[1001] = {0}; cin >> n; for (int i = 0; i < n; i++) scanf("%d,%d", &d[i], &w[i]); for (int i = 1; i <= W; i++) dp[i] = -1; for (int i = 0; i < n; i++) { for (int j = W - w[i]; j >= 0; j--) { if (dp[j] >= 0 && j + w[i] <= W) { dp[j + w[i]] = ((dp[j + w[i]] > dp[j] + d[i]) ? dp[j + w[i]] : (dp[j] + d[i])); } } } for (int i = 0; i <= W; i++) if (dp[i] > dp[mx]) mx = i; cout << "Case " << c++ << endl; cout << dp[mx] << endl; cout << mx << endl; } return 0; }
#include <iostream> #include <stdio.h> using namespace std; int main(void) { int c = 1, W; while (cin >> W, W) { int n, mx = 0, w[1000], d[1000]; long dp[1001] = {0}; cin >> n; for (int i = 0; i < n; i++) scanf("%d,%d", &d[i], &w[i]); for (int i = 1; i <= W; i++) dp[i] = -1; for (int i = 0; i < n; i++) { for (int j = W - w[i]; j >= 0; j--) { if (dp[j] >= 0 && j + w[i] <= W) { dp[j + w[i]] = ((dp[j + w[i]] > dp[j] + d[i]) ? dp[j + w[i]] : (dp[j] + d[i])); } } } for (int i = 0; i <= W; i++) if (dp[i] > dp[mx]) mx = i; cout << "Case " << c++ << ":" << endl; cout << dp[mx] << endl; cout << mx << endl; } return 0; }
[["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 62], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 0, 52, 8, 9, 0, 1, 0, 16, 17, 151]]
1
289
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; int dp[1001][1001]; int main() { int c = 1; while (1) { for (int i = 0; i <= 1000; i++) for (int j = 0; j <= 1000; j++) dp[i][j] = 0; int w, n; cin >> w >> n; if (w == 0) break; int treasure[1000]; int price[1000]; for (int i = 0; i < n; i++) scanf("%d,%d", &price[i], &treasure[i]); for (int i = 0; i < n; i++) { for (int j = 0; j <= w; j++) { if (j < treasure[i]) dp[i + 1][j] = dp[i][j]; else dp[i + 1][j] = max(dp[i][j], dp[i][j - treasure[i]] + price[i]); } } int ansWeight; for (ansWeight = w - 1; w >= 0; ansWeight--) if (dp[n][w] > dp[n][ansWeight]) break; cout << "Case" << c++ << ":" << endl; cout << dp[n][w] << endl; cout << ansWeight + 1 << endl; } return 0; }
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; int dp[1001][1001]; int main() { int c = 1; while (1) { for (int i = 0; i <= 1000; i++) for (int j = 0; j <= 1000; j++) dp[i][j] = 0; int w, n; cin >> w >> n; if (w == 0) break; int treasure[1000]; int price[1000]; for (int i = 0; i < n; i++) scanf("%d,%d", &price[i], &treasure[i]); for (int i = 0; i < n; i++) { for (int j = 0; j <= w; j++) { if (j < treasure[i]) dp[i + 1][j] = dp[i][j]; else dp[i + 1][j] = max(dp[i][j], dp[i][j - treasure[i]] + price[i]); } } int ansWeight; for (ansWeight = w - 1; w >= 0; ansWeight--) if (dp[n][w] > dp[n][ansWeight]) break; cout << "Case " << c++ << ":" << endl; cout << dp[n][w] << endl; cout << ansWeight + 1 << endl; } return 0; }
[["-", 31, 16, 31, 16, 31, 16, 12, 5, 0, 6], ["+", 31, 16, 31, 16, 31, 16, 12, 5, 0, 6]]
1
313
#include <algorithm> #include <cstdio> using namespace std; int T, N, V[1010], W[1010]; int main() { for (int t = 1; scanf("%d", &T) && T; t++) { scanf("%d", &N); for (int i = 0; i < N; i++) { scanf("%d,%d", &V[i], &W[i]); } int A[1010][1010] = {{0}}; for (int i = 1; i < N; i++) { for (int j = 0; j < T; j++) { if (j < W[i]) { A[i][j] = A[i - 1][j]; } else { A[i][j] = max(A[i - 1][j], A[i - 1][j - W[i - 1]] + V[i - 1]); } } } int best = -1, best_w = -1; for (int j = 0; j <= T; j++) { if (A[N][j] > best) { best = A[N][j]; best_w = j; } } printf("Case %d:\n%d\n%d\n", t, best, best_w); } }
#include <algorithm> #include <cstdio> using namespace std; int T, N, V[1010], W[1010]; int main() { for (int t = 1; scanf("%d", &T) && T; t++) { scanf("%d", &N); for (int i = 0; i < N; i++) { scanf("%d,%d", &V[i], &W[i]); } int A[1010][1010] = {{0}}; for (int i = 1; i <= N; i++) { for (int j = 0; j <= T; j++) { if (j < W[i - 1]) { A[i][j] = A[i - 1][j]; } else { A[i][j] = max(A[i - 1][j], A[i - 1][j - W[i - 1]] + V[i - 1]); } } } int best = -1, best_w = -1; for (int j = 0; j <= T; j++) { if (A[N][j] > best) { best = A[N][j]; best_w = j; } } printf("Case %d:\n%d\n%d\n", t, best, best_w); } }
[["-", 0, 7, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 17, 19], ["+", 51, 16, 12, 69, 341, 342, 0, 16, 17, 33], ["+", 51, 16, 12, 69, 341, 342, 0, 16, 12, 13]]
1
293
// バグ有 #include <cmath> #include <iostream> #include <vector> using namespace std; int main() { int W, N; char d; int counter = 1; int takara[1000][2]; while (1) { cin >> W; if (W == 0) { break; } cin >> N; for (int i = 0; i < N; i++) { cin >> takara[i][0] >> d >> takara[i][1]; } vector<vector<int>> dp(W, vector<int>(N + 1)); for (int i = 0; i <= N; i++) { for (int j = 0; j <= W; j++) { dp[j][i] = 0; } } for (int i = 0; i <= N; i++) { for (int j = 0; j <= W; j++) { if (i == 0) { continue; } if (j < takara[i - 1][1]) { dp[j][i] = dp[j][i - 1]; } else { dp[j][i] = max(dp[j][i - 1], dp[j - takara[i - 1][1]][i - 1] + takara[i - 1][0]); } } } int MAX[2]; MAX[0] = 0; MAX[1] = 10000; for (int i = 0; i <= W; i++) { if (MAX[0] == dp[i][N]) { MAX[1] = min(MAX[1], i); } else if (MAX[0] < dp[i][N]) { MAX[0] = dp[i][N]; MAX[1] = i; } } cout << "Case " << counter << ':' << endl << MAX[0] << endl << MAX[1] << endl; counter++; } return 0; }
// バグ有 #include <cmath> #include <iostream> #include <vector> using namespace std; int main() { int W, N; char d; int counter = 1; int takara[1000][2]; while (1) { cin >> W; if (W == 0) { break; } cin >> N; for (int i = 0; i < N; i++) { cin >> takara[i][0] >> d >> takara[i][1]; } vector<vector<int>> dp(W + 1, vector<int>(N + 1)); for (int i = 0; i <= N; i++) { for (int j = 0; j <= W; j++) { dp[j][i] = 0; } } for (int i = 0; i <= N; i++) { for (int j = 0; j <= W; j++) { if (i == 0) { continue; } if (j < takara[i - 1][1]) { dp[j][i] = dp[j][i - 1]; } else { dp[j][i] = max(dp[j][i - 1], dp[j - takara[i - 1][1]][i - 1] + takara[i - 1][0]); } } } int MAX[2]; MAX[0] = 0; MAX[1] = 10000; for (int i = 0; i <= W; i++) { if (MAX[0] == dp[i][N]) { MAX[1] = min(MAX[1], i); } else if (MAX[0] < dp[i][N]) { MAX[0] = dp[i][N]; MAX[1] = i; } } cout << "Case " << counter << ':' << endl << MAX[0] << endl << MAX[1] << endl; counter++; } return 0; }
[["+", 0, 43, 49, 50, 51, 4, 0, 16, 17, 72], ["+", 0, 43, 49, 50, 51, 4, 0, 16, 12, 13]]
1
433
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < n; i++) #define COUNT(i, n) for (int i = 1; i <= n; i++) #define ALL(T) T.begin(), T.end() #define mp make_pair #define pb push_back #define ITER(c) __typeof((c).begin()) #define each(c, it) for (ITER(c) it = (c).begin(); it != (c).end(); it++) #define X first #define Y second using namespace std; typedef pair<int, int> pii; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<string> vs; const int INF = 1 << 24; const int N = 1001; int dp[N]; int cnt; int main(void) { for (int ww; cin >> ww, ww;) { int n; cin >> n; rep(i, N) dp[i] = -1; dp[0] = 0; while (n--) { int p, w; char c; cin >> p >> c >> w; COUNT(i, N) { int cur = N - i; if (~dp[cur] && cur + w <= ww) dp[cur + w] = max(dp[cur + w], dp[cur] + p); } } int ind = max_element(dp, dp + N) - dp; cout << "Case" << ++cnt << endl << dp[ind] << endl << ind << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < n; i++) #define COUNT(i, n) for (int i = 1; i <= n; i++) #define ALL(T) T.begin(), T.end() #define mp make_pair #define pb push_back #define ITER(c) __typeof((c).begin()) #define each(c, it) for (ITER(c) it = (c).begin(); it != (c).end(); it++) #define X first #define Y second using namespace std; typedef pair<int, int> pii; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<string> vs; const int INF = 1 << 24; const int N = 1001; int dp[N]; int cnt; int main(void) { for (int ww; cin >> ww, ww;) { int n; cin >> n; rep(i, N) dp[i] = -1; dp[0] = 0; while (n--) { int p, w; char c; cin >> p >> c >> w; COUNT(i, N) { int cur = N - i; if (~dp[cur] && cur + w <= ww) dp[cur + w] = max(dp[cur + w], dp[cur] + p); } } int ind = max_element(dp, dp + N) - dp; cout << "Case " << ++cnt << ":" << endl << dp[ind] << endl << ind << endl; } return 0; }
[["-", 31, 16, 31, 16, 31, 16, 12, 5, 0, 6], ["+", 31, 16, 31, 16, 31, 16, 12, 5, 0, 6], ["+", 31, 16, 31, 16, 31, 16, 31, 16, 17, 151], ["+", 31, 16, 31, 16, 31, 16, 12, 5, 0, 62]]
1
291
i = 1 until (furoshiki = $<.gets.to_i).zero? table = {0 => 0} Array.new($<.gets.to_i) {$<.gets.split(",").map(&:to_i)}.each do |v, w| h = Hash.new(0) table.each {|key, value| h[key + w] = table[key] + v if key + w <= furoshiki} table.merge!(h) {|k, v1, v2| [v1, v2].max} end p table m = table.values.max max = table.select {|k, v| v == m}.sort {|a, b| a[0] <=> b[0]}.first puts "Case #{i}:", max[1], max[0] i += 1 end
i = 1 until (furoshiki = $<.gets.to_i).zero? table = {0 => 0} Array.new($<.gets.to_i) {$<.gets.split(",").map(&:to_i)}.each do |v, w| h = Hash.new(0) table.each {|key, value| h[key + w] = table[key] + v if key + w <= furoshiki} table.merge!(h) {|k, v1, v2| [v1, v2].max} end m = table.values.max max = table.select {|k, v| v == m}.sort {|a, b| a[0] <=> b[0]}.first puts "Case #{i}:", max[1], max[0] i += 1 end
[["-", 0, 493, 0, 750, 8, 170, 0, 652, 735, 22], ["-", 0, 750, 8, 170, 0, 652, 3, 4, 0, 22]]
4
184
#!/usr/bin/env ruby k = 0 loop do k += 1 w = gets.chomp!.to_i break if w.zero? n = gets.chomp!.to_i things = [] dyn = Array.new(w+1, -1) n.times do things << gets.chomp!.split(?,).map(&:to_i) end ## ???????¨??????? # ????????? dyn[0] = 0 # ?????? things.each do |e| w.downto(0) do |i| next if dyn[i] < 0 next if i+e[1] > w dyn[i+e[1]] = dyn[i] + e[0] if dyn[i+e[1]] < dyn[i] + e[0] end end max = dyn.max w.times do |i| if dyn[i] == max puts "Case #{k}:" puts max puts i end end end
#!/usr/bin/env ruby k = 0 loop do k += 1 w = gets.chomp!.to_i break if w.zero? n = gets.chomp!.to_i things = [] dyn = Array.new(w+1, -1) n.times do things << gets.chomp!.split(?,).map(&:to_i) end ## ???????¨??????? # ????????? dyn[0] = 0 # ?????? things.each do |e| w.downto(0) do |i| next if dyn[i] < 0 next if i+e[1] > w dyn[i+e[1]] = dyn[i] + e[0] if dyn[i+e[1]] < dyn[i] + e[0] end end max = dyn.max (w+1).times do |i| if dyn[i] == max puts "Case #{k}:" puts max puts i break end end end
[["+", 196, 737, 8, 736, 0, 652, 486, 739, 0, 24], ["+", 8, 736, 0, 652, 486, 739, 0, 738, 17, 72], ["+", 8, 736, 0, 652, 486, 739, 0, 738, 12, 612], ["+", 196, 737, 8, 736, 0, 652, 486, 739, 0, 25], ["+", 8, 736, 0, 121, 64, 749, 0, 94, 0, 94]]
4
185
loop do w = gets.to_i @num ||= 1 break if w==0 n = gets.to_i dp = Array.new(n+1){ Array.new(w+1){ {v: 0, w: 0}}} n.times.map{ gets.split(",").map(&:to_i) }.each_with_index do |(vi,wi), i| w.times do |j| t = dp[i][[j+1-wi,0].max] dp[i+1][j+1] = if j+1>=wi && t[:v]+vi >= dp[i][j+1][:v] if t[:v]+vi==dp[i][j+1][:v] t[:w]+wi<dp[i][j+1][:w] ? {v: t[:v]+vi, w: t[:w]+wi} : dp[i][j+1] else {v: t[:v]+vi, w: t[:w]+wi} end else dp[i][j+1] end end end puts "Case #{@num}" puts dp[n][w][:v] puts dp[n][w][:w] @num += 1 end
loop do w = gets.to_i @num ||= 1 break if w==0 n = gets.to_i dp = Array.new(n+1){ Array.new(w+1){ {v: 0, w: 0}}} n.times.map{ gets.split(",").map(&:to_i) }.each_with_index do |(vi,wi), i| w.times do |j| t = dp[i][[j+1-wi,0].max] dp[i+1][j+1] = if j+1>=wi && t[:v]+vi >= dp[i][j+1][:v] if t[:v]+vi==dp[i][j+1][:v] t[:w]+wi<dp[i][j+1][:w] ? {v: t[:v]+vi, w: t[:w]+wi} : dp[i][j+1] else {v: t[:v]+vi, w: t[:w]+wi} end else dp[i][j+1] end end end puts "Case #{@num}:" puts dp[n][w][:v] puts dp[n][w][:w] @num += 1 end
[["+", 8, 736, 0, 652, 3, 4, 0, 557, 0, 6]]
4
284
@case_num = 1 while true do w = gets.to_i break if w == 0 n = gets.to_i table = n.times.map{ gets.split(',').map(&:to_i) } mw, mv = w, 0 dp = Array.new(n+1){Array.new} dp[0][0] = 0 table.each.with_index(1) do |p, i| (0..w).reject{ |wi| dp[i-1][wi].nil? }.each do |wi| va = dp[i][wi] = [dp[i][wi]||0, dp[i-1][wi]].max mw, mv = wi, va if va > mv || (va==mv && wi < mw) if wi + p[1] < w va = dp[i][wi+p[1]] = [dp[i][wi+p[1]]||0, dp[i-1][wi] + p[0]].max mw, mv = wi + p[1], va if va > mv || (va==mv && (wi + p[1])< mw) end end end puts "Case #{@case_num}:", mv, mw @case_num += 1 end
@case_num = 1 while true do w = gets.to_i break if w == 0 n = gets.to_i table = n.times.map{ gets.split(',').map(&:to_i) } mw, mv = w, 0 dp = Array.new(n+1){Array.new} dp[0][0] = 0 table.each.with_index(1) do |p, i| (0..w).reject{ |wi| dp[i-1][wi].nil? }.each do |wi| va = dp[i][wi] = [dp[i][wi]||0, dp[i-1][wi]].max mw, mv = wi, va if va > mv || (va==mv && wi < mw) if wi + p[1] <= w va = dp[i][wi+p[1]] = [dp[i][wi+p[1]]||0, dp[i-1][wi] + p[0]].max mw, mv = wi + p[1], va if va > mv || (va==mv && (wi + p[1])< mw) end end end puts "Case #{@case_num}:", mv, mw @case_num += 1 end
[["-", 196, 737, 8, 736, 0, 121, 15, 738, 17, 18], ["+", 196, 737, 8, 736, 0, 121, 15, 738, 17, 19]]
4
281
@num = 1 loop do w = gets.to_i break if w == 0 n = gets.to_i arr = n.times.map { gets.split(',').map(&:to_i) } dp = Array.new(n+1) { Array.new(w+1) } dp[0][0] = 0 mw, mv = w, 0 arr.each_with_index do |ia, i| (0..w).to_a.reject{ |wi| dp[i][wi].nil? }.each do |wi| t = dp[i+1][wi] = [dp[i][wi], dp[i+1][wi]||0].max mw, mv = wi, t if mv < t || (mv == t && mw > wi) if wi+ia[1] <= w t = dp[i+1][wi+ia[1]] = [dp[i][wi]+ia[0], dp[i+1][wi+ia[1]]||0].max mw, mv = wi+ia[1], t if mv < t || (mv == t && mw > wi+ia[1]) end end end puts "Case: #{@num}", mv, mw @num += 1 end
@num = 1 loop do w = gets.to_i break if w == 0 n = gets.to_i arr = n.times.map { gets.split(',').map(&:to_i) } dp = Array.new(n+1) { Array.new(w+1) } dp[0][0] = 0 mw, mv = w, 0 arr.each_with_index do |ia, i| (0..w).to_a.reject{ |wi| dp[i][wi].nil? }.each do |wi| t = dp[i+1][wi] = [dp[i][wi], dp[i+1][wi]||0].max mw, mv = wi, t if mv < t || (mv == t && mw > wi) if wi+ia[1] <= w t = dp[i+1][wi+ia[1]] = [dp[i][wi]+ia[0], dp[i+1][wi+ia[1]]||0].max mw, mv = wi+ia[1], t if mv < t || (mv == t && mw > wi+ia[1]) end end end puts "Case #{@num}:", mv, mw @num += 1 end
[["-", 8, 736, 0, 652, 3, 4, 0, 557, 0, 6], ["+", 8, 736, 0, 652, 3, 4, 0, 557, 0, 6]]
4
281
from sys import stdin case = 0 while(True): case += 1 W = int(stdin.readline()) if not W: break print("Case %d"%case) N = int(stdin.readline()) v = [0] * N w = [0] * N dp = [[0]*(W + 1) for _ in range(N + 1)] for k in range(N): v[k], w[k] = map(int, stdin.readline().split(",")) for i in range(N-1,-1,-1): for j in range(W+1): if j < w[i]: dp[i][j] = dp[i + 1][j] else: dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - w[i]] + v[i]) print(dp[0][W]) for i in range(W+1): if dp[0][i] == dp[0][W]: print(i); break
from sys import stdin case = 0 while(True): case += 1 W = int(stdin.readline()) if not W: break print("Case %d:"%case) N = int(stdin.readline()) v = [0] * N w = [0] * N dp = [[0]*(W + 1) for _ in range(N + 1)] for k in range(N): v[k], w[k] = map(int, stdin.readline().split(",")) for i in range(N-1,-1,-1): for j in range(W+1): if j < w[i]: dp[i][j] = dp[i + 1][j] else: dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - w[i]] + v[i]) print(dp[0][W]) for i in range(W+1): if dp[0][i] == dp[0][W]: print(i); break
[["-", 0, 652, 3, 4, 0, 657, 31, 557, 0, 6], ["+", 0, 652, 3, 4, 0, 657, 31, 557, 0, 6]]
5
255
#include <iostream> using namespace std; int main() { char com, from, to, ball = 'A'; while (cin >> from >> com >> to) { if (from == ball) ball = to; if (to == ball) ball = from; } cout << ball << endl; }
#include <iostream> using namespace std; int main() { char com, from, to, ball = 'A'; while (cin >> from >> com >> to) { if (from == ball) { ball = to; } else if (to == ball) { ball = from; } } cout << ball << endl; }
[["+", 0, 52, 8, 9, 0, 57, 64, 9, 0, 45], ["+", 0, 52, 8, 9, 0, 57, 64, 9, 0, 46], ["+", 0, 52, 8, 9, 0, 57, 75, 76, 0, 95], ["+", 0, 57, 75, 76, 0, 57, 64, 9, 0, 45], ["+", 0, 57, 75, 76, 0, 57, 64, 9, 0, 46]]
1
63
#include <cstdio> #include <iostream> using namespace std; int main() { char a, b, ans = 'A'; while (scanf(" %c,%c", &a, &b) != EOF) { if (a = ans) { ans = b; } else if (b == ans) { ans = a; } } cout << ans << endl; return 0; }
#include <cstdio> #include <iostream> using namespace std; int main() { char a, b, ans = 'A'; while (scanf(" %c,%c", &a, &b) != EOF) { if (a == ans) { ans = b; } else if (b == ans) { ans = a; } } cout << ans << endl; return 0; }
[["-", 8, 9, 0, 57, 15, 339, 51, 11, 17, 32], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 60]]
1
78
#include <iostream> using namespace std; int conv(char c) { switch (c) { case 'A': return 0; case 'B': return 1; case 'C': return 2; } return 1; } int main() { bool b[3]; b[0] = 1; b[1] = 0; b[2] = 0; string s; while (cin >> s) { int swap1, swap2; swap1 = conv(s[0]); swap2 = conv(s[2]); swap(b[swap1], b[swap2]); } int ans = 0; for (int i = 0; i < 3; i++) { if (b[i]) ans = i; } switch (ans) { case 0: cout << "A" << endl; break; case 1: cout << "A" << endl; break; case 2: cout << "A" << endl; break; } }
#include <iostream> using namespace std; int conv(char c) { switch (c) { case 'A': return 0; case 'B': return 1; case 'C': return 2; } return 1; } int main() { bool b[3]; b[0] = 1; b[1] = 0; b[2] = 0; string s; while (cin >> s) { int swap1, swap2; swap1 = conv(s[0]); swap2 = conv(s[2]); swap(b[swap1], b[swap2]); } int ans = 0; for (int i = 0; i < 3; i++) { if (b[i]) ans = i; } switch (ans) { case 0: cout << "A" << endl; break; case 1: cout << "B" << endl; break; case 2: cout << "C" << endl; break; } }
[["-", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
1
206
#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> int main() { char X, Y, P = 'A'; while (scanf("%c,%c", X, Y)) { if (X == P) { P = Y; } else if (Y == P) { P = X; } } printf("%c\n", P); return 0; }
#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> int main() { char X, Y, P = 'A'; while (scanf("%c,%c", &X, &Y) != EOF) { if (X == P) { P = Y; } else if (Y == P) { P = X; } } printf("%c\n", P); return 0; }
[["+", 51, 16, 31, 2, 3, 4, 0, 66, 17, 67], ["+", 8, 9, 0, 52, 15, 339, 51, 16, 17, 79], ["+", 8, 9, 0, 52, 15, 339, 51, 16, 12, 22]]
1
75
#include <iostream> #include <string> using namespace std; int main() { string str; int a, b, c, d; a = 1; b = c = 0; while (cin >> str) { if ((str[0] == 'A' && str[2] == 'B') || (str[0] == 'B' && str[2] == 'A')) { d = a; a = b; b = d; } else if ((str[0] == 'B' && str[3] == 'C') || (str[0] == 'C' && str[2] == 'B')) { d = b; b = c; c = d; } else { d = a; a = c; c = d; } } if (a == 1) { cout << "A"; } else if (b == 1) { cout << "B"; } else { cout << "C"; } cout << endl; return 0; }
#include <iostream> #include <string> using namespace std; int main() { string str; int a, b, c, d; a = 1; b = 0; c = 0; while (cin >> str) { if ((str[0] == 'A' && str[2] == 'B') || (str[0] == 'B' && str[2] == 'A')) { d = a; a = b; b = d; } else if ((str[0] == 'B' && str[2] == 'C') || (str[0] == 'C' && str[2] == 'B')) { d = b; b = c; c = d; } else { d = a; a = c; c = d; } } if (a == 1) { cout << "A"; } else if (b == 1) { cout << "B"; } else { cout << "C"; } cout << endl; return 0; }
[["+", 0, 14, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 0, 30, 0, 14, 8, 9, 0, 1, 0, 35], ["-", 0, 16, 12, 16, 31, 69, 341, 342, 0, 13], ["+", 0, 16, 12, 16, 31, 69, 341, 342, 0, 13]]
1
217
#include <algorithm> #include <cmath> #include <cstdio> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define fi first #define se second #define fcout(n) cout << fixed << setprecision((n)) #define cinl(str) getline(cin, (str)) using namespace std; bool value(int y, int x, int R, int C) { return 0 <= y && y < R && 0 <= x && x < C; } typedef pair<int, int> pii; typedef long long ll; typedef vector<int> vi; typedef vector<long long> vll; typedef vector<vi> vvi; double pie = acos(-1); int INF = 10000009; int dx[4] = {0, -1, 0, 1}; int dy[4] = {-1, 0, 1, 0}; int main() { char ans = 'A'; char a, b, c; while (true) { cin >> a >> b >> c; if (cin.eof()) break; if (a == ans) { ans = c; } if (c == ans) { ans = a; } } cout << ans << endl; }
#include <algorithm> #include <cmath> #include <cstdio> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define fi first #define se second #define fcout(n) cout << fixed << setprecision((n)) #define cinl(str) getline(cin, (str)) using namespace std; bool value(int y, int x, int R, int C) { return 0 <= y && y < R && 0 <= x && x < C; } typedef pair<int, int> pii; typedef long long ll; typedef vector<int> vi; typedef vector<long long> vll; typedef vector<vi> vvi; double pie = acos(-1); int INF = 10000009; int dx[4] = {0, -1, 0, 1}; int dy[4] = {-1, 0, 1, 0}; int main() { char ans = 'A'; char a, b, c; while (true) { cin >> a >> b >> c; if (cin.eof()) break; if (a == ans) { ans = c; continue; } if (c == ans) { ans = a; continue; } } cout << ans << endl; }
[["+", 8, 9, 0, 57, 64, 9, 0, 116, 0, 117], ["+", 8, 9, 0, 57, 64, 9, 0, 116, 0, 35], ["+", 8, 9, 0, 57, 64, 9, 0, 1, 0, 35]]
1
237
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <string> #include <vector> #define shosu(x) fixed << setprecision(x) using namespace std; typedef pair<int, int> P; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, P> pip; typedef long long ll; const int inf = (1 << 31) - 1; const double pi = acos(-1); char s, t, tmp, now = 'A'; int main() { while (cin >> s >> tmp >> t) { if (s == now) now = t; if (t == now) now = s; } cout << now << endl; }
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <string> #include <vector> #define shosu(x) fixed << setprecision(x) using namespace std; typedef pair<int, int> P; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, P> pip; typedef long long ll; const int inf = (1 << 31) - 1; const double pi = acos(-1); char s, t, tmp, now = 'A'; int main() { while (cin >> s >> tmp >> t) { if (s == now) now = t; else if (t == now) now = s; } cout << now << endl; }
[["+", 0, 52, 8, 9, 0, 57, 75, 76, 0, 95]]
1
139
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <ctime> #include <deque> #include <iostream> #include <limits> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define rep(i, a, n) for (int i = a; i < n; i++) #define repr(i, a, n) for (int i = a; i >= n; i--) #define pb(a) push_back(a) #define fr first #define sc second #define INF 999999999 #define X real() #define Y imag() #define EPS (1e-10) #define EQ(a, b) (abs((a) - (b)) < EPS) #define EQV(a, b) (EQ((a).X, (b).X) && EQ((a).Y, (b).Y)) #define LE(n, m) ((n) < (m) + EPS) #define GE(n, m) ((n) + EPS > (m)) typedef vector<int> VI; typedef vector<VI> MAT; typedef pair<int, int> pii; typedef long long int ll; typedef complex<double> P; typedef pair<P, P> L; typedef pair<P, double> C; int dy[] = {0, 0, 1, -1}; int dx[] = {1, -1, 0, 0}; int const MOD = 1000000007; namespace std { bool operator<(const P a, const P b) { return a.X != b.X ? a.X < b.X : a.Y < b.Y; } } // namespace std int main() { int a[3] = {1, 0, 0}; char c, d; map<char, int> m; m['A'] = 0; m['B'] = 1; m['C'] = 2; while (scanf("%c,%c", &c, &d) + 1) { swap(a[m[c]], a[m[d]]); } if (a[0]) cout << "A" << endl; else if (a[1]) cout << "B" << endl; else cout << "C" << endl; return 0; }
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <ctime> #include <deque> #include <iostream> #include <limits> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define rep(i, a, n) for (int i = a; i < n; i++) #define repr(i, a, n) for (int i = a; i >= n; i--) #define pb(a) push_back(a) #define fr first #define sc second #define INF 999999999 #define X real() #define Y imag() #define EPS (1e-10) #define EQ(a, b) (abs((a) - (b)) < EPS) #define EQV(a, b) (EQ((a).X, (b).X) && EQ((a).Y, (b).Y)) #define LE(n, m) ((n) < (m) + EPS) #define GE(n, m) ((n) + EPS > (m)) typedef vector<int> VI; typedef vector<VI> MAT; typedef pair<int, int> pii; typedef long long int ll; typedef complex<double> P; typedef pair<P, P> L; typedef pair<P, double> C; int dy[] = {0, 0, 1, -1}; int dx[] = {1, -1, 0, 0}; int const MOD = 1000000007; namespace std { bool operator<(const P a, const P b) { return a.X != b.X ? a.X < b.X : a.Y < b.Y; } } // namespace std int main() { int a[3] = {1, 0, 0}; char c, d; map<char, int> m; m['A'] = 0; m['B'] = 1; m['C'] = 2; while (scanf("%c,%c\n", &c, &d) + 1) { swap(a[m[c]], a[m[d]]); } if (a[0]) cout << "A" << endl; else if (a[1]) cout << "B" << endl; else cout << "C" << endl; return 0; }
[["+", 51, 16, 31, 2, 3, 4, 0, 5, 0, 44]]
1
389
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <list> #include <map> #include <stack> #include <vector> using namespace std; #define REP(i, n) for (int(i) = 0; (i) < (n); (i)++) #define FOR(i, a, b) for (int(i) = (a); (i) < (b); (i)++) #define PUSH(n, v) \ for (int i = 0; i < (n); i++) { \ int j; \ cin >> j; \ v.push_back(j); \ } #define ALL(v) v.begin(), v.end() #define print(s) cout << (s) << endl int cups[3] = {1, 0, 0}; void swapCup(char c1, char c2) { int temp = cups[c1 - 'A']; cups[c1 - 'A'] = cups[c2 - 'A']; cups[c2 - 'A'] = temp; } int main() { char c1, c2, c3; while (cin >> c1 >> c2 >> c3) { swapCup(c1, c3); } REP(i, 3) { if (cups[i]) cout << (char)('A' + i) << endl; break; } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <list> #include <map> #include <stack> #include <vector> using namespace std; #define REP(i, n) for (int(i) = 0; (i) < (n); (i)++) #define FOR(i, a, b) for (int(i) = (a); (i) < (b); (i)++) #define PUSH(n, v) \ for (int i = 0; i < (n); i++) { \ int j; \ cin >> j; \ v.push_back(j); \ } #define ALL(v) v.begin(), v.end() #define print(s) cout << (s) << endl int cups[3] = {1, 0, 0}; void swapCup(char c1, char c2) { int temp = cups[c1 - 'A']; cups[c1 - 'A'] = cups[c2 - 'A']; cups[c2 - 'A'] = temp; } int main() { char c1, c2, c3; while (cin >> c1 >> c2 >> c3) { swapCup(c1, c3); } REP(i, 3) { if (cups[i]) { cout << (char)('A' + i) << endl; break; } } return 0; }
[["+", 8, 9, 0, 9, 0, 57, 64, 9, 0, 45], ["+", 8, 9, 0, 9, 0, 57, 64, 9, 0, 46]]
1
193
#define scanf_s scanf //#define gets_s gets #include <iostream> #include <math.h> #include <stdio.h> #include <string> using namespace std; #define MIN -1 #define MAX 1000000 int main(void) { char a, b, x = 'A'; while (scanf_s("%c,%c", &a, &b) != EOF) { if (a == x) x = b; if (b == x) x = a; } printf("%c\n", x); }
#define scanf_s scanf //#define gets_s gets #include <iostream> #include <math.h> #include <stdio.h> #include <string> using namespace std; #define MIN -1 #define MAX 1000000 int main(void) { char a, b, x = 'A'; while (scanf_s("%c,%c", &a, &b) != EOF) { if (a == x) x = b; else if (b == x) x = a; } printf("%c\n", x); }
[["+", 0, 52, 8, 9, 0, 57, 75, 76, 0, 95]]
1
89
#include <iostream> using namespace std; int main() { char c1, c2, delim, ball = 'A'; while (cin >> c1 >> delim >> c2) { if (c1 == ball) ball == c2; else if (c2 == ball) ball == c1; } cout << ball << endl; }
#include <iostream> using namespace std; int main() { char c1, c2, delim, ball = 'A'; while (cin >> c1 >> delim >> c2) { if (c1 == ball) ball = c2; else if (c2 == ball) ball = c1; } cout << ball << endl; }
[["-", 8, 9, 0, 57, 64, 1, 0, 16, 17, 60], ["+", 8, 9, 0, 57, 64, 1, 0, 11, 17, 32], ["-", 75, 76, 0, 57, 64, 1, 0, 16, 17, 60], ["+", 75, 76, 0, 57, 64, 1, 0, 11, 17, 32]]
1
64
#include <cstdio> #include <iostream> using namespace std; int main() { char x, y, now = 'A'; while (scanf("%c,%c", &x, &y) > 0) { if (now == x) now = y; if (now == y) now = x; } cout << now << endl; return 0; }
#include <cstdio> #include <iostream> using namespace std; int main() { char x, y, now = 'A'; while (scanf("%c,%c", &x, &y) > 0) { if (now == x) now = y; else if (now == y) now = x; } cout << now << endl; return 0; }
[["+", 0, 52, 8, 9, 0, 57, 75, 76, 0, 95]]
1
73
#include <iostream> using namespace std; int main() { char a, b, c, ans = 'A'; while (cin >> a >> c >> b) { if (a == 'A' && a == ans) { if (b == 'B') { ans = 'B'; } if (b == 'C') { ans = 'C'; } } if (a == 'B' && a == ans) { if (b == 'A') { ans = 'A'; } if (b == 'C') { ans = 'C'; } } if (a == 'C' && a == ans) { if (b == 'A') { ans = 'A'; } if (b == 'B') { ans = 'B'; } } if (b == 'A' && b == ans) { if (a == 'B') { ans = 'B'; } if (a == 'C') { ans = 'C'; } } if (b == 'B' && b == ans) { if (a == 'A') { ans = 'A'; } if (a == 'C') { ans = 'C'; } } if (b == 'C' && b == ans) { if (a == 'A') { ans = 'A'; } if (a == 'B') { ans = 'B'; } } } cout << ans << endl; return 0; }
#include <iostream> using namespace std; int main() { char a, b, c, ans = 'A'; while (cin >> a >> c >> b) { if (a == 'A' && a == ans) { if (b == 'B') { ans = 'B'; } if (b == 'C') { ans = 'C'; } } else if (a == 'B' && a == ans) { if (b == 'A') { ans = 'A'; } if (b == 'C') { ans = 'C'; } } else if (a == 'C' && a == ans) { if (b == 'A') { ans = 'A'; } if (b == 'B') { ans = 'B'; } } else if (b == 'A' && b == ans) { if (a == 'B') { ans = 'B'; } if (a == 'C') { ans = 'C'; } } else if (b == 'B' && b == ans) { if (a == 'A') { ans = 'A'; } if (a == 'C') { ans = 'C'; } } else if (b == 'C' && b == ans) { if (a == 'A') { ans = 'A'; } if (a == 'B') { ans = 'B'; } } } cout << ans << endl; return 0; }
[["+", 0, 52, 8, 9, 0, 57, 75, 76, 0, 95], ["+", 0, 57, 75, 76, 0, 57, 75, 76, 0, 95]]
1
322
#include <iostream> using namespace std; main() { bool C[3]; C[1] = C[2] = false; C[0] = true; char s, t, k; while (cin >> s >> k >> t) swap(C[s - 'a'], C[t - 'a']); if (C[0]) cout << "A" << endl; if (C[1]) cout << "B" << endl; if (C[2]) cout << "C" << endl; }
#include <iostream> using namespace std; main() { bool C[3]; C[1] = C[2] = false; C[0] = true; char s, t, k; while (cin >> s >> k >> t) swap(C[s - 'A'], C[t - 'A']); if (C[0]) cout << "A" << endl; if (C[1]) cout << "B" << endl; if (C[2]) cout << "C" << endl; }
[["-", 0, 69, 341, 342, 0, 16, 12, 103, 0, 125], ["+", 0, 69, 341, 342, 0, 16, 12, 103, 0, 125]]
1
119
#include <algorithm> #include <cctype> #include <cstdio> #include <iostream> #include <math.h> using namespace std; int main() { char c = 'A', x, y; while (scanf("%c,%c", &x, &y) != EOF) { if (c == x) c = y; if (c == y) c = x; } cout << c << endl; }
#include <algorithm> #include <cctype> #include <cstdio> #include <iostream> #include <math.h> using namespace std; int main() { char c = 'A', x, y; while (scanf("%c,%c", &x, &y) != EOF) { if (c == x) { c = y; continue; } if (c == y) c = x; } cout << c << endl; }
[["+", 0, 52, 8, 9, 0, 57, 64, 9, 0, 45], ["+", 8, 9, 0, 57, 64, 9, 0, 1, 0, 35], ["+", 8, 9, 0, 57, 64, 9, 0, 116, 0, 117], ["+", 0, 52, 8, 9, 0, 57, 64, 9, 0, 46]]
1
76
#include <cstdio> #include <iostream> using namespace std; int main() { char cur = 'A'; char x, y; while (scanf("%c,%c ", &x, &y) == 2) { if (x == cur) { cur = x; } else if (y == cur) { cur = y; } } printf("%c\n", cur); }
#include <cstdio> #include <iostream> using namespace std; int main() { char cur = 'A'; char x, y; while (scanf("%c,%c ", &x, &y) == 2) { if (x == cur) { cur = y; } else if (y == cur) { cur = x; } } printf("%c\n", cur); }
[["-", 0, 57, 64, 9, 0, 1, 0, 11, 12, 22], ["+", 0, 57, 64, 9, 0, 1, 0, 11, 12, 22]]
1
80
#include <algorithm> #include <complex> #include <cstdio> #include <iostream> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < (int)n; ++i) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() int main() { char a, b; int x[] = {1, 0, 0}; while (scanf("%c,%c", &a, &b) == 2) { swap(x[a - 'A'], x[b - 'A']); } REP(i, 3) if (x[i]) cout << (char)('A' + i) << endl; }
#include <algorithm> #include <complex> #include <cstdio> #include <iostream> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < (int)n; ++i) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() int main() { char a, b; int x[] = {1, 0, 0}; while (scanf("%c,%c\n", &a, &b) == 2) { swap(x[a - 'A'], x[b - 'A']); } REP(i, 3) if (x[i]) cout << (char)('A' + i) << endl; }
[["+", 51, 16, 31, 2, 3, 4, 0, 5, 0, 44]]
1
130
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <vector> using namespace std; #define REP(i, a, n) for (int i = a; i < n; i++) #define rep(i, n) REP(i, 0, n) typedef long long ll; void solve() {} int main() { char now = 'A'; char a, b, ch; while (cin >> a >> ch >> b) { if (a == now) now = b; if (b == now) now = a; } cout << now << endl; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <vector> using namespace std; #define REP(i, a, n) for (int i = a; i < n; i++) #define rep(i, n) REP(i, 0, n) typedef long long ll; void solve() {} int main() { char now = 'A'; char a, b, ch; while (cin >> a >> ch >> b) { if (a == now) now = b; else if (b == now) now = a; } cout << now << endl; }
[["+", 0, 52, 8, 9, 0, 57, 75, 76, 0, 95]]
1
111
#include <cstdio> using namespace std; int main() { char from, to, ball = 'A'; while (scanf("%c%*c%c", &from, &to) != EOF) { if (ball == from) { ball = to; } else if (ball == to) { ball = from; } } printf("%c\n", ball); }
#include <cstdio> using namespace std; int main() { char from, to, ball = 'A'; while (scanf("%c,%c", &from, &to) != EOF) { if (ball == from) { ball = to; } else if (ball == to) { ball = from; } } printf("%c\n", ball); }
[["-", 51, 16, 31, 2, 3, 4, 0, 5, 0, 6], ["+", 51, 16, 31, 2, 3, 4, 0, 5, 0, 6]]
1
77
#include <algorithm> #include <cassert> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <memory.h> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define all(c) ((c).begin()), ((c).end()) #define debug(c) cerr << "> " << #c << " = " << (c) << endl; #define iter(c) __typeof((c).begin()) #define present(c, e) ((c).find((e)) != (c).end()) #define cpresent(c, e) (find(all(c), (e)) != (c).end()) #define tr(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++) #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define REP(i, a, b) for (int i = (int)(a); i <= (int)(b); i++) #define mp make_pair #define fst first #define snd second #define pb push_back const double EPS = 1e-10; typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; typedef complex<double> P; int main() { char c; char p = 'A'; for (char a, b; cin >> a >> c >> b;) { if (p == a) p = a; else if (p == b) p = b; } cout << p << endl; return 0; }
#include <algorithm> #include <cassert> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <memory.h> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define all(c) ((c).begin()), ((c).end()) #define debug(c) cerr << "> " << #c << " = " << (c) << endl; #define iter(c) __typeof((c).begin()) #define present(c, e) ((c).find((e)) != (c).end()) #define cpresent(c, e) (find(all(c), (e)) != (c).end()) #define tr(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++) #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define REP(i, a, b) for (int i = (int)(a); i <= (int)(b); i++) #define mp make_pair #define fst first #define snd second #define pb push_back const double EPS = 1e-10; typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; typedef complex<double> P; int main() { char c; char p = 'A'; for (char a, b; cin >> a >> c >> b;) { if (p == a) p = b; else if (p == b) p = a; } cout << p << endl; return 0; }
[["-", 8, 9, 0, 57, 64, 1, 0, 11, 12, 22], ["+", 8, 9, 0, 57, 64, 1, 0, 11, 12, 22], ["-", 75, 76, 0, 57, 64, 1, 0, 11, 12, 22], ["+", 75, 76, 0, 57, 64, 1, 0, 11, 12, 22]]
1
208
#include <iostream> using namespace std; int main() { char i1, i2, c; bool ball[3] = {false}; ball[0] = true; while (cin >> i1) { cin >> c; cin >> i2; cin >> c; i1 -= 'A'; i2 -= 'A'; bool tmp = ball[i1]; ball[i1] = ball[i2]; ball[i2] = tmp; } c = 0; while (!ball[c]) { c++; } cout << (char)(c + 'A') << endl; }
#include <iostream> using namespace std; int main() { char i1, i2, c; bool ball[3] = {false}; ball[0] = true; while (cin >> i1) { cin >> c; cin >> i2; i1 -= 'A'; i2 -= 'A'; bool tmp = ball[i1]; ball[i1] = ball[i2]; ball[i2] = tmp; } c = 0; while (!ball[c]) { c++; } cout << (char)(c + 'A') << endl; }
[["-", 0, 52, 8, 9, 0, 1, 0, 16, 31, 22], ["-", 0, 52, 8, 9, 0, 1, 0, 16, 17, 152], ["-", 0, 52, 8, 9, 0, 1, 0, 16, 12, 22], ["-", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
1
125
#include <iostream> using namespace std; int main() { int a = 1, b = 0, c = 0, temp; char x, y; while (scanf("%c,%c", &x, &y) == 2) { if (x == 'A') { temp = a; if (y == 'B') { a = b; b = temp; } else { a = c; c = temp; } } else if (x == 'B') { temp = b; if (y == 'A') { b = a; a = temp; } else { b = c; c = temp; } } else { temp = c; if (y == 'A') { c = a; a = temp; } else { c = b; b = temp; } } } if (a == 1) cout << "A" << endl; else if (b == 1) cout << "B" << endl; else cout << "C" << endl; }
#include <iostream> using namespace std; int main() { int a = 1, b = 0, c = 0, temp; char x, y; while (scanf(" %c,%c", &x, &y) == 2) { // cout << x << y << endl; if (x == 'A') { temp = a; if (y == 'B') { a = b; b = temp; } else { a = c; c = temp; } } else if (x == 'B') { temp = b; if (y == 'A') { b = a; a = temp; } else { b = c; c = temp; } } else { temp = c; if (y == 'A') { c = a; a = temp; } else { c = b; b = temp; } } } if (a == 1) cout << "A" << endl; else if (b == 1) cout << "B" << endl; else cout << "C" << endl; }
[["-", 51, 16, 31, 2, 3, 4, 0, 5, 0, 6], ["+", 51, 16, 31, 2, 3, 4, 0, 5, 0, 6]]
1
212
#include <cstdio> char boul = 'A'; int main() { char a, b; while (scanf("%c,%c", &a, &b) != EOF) { if (a == boul) boul = b; if (b == boul) boul = a; getchar(); } printf("%c\n", boul); return 0; }
#include <cstdio> char boul = 'A'; int main() { char a, b; while (scanf("%c,%c", &a, &b) != EOF) { if (a == boul) boul = b; else if (b == boul) boul = a; getchar(); } printf("%c\n", boul); return 0; }
[["+", 0, 52, 8, 9, 0, 57, 75, 76, 0, 95]]
1
76
#include <algorithm> #include <iostream> using namespace std; int main() { int cup[3] = {1, 0, 0}; string str; while (cin >> str, !cin.eof()) { swap(cup[str[0] - 'A'], cup[str[2] - 'A']); } for (int i = 0; i < 3; i++) { if (cup[i] == 1) cout << ('A' + i) << endl; } return 0; }
#include <algorithm> #include <iostream> using namespace std; int main() { int cup[3] = {1, 0, 0}; string str; while (cin >> str, !cin.eof()) { swap(cup[str[0] - 'A'], cup[str[2] - 'A']); } for (int i = 0; i < 3; i++) { if (cup[i] == 1) cout << (char)('A' + i) << endl; } return 0; }
[["+", 0, 16, 31, 16, 12, 74, 39, 77, 39, 40], ["+", 64, 1, 0, 16, 31, 16, 12, 74, 0, 25], ["+", 0, 16, 31, 16, 12, 74, 51, 23, 0, 24]]
1
113
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; int main() { char a, b; int ball[3] = {1, 0, 0}; while (scanf("%c,%c", &a, &b) != EOF) { swap(ball[a - 'A'], ball[b - 'A']); } for (int i = 0; i < 3; ++i) { if (ball[i]) printf("%c\n", i + 'A'); } return 0; }
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; int main() { char a, b; int ball[3] = {1, 0, 0}; while (scanf(" %c,%c", &a, &b) != EOF) { swap(ball[a - 'A'], ball[b - 'A']); } for (int i = 0; i < 3; ++i) { if (ball[i]) printf("%c\n", i + 'A'); } return 0; }
[["-", 51, 16, 31, 2, 3, 4, 0, 5, 0, 6], ["+", 51, 16, 31, 2, 3, 4, 0, 5, 0, 6]]
1
115
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef pair<int, int> P; #define pu push #define pb push_back #define mp make_pair #define eps 1e-7 #define INF 2000000000 int main() { char ball = 'A', s1, s2; string s; while (cin >> s) { if (s[0] == ball) ball = s[2]; if (s[2] == ball) ball = s[0]; } cout << ball << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef pair<int, int> P; #define pu push #define pb push_back #define mp make_pair #define eps 1e-7 #define INF 2000000000 int main() { char ball = 'A', s1, s2; string s; while (cin >> s) { if (s[0] == ball) ball = s[2]; else if (s[2] == ball) ball = s[0]; } cout << ball << endl; return 0; }
[["+", 0, 52, 8, 9, 0, 57, 75, 76, 0, 95]]
1
123
#include <iostream> using namespace std; int main(void) { char p1, p2; char ten; char ans = 'A'; while (cin >> p1 >> ten >> p2) { if (p1 == ans) { ans = p2; } if (p2 == ans) { ans = p1; } } cout << ans << endl; return 0; }
//今、ボールがどこにあるかだけ考えれば出来る #include <iostream> using namespace std; int main(void) { char p1, p2; char ten; char ans = 'A'; while (cin >> p1 >> ten >> p2) { if (p1 == ans) { ans = p2; } else if (p2 == ans) { ans = p1; } } cout << ans << endl; return 0; }
[["+", 0, 52, 8, 9, 0, 57, 75, 76, 0, 95]]
1
73
#include <iostream> #include <sstream> #include <string> using namespace std; int main() { string line; char ball = 'A'; while (getline(cin, line)) { stringstream s; s << line; char a, b, c; s >> a >> b >> c; if (ball == a) ball = c; if (ball == c) ball = a; } cout << ball << endl; }
#include <iostream> #include <sstream> #include <string> using namespace std; int main() { string line; char ball = 'A'; while (getline(cin, line)) { stringstream s; s << line; char a, b, c; s >> a >> b >> c; if (ball == a) ball = c; else if (ball == c) ball = a; } cout << ball << endl; }
[["+", 0, 52, 8, 9, 0, 57, 75, 76, 0, 95]]
1
85