problem_id
stringlengths
6
6
language
stringclasses
2 values
original_status
stringclasses
3 values
original_src
stringlengths
19
243k
changed_src
stringlengths
19
243k
change
stringclasses
3 values
i1
int64
0
8.44k
i2
int64
0
8.44k
j1
int64
0
8.44k
j2
int64
0
8.44k
error
stringclasses
270 values
stderr
stringlengths
0
226k
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll MOD = 1e9 + 7; const ll inf = 1LL << 60; const double eps = 1e-9; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } ll dp[100 + 1][100 + 1] = {0}; int main() { int N, W; cin >> N >> W; ll w[N], v[N]; for (int i = 0; i < N; i++) cin >> w[i] >> v[i]; // for (int i = 0; i < N; i++) cout << w[i] << v[i] << endl; for (int i = 0; i < N; i++) { for (int j = 0; j <= W; j++) { if (j >= w[i]) { // cout << dp[i][j-w[i]] << endl; chmax(dp[i + 1][j], dp[i][j - w[i]] + v[i]); // cout << dp[i+1][j] << endl; } chmax(dp[i + 1][j], dp[i][j]); } } /* for (int i = 0; i <= N; i++) { for (int j = 0; j <= W; j++) { cout << dp[i][j] << " "; } cout << endl; }*/ cout << dp[N][W] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll MOD = 1e9 + 7; const ll inf = 1LL << 60; const double eps = 1e-9; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } ll dp[100 + 1][100000 + 1] = {0}; int main() { int N, W; cin >> N >> W; ll w[N], v[N]; for (int i = 0; i < N; i++) cin >> w[i] >> v[i]; // for (int i = 0; i < N; i++) cout << w[i] << v[i] << endl; for (int i = 0; i < N; i++) { for (int j = 0; j <= W; j++) { if (j >= w[i]) { // cout << dp[i][j-w[i]] << endl; chmax(dp[i + 1][j], dp[i][j - w[i]] + v[i]); // cout << dp[i+1][j] << endl; } chmax(dp[i + 1][j], dp[i][j]); } } /* for (int i = 0; i <= N; i++) { for (int j = 0; j <= W; j++) { cout << dp[i][j] << " "; } cout << endl; }*/ cout << dp[N][W] << endl; return 0; }
replace
23
24
23
24
0
p03163
C++
Runtime Error
// This code is written by Shammi Anand #include <bits/stdc++.h> using namespace std; #define ll long long #define Max(a, b, c) max((a),max((b),(c)) #define Min(a, b, c) min(a, min(b, c)) #define pb push_back #define mp make_pair #define f first #define s second #define mod 1000000007 #define nl "\n" #define w(x) \ int x; \ cin >> x; \ while (x--) const int N = 200; ll W[N], V[N], w, n; ll dp[N][N]; ll solve(ll capacity, ll item) { if (capacity == 0 || item == 0) { return 0; } if (dp[item][capacity] != -1) { return dp[item][capacity]; } if (W[item] > capacity) { return dp[item][capacity] = solve(capacity, item - 1); } else { return dp[item][capacity] = max(V[item] + solve(capacity - W[item], item - 1), solve(capacity, item - 1)); } } int main() { cin >> n >> w; for (int i = 1; i <= n; i++) { cin >> W[i] >> V[i]; } memset(dp, -1, sizeof(dp)); cout << solve(w, n) << nl; return 0; }
// This code is written by Shammi Anand #include <bits/stdc++.h> using namespace std; #define ll long long #define Max(a, b, c) max((a),max((b),(c)) #define Min(a, b, c) min(a, min(b, c)) #define pb push_back #define mp make_pair #define f first #define s second #define mod 1000000007 #define nl "\n" #define w(x) \ int x; \ cin >> x; \ while (x--) const int N = 1e5 + 5; ll W[105], V[105], w, n; ll dp[105][N]; ll solve(ll capacity, ll item) { if (capacity == 0 || item == 0) { return 0; } if (dp[item][capacity] != -1) { return dp[item][capacity]; } if (W[item] > capacity) { return dp[item][capacity] = solve(capacity, item - 1); } else { return dp[item][capacity] = max(V[item] + solve(capacity - W[item], item - 1), solve(capacity, item - 1)); } } int main() { cin >> n >> w; for (int i = 1; i <= n; i++) { cin >> W[i] >> V[i]; } memset(dp, -1, sizeof(dp)); cout << solve(w, n) << nl; return 0; }
replace
18
21
18
21
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> #define pii pair<int, int> #define fi first #define se second #define int long long #define pb push_back #define mod 1000000007 #define pll pair<long long, long long> using namespace std; long long power(long long a, long long b) { long long ans = 1; while (b > 0) { if (b & 1) { ans = (ans * a) % mod; } a = (a * a) % mod; b >>= 1; } return ans; } bool prime(int n) { for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } int dp[100005] = {0}; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, w; cin >> n >> w; int a[n], b[n]; for (int i = 0; i < n; i++) { cin >> a[i]; cin >> b[i]; } for (int i = 0; i < n; i++) { for (int j = w; j >= 0; j--) { dp[j + a[i]] = max(dp[j + a[i]], dp[j] + b[i]); } } int ans = 0; for (int i = 0; i <= w; i++) { ans = max(ans, dp[i]); } cout << ans; return 0; }
#include <bits/stdc++.h> #define pii pair<int, int> #define fi first #define se second #define int long long #define pb push_back #define mod 1000000007 #define pll pair<long long, long long> using namespace std; long long power(long long a, long long b) { long long ans = 1; while (b > 0) { if (b & 1) { ans = (ans * a) % mod; } a = (a * a) % mod; b >>= 1; } return ans; } bool prime(int n) { for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } int dp[10000005] = {0}; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, w; cin >> n >> w; int a[n], b[n]; for (int i = 0; i < n; i++) { cin >> a[i]; cin >> b[i]; } for (int i = 0; i < n; i++) { for (int j = w; j >= 0; j--) { dp[j + a[i]] = max(dp[j + a[i]], dp[j] + b[i]); } } int ans = 0; for (int i = 0; i <= w; i++) { ans = max(ans, dp[i]); } cout << ans; return 0; }
replace
27
28
27
28
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; long long knapsack(long long n, long long *w, long long *v, long long W) { long long dp[100][100]; memset(dp, 0, sizeof(dp)); for (long long i = 1; i <= n; i++) { for (long long j = 1; j <= W; j++) { if (j - w[i - 1] >= 0) { dp[i][j] = max(v[i - 1] + dp[i - 1][j - w[i - 1]], dp[i - 1][j]); } else { dp[i][j] = dp[i - 1][j]; } } } // for(long long i=0;i<=n;i++){ // for(long long j=0;j<=W;j++){ // cout<<dp[i][j]<<" "; // } // cout<<endl; // } return dp[n][W]; } int main() { long long n, W; cin >> n >> W; long long w[n]; long long v[n]; for (long long i = 0; i < n; i++) { cin >> w[i]; cin >> v[i]; } // for(long long i=0;i<n;i++){ // cout<<w[i]<<" "; // } cout << knapsack(n, w, v, W); return 0; }
#include <bits/stdc++.h> using namespace std; long long knapsack(long long n, long long *w, long long *v, long long W) { long long dp[n + 1][W + 1]; memset(dp, 0, sizeof(dp)); for (long long i = 1; i <= n; i++) { for (long long j = 1; j <= W; j++) { if (j - w[i - 1] >= 0) { dp[i][j] = max(v[i - 1] + dp[i - 1][j - w[i - 1]], dp[i - 1][j]); } else { dp[i][j] = dp[i - 1][j]; } } } // for(long long i=0;i<=n;i++){ // for(long long j=0;j<=W;j++){ // cout<<dp[i][j]<<" "; // } // cout<<endl; // } return dp[n][W]; } int main() { long long n, W; cin >> n >> W; long long w[n]; long long v[n]; for (long long i = 0; i < n; i++) { cin >> w[i]; cin >> v[i]; } // for(long long i=0;i<n;i++){ // cout<<w[i]<<" "; // } cout << knapsack(n, w, v, W); return 0; }
replace
3
4
3
4
0
p03163
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; using ll = long long; using ld = long double; const double PI = 3.1415926535897932384626433832795; const int dx[] = {0, 1, 0, -1}; const int dy[] = {-1, 0, 1, 0}; int gcd(int x, int y) { return y ? gcd(y, x % y) : abs(x); } ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : abs(x); } int lcm(int x, int y) { return x / gcd(x, y) * y; } ll lcm(ll x, ll y) { return x / gcd(x, y) * y; } int main() { ios::sync_with_stdio(false); cin.tie(0); ll n, W, dp[10001]; fill_n(dp, 10001, -1); cin >> n >> W; vector<ll> v(n), w(n); for (int i = 0; i < n; i++) { cin >> w[i] >> v[i]; } dp[0] = 0; for (int i = 0; i < n; i++) { for (int j = W; j >= w[i]; j--) { dp[j] = max(dp[j], dp[j - w[i]] + v[i]); } } ll ans = *max_element(dp, dp + W + 1); cout << ans << endl; return 0; }
#include "bits/stdc++.h" using namespace std; using ll = long long; using ld = long double; const double PI = 3.1415926535897932384626433832795; const int dx[] = {0, 1, 0, -1}; const int dy[] = {-1, 0, 1, 0}; int gcd(int x, int y) { return y ? gcd(y, x % y) : abs(x); } ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : abs(x); } int lcm(int x, int y) { return x / gcd(x, y) * y; } ll lcm(ll x, ll y) { return x / gcd(x, y) * y; } int main() { ios::sync_with_stdio(false); cin.tie(0); ll n, W, dp[100001]; fill_n(dp, 100001, -1); cin >> n >> W; vector<ll> v(n), w(n); for (int i = 0; i < n; i++) { cin >> w[i] >> v[i]; } dp[0] = 0; for (int i = 0; i < n; i++) { for (int j = W; j >= w[i]; j--) { dp[j] = max(dp[j], dp[j - w[i]] + v[i]); } } ll ans = *max_element(dp, dp + W + 1); cout << ans << endl; return 0; }
replace
21
23
21
23
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> #define REP(i, n) for (ll i = 0; i < (ll)n; i++) #define FOR(i, a, b) for (ll i = (a); i < (ll)b; i++) #define ALL(obj) (obj).begin(), (obj).end() #define INF 1000000000000000 template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } using namespace std; typedef long long ll; typedef double db; typedef string str; typedef pair<ll, ll> p; const int MOD = 1000000007; void print(const std::vector<int> &v) { std::for_each(v.begin(), v.end(), [](int x) { std::cout << x << " "; }); std::cout << std::endl; } // dp[i][sum] i-1番目の品物をつかって重さがsumを超えないように選んだ時の // 価値の最大値 int main() { int N, W; cin >> N >> W; vector<int> weight(N), value(N); vector<vector<ll>> dp(N + 1, vector<ll>(W, 0)); REP(i, N) { cin >> weight[i] >> value[i]; } REP(i, N) { REP(sum_w, W + 1) { if (sum_w - weight[i] >= 0) { chmax(dp[i + 1][sum_w], dp[i][sum_w - weight[i]] + value[i]); } chmax(dp[i + 1][sum_w], dp[i][sum_w]); } } cout << dp[N][W] << endl; return 0; }
#include <bits/stdc++.h> #define REP(i, n) for (ll i = 0; i < (ll)n; i++) #define FOR(i, a, b) for (ll i = (a); i < (ll)b; i++) #define ALL(obj) (obj).begin(), (obj).end() #define INF 1000000000000000 template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } using namespace std; typedef long long ll; typedef double db; typedef string str; typedef pair<ll, ll> p; const int MOD = 1000000007; void print(const std::vector<int> &v) { std::for_each(v.begin(), v.end(), [](int x) { std::cout << x << " "; }); std::cout << std::endl; } // dp[i][sum] i-1番目の品物をつかって重さがsumを超えないように選んだ時の // 価値の最大値 int main() { int N, W; cin >> N >> W; vector<int> weight(N), value(N); vector<vector<ll>> dp(N + 1, vector<ll>(W + 1, 0)); REP(i, N) { cin >> weight[i] >> value[i]; } REP(i, N) { REP(sum_w, W + 1) { if (sum_w - weight[i] >= 0) { chmax(dp[i + 1][sum_w], dp[i][sum_w - weight[i]] + value[i]); } chmax(dp[i + 1][sum_w], dp[i][sum_w]); } } cout << dp[N][W] << endl; return 0; }
replace
37
38
37
38
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 101; const int maxw = 1e5 + 1; ll w[maxn], v[maxn], dp[4][9]; int main() { #ifdef LOCAL freopen("data.txt", "r", stdin); #endif ll n, W; scanf("%lld%lld", &n, &W); for (int i = 1; i <= n; ++i) scanf("%lld%lld", &w[i], &v[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]); printf("%lld\n", dp[n][W]); }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 101; const int maxw = 1e5 + 1; ll w[maxn], v[maxn], dp[maxn][maxw]; int main() { #ifdef LOCAL freopen("data.txt", "r", stdin); #endif ll n, W; scanf("%lld%lld", &n, &W); for (int i = 1; i <= n; ++i) scanf("%lld%lld", &w[i], &v[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]); printf("%lld\n", dp[n][W]); }
replace
6
7
6
7
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using Field = vector<vector<int>>; using Graph = vector<vector<int>>; using VI = vector<int>; using VC = vector<char>; using PI = pair<int, int>; #define REP(i, n) for (int i = 0; i < (n); i++) #define ALL(x) x.begin(), x.end() const long long INF = 1LL << 60; const int mod = 1000000007; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } int N, W; vector<ll> w(110); vector<ll> v(110); vector<vector<ll>> dp(110, vector<ll>(110, 0)); int main() { cin.tie(0); ios_base::sync_with_stdio(false); cin >> N >> W; REP(i, N) cin >> w.at(i) >> v.at(i); dp.at(0).at(0) = 0; for (int i = 0; i < N; i++) { for (int sum_w = 0; sum_w <= W; sum_w++) { if (sum_w < w.at(i)) { chmax(dp.at(i + 1).at(sum_w), dp.at(i).at(sum_w)); } else { chmax(dp.at(i + 1).at(sum_w), dp.at(i).at(sum_w)); chmax(dp.at(i + 1).at(sum_w), dp.at(i).at(sum_w - w.at(i)) + v.at(i)); } } } cout << dp.at(N).at(W) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using Field = vector<vector<int>>; using Graph = vector<vector<int>>; using VI = vector<int>; using VC = vector<char>; using PI = pair<int, int>; #define REP(i, n) for (int i = 0; i < (n); i++) #define ALL(x) x.begin(), x.end() const long long INF = 1LL << 60; const int mod = 1000000007; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } int N, W; vector<ll> w(110); vector<ll> v(110); vector<vector<ll>> dp(110, vector<ll>(100100, 0)); int main() { cin.tie(0); ios_base::sync_with_stdio(false); cin >> N >> W; REP(i, N) cin >> w.at(i) >> v.at(i); dp.at(0).at(0) = 0; for (int i = 0; i < N; i++) { for (int sum_w = 0; sum_w <= W; sum_w++) { if (sum_w < w.at(i)) { chmax(dp.at(i + 1).at(sum_w), dp.at(i).at(sum_w)); } else { chmax(dp.at(i + 1).at(sum_w), dp.at(i).at(sum_w)); chmax(dp.at(i + 1).at(sum_w), dp.at(i).at(sum_w - w.at(i)) + v.at(i)); } } } cout << dp.at(N).at(W) << endl; return 0; }
replace
36
37
36
37
0
p03163
C++
Memory Limit Exceeded
#include <algorithm> #include <assert.h> #include <bitset> #include <complex> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i) #define rep(i, n) REP(i, 0, n) using ll = long long; const int inf = 1e9 + 7; const ll longinf = 1LL << 60; const ll mod = 1e9 + 7; struct edge { int to; ll cap; int rev; }; int main() { ll N, W; cin >> N >> W; vector<ll> w(N), v(N); rep(i, N) cin >> w[i] >> v[i]; vector<vector<ll>> dp(2, vector<ll>(1e8 + 5)); rep(i, 2) rep(j, 100010) dp[i][j] = -inf; int crt = 0, nxt = 1; dp[crt][0] = 0; rep(i, N) { rep(j, 100010) dp[nxt][j] = -inf; rep(j, W + 1) { if (dp[crt][j] == -inf) continue; if (j + w[i] <= W) dp[nxt][j + w[i]] = max(dp[nxt][j + w[i]], dp[crt][j] + v[i]); dp[nxt][j] = max(dp[nxt][j], dp[crt][j]); } swap(crt, nxt); } ll res = -inf; rep(j, W + 1) res = max(res, dp[crt][j]); cout << res << endl; return 0; }
#include <algorithm> #include <assert.h> #include <bitset> #include <complex> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i) #define rep(i, n) REP(i, 0, n) using ll = long long; const int inf = 1e9 + 7; const ll longinf = 1LL << 60; const ll mod = 1e9 + 7; struct edge { int to; ll cap; int rev; }; int main() { ll N, W; cin >> N >> W; vector<ll> w(N), v(N); rep(i, N) cin >> w[i] >> v[i]; vector<vector<ll>> dp(2, vector<ll>(100010)); rep(i, 2) rep(j, 100010) dp[i][j] = -inf; int crt = 0, nxt = 1; dp[crt][0] = 0; rep(i, N) { rep(j, 100010) dp[nxt][j] = -inf; rep(j, W + 1) { if (dp[crt][j] == -inf) continue; if (j + w[i] <= W) dp[nxt][j + w[i]] = max(dp[nxt][j + w[i]], dp[crt][j] + v[i]); dp[nxt][j] = max(dp[nxt][j], dp[crt][j]); } swap(crt, nxt); } ll res = -inf; rep(j, W + 1) res = max(res, dp[crt][j]); cout << res << endl; return 0; }
replace
34
35
34
35
MLE
p03163
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long int using namespace std; #define mod 1000000007 #define pb push_back #define fi first #define si second #define mp make_pair #define f(n) for (ll i = 0; i < n; i++) #define ff(m) for (ll j = 0; j < m; j++) #define fr(i, p, n) for (ll i = p; i < n; i++) #define in insert ll exp(ll a, ll b, ll m); #define vl vector<ll> #define pll pair<ll, ll> void solve() { int n, W; cin >> n >> W; vl v(n + 1, 0); vl w(n + 1, 0); fr(i, 1, n + 1) { cin >> w[i] >> v[i]; } ll dp[n + 1][n + 1]; memset(dp, 0, sizeof(dp)); fr(i, 1, n + 1) { fr(j, 1, W + 1) { if (j < w[i]) dp[i][j] = dp[i - 1][j]; else dp[i][j] = max(dp[i - 1][j], v[i] + dp[i - 1][j - w[i]]); } } // dp(i,j)=max(dp(i-1,j),v[i]+dp(i-1,j-w[i])) cout << dp[n][W] << endl; } int main() { int t = 1; // cin>>t; while (t--) solve(); return 0; } ll exp(ll a, ll b, ll m) { if (b == 0) { return 1; } ll temp = exp(a, b / 2, m); temp = (temp * temp) % m; if (b & 1) { return (temp * (a % m)) % m; } return temp; }
#include <bits/stdc++.h> #define ll long long int using namespace std; #define mod 1000000007 #define pb push_back #define fi first #define si second #define mp make_pair #define f(n) for (ll i = 0; i < n; i++) #define ff(m) for (ll j = 0; j < m; j++) #define fr(i, p, n) for (ll i = p; i < n; i++) #define in insert ll exp(ll a, ll b, ll m); #define vl vector<ll> #define pll pair<ll, ll> void solve() { int n, W; cin >> n >> W; vl v(n + 1, 0); vl w(n + 1, 0); fr(i, 1, n + 1) { cin >> w[i] >> v[i]; } ll dp[n + 1][W + 1]; memset(dp, 0, sizeof(dp)); fr(i, 1, n + 1) { fr(j, 1, W + 1) { if (j < w[i]) dp[i][j] = dp[i - 1][j]; else dp[i][j] = max(dp[i - 1][j], v[i] + dp[i - 1][j - w[i]]); } } // dp(i,j)=max(dp(i-1,j),v[i]+dp(i-1,j-w[i])) cout << dp[n][W] << endl; } int main() { int t = 1; // cin>>t; while (t--) solve(); return 0; } ll exp(ll a, ll b, ll m) { if (b == 0) { return 1; } ll temp = exp(a, b / 2, m); temp = (temp * temp) % m; if (b & 1) { return (temp * (a % m)) % m; } return temp; }
replace
25
26
25
26
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define MM 1000000000 #define MOD MM + 7 #define MAX 101000 #define MAP 110 #define initial_value -1 #define Pair pair<int, int> #define lPair pair<ll, ll> #define chmax(a, b) (a < b ? a = b : 0) #define chmin(a, b) (a > b ? a = b : 0) int dx[4] = {-1, 0, 1, 0}; int dy[4] = {0, -1, 0, 1}; ll N, W; ll w[101], v[101]; int main() { cin >> N >> W; for (ll i = 1; i <= N; i++) cin >> w[i] >> v[i]; ll dp[101][100100] = {0}; dp[0][0] = 0; for (ll i = 1; i <= N; i++) { for (ll j = 0; j <= W; j++) { dp[i + 1][j] = dp[i][j]; if (j >= w[i + 1]) { dp[i + 1][j] = max(dp[i + 1][j], dp[i][j - w[i + 1]] + v[i + 1]); } else { dp[i + 1][j] = dp[i][j]; } } } ll mx = 0; for (ll i = 0; i <= W; i++) { mx = max(mx, dp[N][i]); } cout << mx << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define MM 1000000000 #define MOD MM + 7 #define MAX 101000 #define MAP 110 #define initial_value -1 #define Pair pair<int, int> #define lPair pair<ll, ll> #define chmax(a, b) (a < b ? a = b : 0) #define chmin(a, b) (a > b ? a = b : 0) int dx[4] = {-1, 0, 1, 0}; int dy[4] = {0, -1, 0, 1}; ll N, W; ll w[101], v[101]; int main() { cin >> N >> W; for (ll i = 1; i <= N; i++) cin >> w[i] >> v[i]; ll dp[101][100100] = {0}; dp[0][0] = 0; for (ll i = 0; i < N; i++) { for (ll j = 0; j <= W; j++) { dp[i + 1][j] = dp[i][j]; if (j >= w[i + 1]) { dp[i + 1][j] = max(dp[i + 1][j], dp[i][j - w[i + 1]] + v[i + 1]); } else { dp[i + 1][j] = dp[i][j]; } } } ll mx = 0; for (ll i = 0; i <= W; i++) { mx = max(mx, dp[N][i]); } cout << mx << endl; }
replace
23
24
23
24
-11
p03163
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstring> #include <functional> #include <iostream> #include <map> #include <math.h> #include <queue> #include <stack> #include <string> #include <unordered_map> #include <utility> #include <vector> using namespace std; /**** Type Define ****/ typedef long long ll; typedef pair<ll, ll> P; typedef pair<ll, P> Q; /**** Const List ****/ const ll INF = 1LL << 60; const ll mod = 1000000007; const ll dx[4] = {1, 0, -1, 0}; const ll dy[4] = {0, -1, 0, 1}; /**** General Functions ****/ ll RepeatSquaring(ll N, ll P, ll M) { if (P == 0) return 1; if (P % 2 == 0) { ll t = RepeatSquaring(N, P / 2, M); return t * t % M; } return N * RepeatSquaring(N, P - 1, M); } bool IsPrime(ll a) { // order root a if (a == 1) return false; for (int i = 2; i * i <= a; i++) { if (a % i == 0 && a != i) { return false; } } return true; } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll extgcd(ll a, ll b, ll &x, ll &y) { if (b == 0) { x = 1, y = 0; return a; } ll q = a / b, g = extgcd(b, a - q * b, x, y); ll z = x - q * y; x = y; y = z; return g; } ll invmod(ll a, ll m) { // a^-1 mod m ll x, y; extgcd(a, m, x, y); x %= m; if (x < 0) x += m; return x; } ll nCk(ll n, ll k, ll mod) { ll ans = 1; for (ll i = n, j = 1; j <= k; i--, j++) ans = (((ans * i) % mod) * invmod(j, mod)) % mod; return ans; } ll lmin(ll a, ll b) { return a > b ? b : a; }; ll lmax(ll a, ll b) { return a > b ? a : b; }; ll lsum(ll a, ll b) { return a + b; }; /**** Zip ****/ template <typename T> class Zip { vector<T> d; bool flag; public: Zip() { flag = false; } void add(T x) { d.push_back(x); flag = true; } ll getNum(T x) { // T need to have operator < !! if (flag) { sort(d.begin(), d.end()); d.erase(unique(d.begin(), d.end()), d.end()); flag = false; } return lower_bound(d.begin(), d.end(), x) - d.begin(); } ll size() { if (flag) { sort(d.begin(), d.end()); d.erase(unique(d.begin(), d.end()), d.end()); flag = false; } return (ll)d.size(); } }; /**** Union Find ****/ class UnionFind { vector<ll> par, rank; // par > 0: number, par < 0: -par public: void init(ll n) { par.resize(n, 1); rank.resize(n, 0); } ll getSize(ll x) { return par[find(x)]; } ll find(ll x) { if (par[x] > 0) return x; return -(par[x] = -find(-par[x])); } void merge(ll x, ll y) { x = find(x); y = find(y); if (x == y) return; if (rank[x] < rank[y]) { par[y] += par[x]; par[x] = -y; } else { par[x] += par[y]; par[y] = -x; if (rank[x] == rank[y]) rank[x]++; } } bool isSame(ll x, ll y) { return find(x) == find(y); } }; /**** Segment Tree ****/ template <typename T> class SegmentTree { ll n; vector<T> node; function<T(T, T)> fun, fun2; bool customChange; T outValue, initValue; public: void init(ll num, function<T(T, T)> resultFunction, T init, T out, function<T(T, T)> changeFunction = NULL) { // changeFunction: (input, beforevalue) => newvalue fun = resultFunction; fun2 = changeFunction; customChange = changeFunction != NULL; n = 1; while (n < num) n *= 2; node.resize(2 * n - 1, init); outValue = out; initValue = init; } void valueChange(ll num, T value) { num += n - 1; if (customChange) node[num] = fun2(value, node[num]); else node[num] = value; while (num > 0) num = (num - 1) / 2, node[num] = fun(node[num * 2 + 1], node[num * 2 + 2]); } T rangeQuery(ll a, ll b, ll l = 0, ll r = -1, ll k = 0) { // [a, b) if (r == -1) r = n; if (a <= l && r <= b) return node[k]; if (b <= l || r <= a) return outValue; ll mid = (l + r) / 2; return fun(rangeQuery(a, b, l, mid, 2 * k + 1), rangeQuery(a, b, mid, r, 2 * k + 2)); } }; /**** LIS ****/ ll lis(ll *a, ll n, ll *dp) { fill(dp, dp + n, INF); // INFを代入 for (ll i = 0; i < n; i++) *lower_bound(dp, dp + n, a[i]) = a[i]; return (ll)(lower_bound(dp, dp + n, INF) - dp); } /**** main function ****/ ll n, ww; ll dp[100][100001]; // dp[i][j]: // (インデックス的に)i-1こめまで見て、重さの総和がj以下となるように選んだ時の価値の総和の最大値 ll w[101]; // 重さ ll v[101]; // 価値 int main() { cin >> n >> ww; for (int i = 0; i < n; i++) { cin >> w[i] >> v[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j <= ww; j++) { if (j < w[i]) { // はいらへん dp[i + 1][j] = dp[i][j]; } else { dp[i + 1][j] = lmax(dp[i][j], dp[i][j - w[i]] + v[i]); } } } cout << dp[n][ww] << endl; }
#include <algorithm> #include <cstdio> #include <cstring> #include <functional> #include <iostream> #include <map> #include <math.h> #include <queue> #include <stack> #include <string> #include <unordered_map> #include <utility> #include <vector> using namespace std; /**** Type Define ****/ typedef long long ll; typedef pair<ll, ll> P; typedef pair<ll, P> Q; /**** Const List ****/ const ll INF = 1LL << 60; const ll mod = 1000000007; const ll dx[4] = {1, 0, -1, 0}; const ll dy[4] = {0, -1, 0, 1}; /**** General Functions ****/ ll RepeatSquaring(ll N, ll P, ll M) { if (P == 0) return 1; if (P % 2 == 0) { ll t = RepeatSquaring(N, P / 2, M); return t * t % M; } return N * RepeatSquaring(N, P - 1, M); } bool IsPrime(ll a) { // order root a if (a == 1) return false; for (int i = 2; i * i <= a; i++) { if (a % i == 0 && a != i) { return false; } } return true; } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll extgcd(ll a, ll b, ll &x, ll &y) { if (b == 0) { x = 1, y = 0; return a; } ll q = a / b, g = extgcd(b, a - q * b, x, y); ll z = x - q * y; x = y; y = z; return g; } ll invmod(ll a, ll m) { // a^-1 mod m ll x, y; extgcd(a, m, x, y); x %= m; if (x < 0) x += m; return x; } ll nCk(ll n, ll k, ll mod) { ll ans = 1; for (ll i = n, j = 1; j <= k; i--, j++) ans = (((ans * i) % mod) * invmod(j, mod)) % mod; return ans; } ll lmin(ll a, ll b) { return a > b ? b : a; }; ll lmax(ll a, ll b) { return a > b ? a : b; }; ll lsum(ll a, ll b) { return a + b; }; /**** Zip ****/ template <typename T> class Zip { vector<T> d; bool flag; public: Zip() { flag = false; } void add(T x) { d.push_back(x); flag = true; } ll getNum(T x) { // T need to have operator < !! if (flag) { sort(d.begin(), d.end()); d.erase(unique(d.begin(), d.end()), d.end()); flag = false; } return lower_bound(d.begin(), d.end(), x) - d.begin(); } ll size() { if (flag) { sort(d.begin(), d.end()); d.erase(unique(d.begin(), d.end()), d.end()); flag = false; } return (ll)d.size(); } }; /**** Union Find ****/ class UnionFind { vector<ll> par, rank; // par > 0: number, par < 0: -par public: void init(ll n) { par.resize(n, 1); rank.resize(n, 0); } ll getSize(ll x) { return par[find(x)]; } ll find(ll x) { if (par[x] > 0) return x; return -(par[x] = -find(-par[x])); } void merge(ll x, ll y) { x = find(x); y = find(y); if (x == y) return; if (rank[x] < rank[y]) { par[y] += par[x]; par[x] = -y; } else { par[x] += par[y]; par[y] = -x; if (rank[x] == rank[y]) rank[x]++; } } bool isSame(ll x, ll y) { return find(x) == find(y); } }; /**** Segment Tree ****/ template <typename T> class SegmentTree { ll n; vector<T> node; function<T(T, T)> fun, fun2; bool customChange; T outValue, initValue; public: void init(ll num, function<T(T, T)> resultFunction, T init, T out, function<T(T, T)> changeFunction = NULL) { // changeFunction: (input, beforevalue) => newvalue fun = resultFunction; fun2 = changeFunction; customChange = changeFunction != NULL; n = 1; while (n < num) n *= 2; node.resize(2 * n - 1, init); outValue = out; initValue = init; } void valueChange(ll num, T value) { num += n - 1; if (customChange) node[num] = fun2(value, node[num]); else node[num] = value; while (num > 0) num = (num - 1) / 2, node[num] = fun(node[num * 2 + 1], node[num * 2 + 2]); } T rangeQuery(ll a, ll b, ll l = 0, ll r = -1, ll k = 0) { // [a, b) if (r == -1) r = n; if (a <= l && r <= b) return node[k]; if (b <= l || r <= a) return outValue; ll mid = (l + r) / 2; return fun(rangeQuery(a, b, l, mid, 2 * k + 1), rangeQuery(a, b, mid, r, 2 * k + 2)); } }; /**** LIS ****/ ll lis(ll *a, ll n, ll *dp) { fill(dp, dp + n, INF); // INFを代入 for (ll i = 0; i < n; i++) *lower_bound(dp, dp + n, a[i]) = a[i]; return (ll)(lower_bound(dp, dp + n, INF) - dp); } /**** main function ****/ ll n, ww; ll dp[101][100001]; // dp[i][j]: // (インデックス的に)i-1こめまで見て、重さの総和がj以下となるように選んだ時の価値の総和の最大値 ll w[101]; // 重さ ll v[101]; // 価値 int main() { cin >> n >> ww; for (int i = 0; i < n; i++) { cin >> w[i] >> v[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j <= ww; j++) { if (j < w[i]) { // はいらへん dp[i + 1][j] = dp[i][j]; } else { dp[i + 1][j] = lmax(dp[i][j], dp[i][j - w[i]] + v[i]); } } } cout << dp[n][ww] << endl; }
replace
208
209
208
209
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long int #define pb push_back #define ub upper_bound #define lb lower_bound #define vi vector<int> #define sz(s) (int)s.size() #define pii pair<int, int> #define piii pair<int, pair<int, int>> #define tiii tuple<int, int, int> #define ff first #define ss second #define all(a) a.begin(), a.end() #define rall(v) v.rbegin(), v.rend() #define endl '\n' #define matrix vector<vector<int>> #define boost \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define NAYAN \ freopen("input.txt", "r", stdin); \ freopen("output.txt", "w", stdout); #define N 500006 #define mod 1000000007 #define mod2 998244353 #define inf 1e18 vector<pii> edges; set<int> st[N]; int32_t main() { boost #ifndef ONLINE_JUDGE NAYAN #endif int n, W; cin >> n >> W; int dp[W + 1]; memset(dp, 0, sizeof dp); int i, j; int w[n], v[n]; for (i = 0; i < n; i++) { cin >> w[i] >> v[i]; } for (i = 0; i < n; i++) { for (j = W; j >= w[i]; j--) { dp[j] = max(dp[j], v[i] + dp[j - w[i]]); } } cout << dp[W] << endl; }
#include <bits/stdc++.h> using namespace std; #define int long long int #define pb push_back #define ub upper_bound #define lb lower_bound #define vi vector<int> #define sz(s) (int)s.size() #define pii pair<int, int> #define piii pair<int, pair<int, int>> #define tiii tuple<int, int, int> #define ff first #define ss second #define all(a) a.begin(), a.end() #define rall(v) v.rbegin(), v.rend() #define endl '\n' #define matrix vector<vector<int>> #define boost \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define NAYAN \ freopen("input.txt", "r", stdin); \ freopen("output.txt", "w", stdout); #define N 500006 #define mod 1000000007 #define mod2 998244353 #define inf 1e18 vector<pii> edges; set<int> st[N]; int32_t main() { boost // #ifndef ONLINE_JUDGE // NAYAN // #endif int n, W; cin >> n >> W; int dp[W + 1]; memset(dp, 0, sizeof dp); int i, j; int w[n], v[n]; for (i = 0; i < n; i++) { cin >> w[i] >> v[i]; } for (i = 0; i < n; i++) { for (j = W; j >= w[i]; j--) { dp[j] = max(dp[j], v[i] + dp[j - w[i]]); } } cout << dp[W] << endl; }
replace
35
38
35
38
-11
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long int int n, capc; int dp[102][10005]; int fill_knapsack(int i, int w, int val[], int wt[]) { if (i < 0) return 0; if (dp[i][w] != -1) return dp[i][w]; if (wt[i] > w) { dp[i][w] = fill_knapsack(i - 1, w, val, wt); } else { dp[i][w] = max(val[i] + fill_knapsack(i - 1, w - wt[i], val, wt), fill_knapsack(i - 1, w, val, wt)); } return dp[i][w]; } void solve() { cin >> n >> capc; int val[n]; int wt[n]; for (int i = 0; i < n; i++) cin >> wt[i] >> val[i]; memset(dp, -1, sizeof(dp)); cout << fill_knapsack(n - 1, capc, val, wt) << endl; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); solve(); }
#include <bits/stdc++.h> using namespace std; #define int long long int int n, capc; int dp[102][100005]; int fill_knapsack(int i, int w, int val[], int wt[]) { if (i < 0) return 0; if (dp[i][w] != -1) return dp[i][w]; if (wt[i] > w) { dp[i][w] = fill_knapsack(i - 1, w, val, wt); } else { dp[i][w] = max(val[i] + fill_knapsack(i - 1, w - wt[i], val, wt), fill_knapsack(i - 1, w, val, wt)); } return dp[i][w]; } void solve() { cin >> n >> capc; int val[n]; int wt[n]; for (int i = 0; i < n; i++) cin >> wt[i] >> val[i]; memset(dp, -1, sizeof(dp)); cout << fill_knapsack(n - 1, capc, val, wt) << endl; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); solve(); }
replace
5
6
5
6
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long int ll; ll weights[105]; ll values[105]; ll knap[205][205]; bool isPrime(ll n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (ll i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } int main() { ios::sync_with_stdio(false); ll i, j, k, l, m, n, w; cin >> n >> w; for (i = 0; i < n; i++) { cin >> weights[i] >> values[i]; } for (i = 0; i <= n; i++) { for (j = 0; j <= w; j++) { if (i == 0 || j == 0) knap[i][j] = 0; else if (weights[i - 1] <= j) { knap[i][j] = max(values[i - 1] + knap[i - 1][j - weights[i - 1]], knap[i - 1][j]); } else knap[i][j] = knap[i - 1][j]; // cout << knap[i][j] << " "; } // cout << '\n'; } cout << knap[n][w] << '\n'; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; ll weights[105]; ll values[105]; ll knap[205][100005]; bool isPrime(ll n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (ll i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } int main() { ios::sync_with_stdio(false); ll i, j, k, l, m, n, w; cin >> n >> w; for (i = 0; i < n; i++) { cin >> weights[i] >> values[i]; } for (i = 0; i <= n; i++) { for (j = 0; j <= w; j++) { if (i == 0 || j == 0) knap[i][j] = 0; else if (weights[i - 1] <= j) { knap[i][j] = max(values[i - 1] + knap[i - 1][j - weights[i - 1]], knap[i - 1][j]); } else knap[i][j] = knap[i - 1][j]; // cout << knap[i][j] << " "; } // cout << '\n'; } cout << knap[n][w] << '\n'; }
replace
8
9
8
9
0
p03163
C++
Runtime Error
#include <cstring> #include <iostream> using namespace std; long long wt[110]; long long val[110]; long long dp[110][110]; long long n, w; long long knapsack(long long wt[], long long val[], long long n, long long w) { if (n == 0 || w == 0) return 0; if (dp[n][w] != -1) return dp[n][w]; if (wt[n - 1] <= w) { return dp[n][w] = max(knapsack(wt, val, n - 1, w), val[n - 1] + knapsack(wt, val, n - 1, w - wt[n - 1])); } else { return dp[n][w] = knapsack(wt, val, n - 1, w); } } int main() { cin >> n >> w; for (long long i = 0; i < n; i++) { cin >> wt[i] >> val[i]; } /*for (int i = 0; i < n; i++) { cin >> val[i]; }*/ memset(dp, -1, sizeof(dp)); cout << knapsack(wt, val, n, w); return 0; }
#include <cstring> #include <iostream> using namespace std; long long wt[110]; long long val[110]; long long dp[101][100005]; long long n, w; long long knapsack(long long wt[], long long val[], long long n, long long w) { if (n == 0 || w == 0) return 0; if (dp[n][w] != -1) return dp[n][w]; if (wt[n - 1] <= w) { return dp[n][w] = max(knapsack(wt, val, n - 1, w), val[n - 1] + knapsack(wt, val, n - 1, w - wt[n - 1])); } else { return dp[n][w] = knapsack(wt, val, n - 1, w); } } int main() { cin >> n >> w; for (long long i = 0; i < n; i++) { cin >> wt[i] >> val[i]; } /*for (int i = 0; i < n; i++) { cin >> val[i]; }*/ memset(dp, -1, sizeof(dp)); cout << knapsack(wt, val, n, w); return 0; }
replace
5
6
5
6
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define mp make_pair bool comp(pair<int, int> a, pair<int, int> b) { return (a.first < b.first); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); #ifndef ONLINE_JUDGE // for getting input from input.txt freopen("input.txt", "r+", stdin); // for writing output to output.txt freopen("output.txt", "w+", stdout); #endif int n; int w; cin >> n >> w; vector<pair<int, int>> ele(n); for (int i = 0, a, b; i < n; i++) { cin >> a >> b; ele[i] = mp(a, b); } sort(ele.begin(), ele.end(), comp); // for(int i=0;i<n;i++) { // cout << ele[i].first << " " << ele[i].second << '\n'; // } ll dp[n + 1][w + 1]; for (int i = 0; i <= n; i++) { dp[i][0] = 0; } for (int i = 0; i <= w; i++) { dp[0][i] = 0; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= w; j++) { pair<int, int> obj = ele[i - 1]; int wt = obj.first; int val = obj.second; if (j >= wt) { dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - wt] + val); } else { dp[i][j] = dp[i - 1][j]; } } } // for(int i=0;i<=n;i++) { // for(int j=0;j<=w;j++) { // cout << dp[i][j] << " "; // } // cout << '\n'; // } cout << dp[n][w] << '\n'; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define mp make_pair bool comp(pair<int, int> a, pair<int, int> b) { return (a.first < b.first); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); /*#ifndef ONLINE_JUDGE // for getting input from input.txt freopen("input.txt", "r+", stdin); // for writing output to output.txt freopen("output.txt", "w+", stdout); #endif*/ int n; int w; cin >> n >> w; vector<pair<int, int>> ele(n); for (int i = 0, a, b; i < n; i++) { cin >> a >> b; ele[i] = mp(a, b); } sort(ele.begin(), ele.end(), comp); // for(int i=0;i<n;i++) { // cout << ele[i].first << " " << ele[i].second << '\n'; // } ll dp[n + 1][w + 1]; for (int i = 0; i <= n; i++) { dp[i][0] = 0; } for (int i = 0; i <= w; i++) { dp[0][i] = 0; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= w; j++) { pair<int, int> obj = ele[i - 1]; int wt = obj.first; int val = obj.second; if (j >= wt) { dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - wt] + val); } else { dp[i][j] = dp[i - 1][j]; } } } // for(int i=0;i<=n;i++) { // for(int j=0;j<=w;j++) { // cout << dp[i][j] << " "; // } // cout << '\n'; // } cout << dp[n][w] << '\n'; }
replace
13
19
13
19
-6
terminate called after throwing an instance of 'std::length_error' what(): cannot create std::vector larger than max_size()
p03163
C++
Runtime Error
/*author : Yashvardhan Saturday, January 12, 2019 */ #include <bits/stdc++.h> using namespace std; typedef long long ll; #define int ll #define pb push_back #define vi vector<int> #define pi pair<int, int> #define vpi vector<pi> #define ff first #define ss second #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("O3") #pragma GCC optimize("O2") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define _CRT_SECURE_NO_WARNINGS #ifdef __APPLE__ #define debug(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " "; __f(comma + 1, args...); } #else #define debug(...) #endif template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (int i = 0; i < v.size(); ++i) os << v[i] << " "; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { for (auto it : v) os << it << " "; return os; } template <typename T, typename S> ostream &operator<<(ostream &os, const pair<T, S> &v) { os << v.ff << " " << v.ss; return os; } const int mod = 1e9 + 7; const int inf = 2e18; const int ninf = -2e18; int dp[101][100005]; int wi[101]; int vs[101]; int pow(int a, int b, int m) { int ans = 1; while (b) { if (b & 1) ans = (ans * a) % m; b /= 2; a = (a * a) % m; } return ans; } int solve(int n, int w) { if (dp[n][w] != -1) return dp[n][w]; if (w == 0) return dp[n][w] = 0; if (wi[n] > w) return dp[n][w] = solve(n - 1, w); else return dp[n][w] = max(solve(n - 1, w), vs[n] + solve(n - 1, w - wi[n])); } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifdef __APPLE__ freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("error.txt", "w", stderr); #endif time_t t1, t2; t1 = clock(); int n, w1; cin >> n >> w1; for (int i = 1; i <= n; i++) { int w, v; cin >> w >> v; wi[i] = w; vs[i] = v; } for (int i = 0; i < 101; i++) { for (int j = 0; j <= 100000; j++) { dp[i][j] = -1; } } cout << solve(n, w1) << endl; t2 = clock(); cerr << endl << t2 - t1 << endl; return 0; }
/*author : Yashvardhan Saturday, January 12, 2019 */ #include <bits/stdc++.h> using namespace std; typedef long long ll; #define int ll #define pb push_back #define vi vector<int> #define pi pair<int, int> #define vpi vector<pi> #define ff first #define ss second #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("O3") #pragma GCC optimize("O2") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define _CRT_SECURE_NO_WARNINGS #ifdef __APPLE__ #define debug(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " "; __f(comma + 1, args...); } #else #define debug(...) #endif template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (int i = 0; i < v.size(); ++i) os << v[i] << " "; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { for (auto it : v) os << it << " "; return os; } template <typename T, typename S> ostream &operator<<(ostream &os, const pair<T, S> &v) { os << v.ff << " " << v.ss; return os; } const int mod = 1e9 + 7; const int inf = 2e18; const int ninf = -2e18; int dp[101][100005]; int wi[101]; int vs[101]; int pow(int a, int b, int m) { int ans = 1; while (b) { if (b & 1) ans = (ans * a) % m; b /= 2; a = (a * a) % m; } return ans; } int solve(int n, int w) { if (dp[n][w] != -1) return dp[n][w]; if (n == 0) return dp[n][w] = 0; if (wi[n] > w) return dp[n][w] = solve(n - 1, w); else return dp[n][w] = max(solve(n - 1, w), vs[n] + solve(n - 1, w - wi[n])); } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifdef __APPLE__ freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("error.txt", "w", stderr); #endif time_t t1, t2; t1 = clock(); int n, w1; cin >> n >> w1; for (int i = 1; i <= n; i++) { int w, v; cin >> w >> v; wi[i] = w; vs[i] = v; } for (int i = 0; i < 101; i++) { for (int j = 0; j <= 100000; j++) { dp[i][j] = -1; } } cout << solve(n, w1) << endl; t2 = clock(); cerr << endl << t2 - t1 << endl; return 0; }
replace
77
78
77
78
-11
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define REP(i, a) for (int i = 0; i < (a); i++) #define ALL(a) (a).begin(), (a).end() typedef long long ll; typedef pair<int, int> P; const int INF = 1e9; const int MOD = 1e9 + 7; #define MAX_N 100 #define MAX_W 100000 ll dp[MAX_N][MAX_W]; signed main() { int N, W; cin >> N >> W; int w[N]; ll v[N]; REP(i, N) { cin >> w[i] >> v[i]; } 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] < 0) { continue; } dp[i + 1][j] = max(dp[i + 1][j], dp[i][j - w[i]] + v[i]); } } cout << dp[N][W] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i, a) for (int i = 0; i < (a); i++) #define ALL(a) (a).begin(), (a).end() typedef long long ll; typedef pair<int, int> P; const int INF = 1e9; const int MOD = 1e9 + 7; #define MAX_N 100 #define MAX_W 100000 ll dp[MAX_N + 1][MAX_W + 1]; signed main() { int N, W; cin >> N >> W; int w[N]; ll v[N]; REP(i, N) { cin >> w[i] >> v[i]; } 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] < 0) { continue; } dp[i + 1][j] = max(dp[i + 1][j], dp[i][j - w[i]] + v[i]); } } cout << dp[N][W] << endl; return 0; }
replace
12
13
12
13
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define in(a) \ ll a; \ cin >> a; #define in2(a, b) \ ll a, b; \ cin >> a >> b; #define in3(a, b, c) \ ll a, b, c; \ cin >> a >> b >> c; #define in4(a, b, c, d) \ ll a, b, c, d; \ cin >> a >> b >> c >> d; #define in5(a, b, c, d, e) \ ll a, b, c, d, e; \ cin >> a >> b >> c >> d >> e; #define lelo(b) \ for (auto &i : b) \ cin >> i; #define ff first #define ss second #define call(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define show(x) \ for (auto t : x) \ cout << t << " "; #define pb push_back #define bhar(s, v) memset(s, v, sizeof(s)) #define endl "\n" #define ce(x) cout << x << "\n"; #define nl cout << endl; #define jaldi \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define dekh_lo ce("dekh_lo"); #define sz(x) (ll) x.size() #define re return void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } #ifndef ONLINE_JUDGE #define debug(x...) \ cerr << "[" << #x << "] = ["; \ _print(x) #else #define debug(x...) #endif typedef pair<ll, ll> pii; typedef vector<pii> vii; typedef vector<ll> vi; const ll mod = 1e9 + 7; const ll N = 200; ll i, j; /* */ ll dp[N]; void solve() { in2(n, W); for (ll item = 1; item <= n; item++) { in2(w, v); for (i = W; i >= w; i--) { dp[i] = max(dp[i - w] + v, dp[i]); } } ll best = 0; for (i = 0; i <= W; i++) { best = max(best, dp[i]); } ce(best); } int32_t main() { // jaldi ll t; // cin>>t; t = 1; while (t--) { solve(); } } /* */
#include <bits/stdc++.h> using namespace std; #define ll long long #define in(a) \ ll a; \ cin >> a; #define in2(a, b) \ ll a, b; \ cin >> a >> b; #define in3(a, b, c) \ ll a, b, c; \ cin >> a >> b >> c; #define in4(a, b, c, d) \ ll a, b, c, d; \ cin >> a >> b >> c >> d; #define in5(a, b, c, d, e) \ ll a, b, c, d, e; \ cin >> a >> b >> c >> d >> e; #define lelo(b) \ for (auto &i : b) \ cin >> i; #define ff first #define ss second #define call(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define show(x) \ for (auto t : x) \ cout << t << " "; #define pb push_back #define bhar(s, v) memset(s, v, sizeof(s)) #define endl "\n" #define ce(x) cout << x << "\n"; #define nl cout << endl; #define jaldi \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define dekh_lo ce("dekh_lo"); #define sz(x) (ll) x.size() #define re return void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } #ifndef ONLINE_JUDGE #define debug(x...) \ cerr << "[" << #x << "] = ["; \ _print(x) #else #define debug(x...) #endif typedef pair<ll, ll> pii; typedef vector<pii> vii; typedef vector<ll> vi; const ll mod = 1e9 + 7; const ll N = 1e5 + 5; ll i, j; /* */ ll dp[N]; void solve() { in2(n, W); for (ll item = 1; item <= n; item++) { in2(w, v); for (i = W; i >= w; i--) { dp[i] = max(dp[i - w] + v, dp[i]); } } ll best = 0; for (i = 0; i <= W; i++) { best = max(best, dp[i]); } ce(best); } int32_t main() { // jaldi ll t; // cin>>t; t = 1; while (t--) { solve(); } } /* */
replace
88
89
88
89
0
p03163
C++
Runtime Error
#include <iostream> using namespace std; using ll = long long; int N, Weight; ll w[101], v[101]; ll dp[101][10001]; int main() { cin >> N >> Weight; for (int i = 1; i <= N; ++i) cin >> w[i] >> v[i]; for (int i = 1; i <= N; ++i) for (int j = 1; j <= Weight; ++j) if (j >= w[i]) dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]); else dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]); cout << dp[N][Weight]; return 0; }
#include <iostream> using namespace std; using ll = long long; int N, Weight; ll w[101], v[101]; ll dp[101][100001]; int main() { cin >> N >> Weight; for (int i = 1; i <= N; ++i) cin >> w[i] >> v[i]; for (int i = 1; i <= N; ++i) for (int j = 1; j <= Weight; ++j) if (j >= w[i]) dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]); else dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]); cout << dp[N][Weight]; return 0; }
replace
7
8
7
8
0
p03163
C++
Runtime Error
#include <iostream> using namespace std; typedef long long ll; int n, t, wt[100 + 9]; ll ps[100 + 9]; ll dp[100 + 9][100 + 9]; int main() { scanf("%d%d", &n, &t); for (int i = 0; i < n; i++) scanf("%d%lld", &wt[i], &ps[i]); for (int i = wt[n - 1]; i <= t; i++) dp[n - 1][i] = ps[n - 1]; for (int i = n - 2; i >= 0; i--) { for (int j = t; j >= 0; j--) { dp[i][j] = dp[i + 1][j]; if (j > wt[i]) dp[i][j] = max(dp[i][j], dp[i + 1][j - wt[i]] + ps[i]); } } cout << dp[0][t] << endl; return 0; }
#include <iostream> using namespace std; typedef long long ll; int n, t, wt[100 + 9]; ll ps[100 + 9]; ll dp[100 + 9][100000 + 9]; int main() { scanf("%d%d", &n, &t); for (int i = 0; i < n; i++) scanf("%d%lld", &wt[i], &ps[i]); for (int i = wt[n - 1]; i <= t; i++) dp[n - 1][i] = ps[n - 1]; for (int i = n - 2; i >= 0; i--) { for (int j = t; j >= 0; j--) { dp[i][j] = dp[i + 1][j]; if (j > wt[i]) dp[i][j] = max(dp[i][j], dp[i + 1][j - wt[i]] + ps[i]); } } cout << dp[0][t] << endl; return 0; }
replace
6
7
6
7
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long ll; typedef pair<int, int> P; const int MAX_N = 105; const int MAX_W = 10005; ll w[MAX_N], v[MAX_N]; ll dp[MAX_N][MAX_W]; int main() { int N, W; cin >> N >> W; rep(i, N) { cin >> w[i] >> v[i]; } 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]] + v[i]); } else { dp[i + 1][j] = dp[i][j]; } } } /* rep(i,N+1) { rep(j,W+1) { cout << dp[i][j] << " "; } cout << endl; } */ cout << dp[N][W] << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long ll; typedef pair<int, int> P; const int MAX_N = 105; const int MAX_W = 100005; ll w[MAX_N], v[MAX_N]; ll dp[MAX_N][MAX_W]; int main() { int N, W; cin >> N >> W; rep(i, N) { cin >> w[i] >> v[i]; } 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]] + v[i]); } else { dp[i + 1][j] = dp[i][j]; } } } /* rep(i,N+1) { rep(j,W+1) { cout << dp[i][j] << " "; } cout << endl; } */ cout << dp[N][W] << endl; return 0; }
replace
6
7
6
7
0
p03163
C++
Runtime Error
// I SELL YOU...! #include <algorithm> #include <functional> #include <iostream> #include <queue> #include <vector> using namespace std; typedef long long ll; typedef pair<ll, ll> P; ll dp[100][110000] = {}; signed main() { ll n, W, ans = 0; cin >> n >> W; ll w[n], v[n]; for (int i = 0; i < n; i++) { cin >> w[i] >> v[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j <= W; j++) { dp[i + 1][j] = dp[i][j]; if (j - w[i] >= 0) { dp[i + 1][j] = max(dp[i + 1][j], dp[i][j - w[i]] + v[i]); ans = max(ans, dp[i + 1][j]); } } } cout << ans << endl; }
// I SELL YOU...! #include <algorithm> #include <functional> #include <iostream> #include <queue> #include <vector> using namespace std; typedef long long ll; typedef pair<ll, ll> P; ll dp[110][110000] = {}; signed main() { ll n, W, ans = 0; cin >> n >> W; ll w[n], v[n]; for (int i = 0; i < n; i++) { cin >> w[i] >> v[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j <= W; j++) { dp[i + 1][j] = dp[i][j]; if (j - w[i] >= 0) { dp[i + 1][j] = max(dp[i + 1][j], dp[i][j - w[i]] + v[i]); ans = max(ans, dp[i + 1][j]); } } } cout << ans << endl; }
replace
9
10
9
10
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, j, n) for (int i = (int)(j); i < (int)(n); i++) #define REP(i, j, n) for (int i = (int)(j); i <= (int)(n); i++) #define MOD 1000000007 #define int long long #define ALL(a) (a).begin(), (a).end() #define vi vector<int> #define vii vector<vi> #define pii pair<int, int> #define priq priority_queue<int> #define disup(A, key) distance(A.begin(), upper_bound(ALL(A), (int)(key))) #define dislow(A, key) distance(A.begin(), lower_bound(ALL(A), (int)(key))) #define tii tuple<int, int, int> signed main() { int N, W; cin >> N >> W; vii dp(N + 1, vi(W + 1)); REP(i, 1, N) { int w, v; cin >> w >> v; rep(j, 0, w) dp[i][j] = dp[i - 1][j]; REP(j, w, v) { dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w] + v); } } cout << dp[N][W] << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, j, n) for (int i = (int)(j); i < (int)(n); i++) #define REP(i, j, n) for (int i = (int)(j); i <= (int)(n); i++) #define MOD 1000000007 #define int long long #define ALL(a) (a).begin(), (a).end() #define vi vector<int> #define vii vector<vi> #define pii pair<int, int> #define priq priority_queue<int> #define disup(A, key) distance(A.begin(), upper_bound(ALL(A), (int)(key))) #define dislow(A, key) distance(A.begin(), lower_bound(ALL(A), (int)(key))) #define tii tuple<int, int, int> signed main() { int N, W; cin >> N >> W; vii dp(N + 1, vi(W + 1)); REP(i, 1, N) { int w, v; cin >> w >> v; rep(j, 0, w) dp[i][j] = dp[i - 1][j]; REP(j, w, W) { dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w] + v); } } cout << dp[N][W] << endl; }
replace
22
23
22
23
-6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)
p03163
C++
Runtime Error
#include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <vector> #define ll long long int #define skip cin >> ws; #define vll vector<ll> #define vb vector<bool> #define vpll vector<pair<ll, ll>> #define vvll vector<vector<ll>> #define pll pair<ll, ll> #define vs vector<string> #define vvpll vector<vector<pair<ll, ll>>> #define pb push_back #define pob pop_back() #define MOD (ll)(1e9 + 7) #define test \ ll t; \ cin >> t; \ while (t--) using namespace std; void enter(vll &ar) { ll n = ar.size(); for (ll i = 0; i < n; i++) { cin >> ar[i]; } } void show(vll &a) { ll n = a.size(); for (ll i = 0; i < n; i++) { cout << a[i] << " "; } cout << "\n"; } void SieveOfErat1sthenes(int n, vll &pri) { vb prime(n + 1, true); for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { pri.pb(p); for (int i = p * p; i <= n; i += p) prime[i] = false; } } } ll mo(ll a) { return a % MOD; } bool compare(pll a, pll b) { return a.first > b.first; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n, W; cin >> n >> W; vll w(n), v(n); for (ll i = 0; i < n; i++) { cin >> w[i] >> v[i]; } vvll mat(W + 1, vll(n + 1)); for (ll i = 1; i <= W; i++) { for (ll j = 1; j <= n; j++) { if (w[j - 1] > i) mat[i][j] = mat[i][j - 1]; else { mat[i][j] = max(mat[i][j - 1], v[j - 1] + mat[i - v[j - 1]][j - 1]); } } } cout << mat[W][n] << "\n"; return 0; }
#include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <vector> #define ll long long int #define skip cin >> ws; #define vll vector<ll> #define vb vector<bool> #define vpll vector<pair<ll, ll>> #define vvll vector<vector<ll>> #define pll pair<ll, ll> #define vs vector<string> #define vvpll vector<vector<pair<ll, ll>>> #define pb push_back #define pob pop_back() #define MOD (ll)(1e9 + 7) #define test \ ll t; \ cin >> t; \ while (t--) using namespace std; void enter(vll &ar) { ll n = ar.size(); for (ll i = 0; i < n; i++) { cin >> ar[i]; } } void show(vll &a) { ll n = a.size(); for (ll i = 0; i < n; i++) { cout << a[i] << " "; } cout << "\n"; } void SieveOfErat1sthenes(int n, vll &pri) { vb prime(n + 1, true); for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { pri.pb(p); for (int i = p * p; i <= n; i += p) prime[i] = false; } } } ll mo(ll a) { return a % MOD; } bool compare(pll a, pll b) { return a.first > b.first; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n, W; cin >> n >> W; vll w(n), v(n); for (ll i = 0; i < n; i++) { cin >> w[i] >> v[i]; } vvll mat(W + 1, vll(n + 1)); for (ll i = 1; i <= W; i++) { for (ll j = 1; j <= n; j++) { if (w[j - 1] > i) mat[i][j] = mat[i][j - 1]; else { mat[i][j] = max(mat[i][j - 1], v[j - 1] + mat[i - w[j - 1]][j - 1]); } } } cout << mat[W][n] << "\n"; return 0; }
replace
84
85
84
85
-11
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define itf ios_base::sync_with_stdio(false) #define ll long long int #define ct cin.tie(NULL) ll Z[102][10007]; ll step(ll a[], ll v[], ll w, ll n) { if (n == 0) return 0; if (Z[n][w] != -1) return Z[n][w]; if (w >= a[n - 1]) return (Z[n][w] = max(v[n - 1] + step(a, v, w - a[n - 1], n - 1), step(a, v, w, n - 1))); else return Z[n][w] = step(a, v, w, n - 1); } int main() { memset(Z, -1, sizeof(Z)); ll n, w; cin >> n >> w; ll a[n], v[n]; for (int i = 0; i < n; i++) cin >> a[i] >> v[i]; cout << step(a, v, w, n) << " "; return 0; }
#include <bits/stdc++.h> using namespace std; #define itf ios_base::sync_with_stdio(false) #define ll long long int #define ct cin.tie(NULL) ll Z[102][1000007]; ll step(ll a[], ll v[], ll w, ll n) { if (n == 0) return 0; if (Z[n][w] != -1) return Z[n][w]; if (w >= a[n - 1]) return (Z[n][w] = max(v[n - 1] + step(a, v, w - a[n - 1], n - 1), step(a, v, w, n - 1))); else return Z[n][w] = step(a, v, w, n - 1); } int main() { memset(Z, -1, sizeof(Z)); ll n, w; cin >> n >> w; ll a[n], v[n]; for (int i = 0; i < n; i++) cin >> a[i] >> v[i]; cout << step(a, v, w, n) << " "; return 0; }
replace
7
8
7
8
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> #define MAXN 20000 using namespace std; long long n, W, w[MAXN], v[MAXN], dp[MAXN]; int main() { scanf("%lld%lld", &n, &W); for (int i = 0; i < n; i++) { scanf("%lld%lld", w + i, v + i); } for (int i = 0; i < n; i++) { for (int j = W; j >= w[i]; j--) { dp[j] = max(dp[j], dp[j - w[i]] + v[i]); } } printf("%lld\n", dp[W]); return 0; }
#include <bits/stdc++.h> #define MAXN 200000 using namespace std; long long n, W, w[MAXN], v[MAXN], dp[MAXN]; int main() { scanf("%lld%lld", &n, &W); for (int i = 0; i < n; i++) { scanf("%lld%lld", w + i, v + i); } for (int i = 0; i < n; i++) { for (int j = W; j >= w[i]; j--) { dp[j] = max(dp[j], dp[j - w[i]] + v[i]); } } printf("%lld\n", dp[W]); return 0; }
replace
1
2
1
2
0
p03163
C++
Runtime Error
#include <algorithm> #include <ctype.h> #include <functional> #include <iostream> #include <limits.h> #include <map> #include <math.h> #include <queue> #include <stdarg.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <vector> #define ll long long using namespace std; void solve() { ll n, w; cin >> n >> w; vector<ll> dp(10005, -1); dp[0] = 0; ll x, v; for (ll i = 0; i < n; i++) { cin >> x >> v; for (ll j = w; j >= 0; j--) if (dp[j] >= 0 && j + x <= w && (dp[j + x] == -1 || dp[j + x] < dp[j] + v)) dp[j + x] = dp[j] + v; } ll ans = 0; for (ll i = 0; i <= w; i++) if (dp[i] >= 0) ans = max(ans, dp[i]); cout << ans << endl; } int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); ll t = 1; // cin>>t; while (t > 0) { t--; solve(); } return 0; }
#include <algorithm> #include <ctype.h> #include <functional> #include <iostream> #include <limits.h> #include <map> #include <math.h> #include <queue> #include <stdarg.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <vector> #define ll long long using namespace std; void solve() { ll n, w; cin >> n >> w; vector<ll> dp(100005, -1); dp[0] = 0; ll x, v; for (ll i = 0; i < n; i++) { cin >> x >> v; for (ll j = w; j >= 0; j--) if (dp[j] >= 0 && j + x <= w && (dp[j + x] == -1 || dp[j + x] < dp[j] + v)) dp[j + x] = dp[j] + v; } ll ans = 0; for (ll i = 0; i <= w; i++) if (dp[i] >= 0) ans = max(ans, dp[i]); cout << ans << endl; } int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); ll t = 1; // cin>>t; while (t > 0) { t--; solve(); } return 0; }
replace
23
24
23
24
0
p03163
C++
Runtime Error
///"Bismillahir Rahmanir Raheem"/// #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef pair<int, int> pii; typedef pair<ll, int> pli; typedef pair<int, ll> pil; typedef vector<pii> vii; typedef vector<pil> vil; typedef vector<pli> vli; #define ff first #define ss second #define pb push_back #define mp make_pair #define sz size() #define all(a) a.begin(), a.end() #define mem(a, b) memset(a, b, sizeof(a)) #define f0(i, b) for (int i = 0; i < (b); i++) #define f1(i, b) for (int i = 1; i <= (b); i++) #define f2(i, a, b) for (int i = (a); i <= (b); i++) #define fr(i, b, a) for (int i = (b); i >= (a); i--) #define rep(i, a, b, c) for (int i = (a); i != (b); i += (c)) int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1}; int dy8[] = {1, -1, 1, -1, 0, 0, -1, 1}; const double PI = acos(-1.0); const double EPS = 1e-6; const int MOD = (int)1e9 + 7; const int maxn = (int)2e5 + 5; const int LOGN = 20; const int N = 110; const int W = 100010; long long int dp[N][W]; int main() { int n, w; cin >> n >> w; // vector<int> dp(n+1,0); vector<int> pf(n); vector<int> wt(n); f1(i, n) { cin >> wt[i] >> pf[i]; } f1(i, n) { f1(j, w) { if (j - wt[i] >= 0) dp[i][j] = max(dp[i - 1][j], (dp[i - 1][j - wt[i]] + pf[i])); else { dp[i][j] = dp[i - 1][j]; } } } cout << dp[n][w] << endl; /*for(int i = 0; i<=n; i++){ for(int j =0;j<=bw;j++){ cout<<dp[i][j]<<" "; } cout<<endl; }*/ return 0; }
///"Bismillahir Rahmanir Raheem"/// #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef pair<int, int> pii; typedef pair<ll, int> pli; typedef pair<int, ll> pil; typedef vector<pii> vii; typedef vector<pil> vil; typedef vector<pli> vli; #define ff first #define ss second #define pb push_back #define mp make_pair #define sz size() #define all(a) a.begin(), a.end() #define mem(a, b) memset(a, b, sizeof(a)) #define f0(i, b) for (int i = 0; i < (b); i++) #define f1(i, b) for (int i = 1; i <= (b); i++) #define f2(i, a, b) for (int i = (a); i <= (b); i++) #define fr(i, b, a) for (int i = (b); i >= (a); i--) #define rep(i, a, b, c) for (int i = (a); i != (b); i += (c)) int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1}; int dy8[] = {1, -1, 1, -1, 0, 0, -1, 1}; const double PI = acos(-1.0); const double EPS = 1e-6; const int MOD = (int)1e9 + 7; const int maxn = (int)2e5 + 5; const int LOGN = 20; const int N = 110; const int W = 100010; long long int dp[N][W]; int main() { int n, w; cin >> n >> w; // vector<int> dp(n+1,0); vector<int> pf(N); vector<int> wt(N); f1(i, n) { cin >> wt[i] >> pf[i]; } f1(i, n) { f1(j, w) { if (j - wt[i] >= 0) dp[i][j] = max(dp[i - 1][j], (dp[i - 1][j - wt[i]] + pf[i])); else { dp[i][j] = dp[i - 1][j]; } } } cout << dp[n][w] << endl; /*for(int i = 0; i<=n; i++){ for(int j =0;j<=bw;j++){ cout<<dp[i][j]<<" "; } cout<<endl; }*/ return 0; }
replace
41
43
41
43
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> #include <iostream> using namespace std; #define ll long long int main() { ll N, W; cin >> N >> W; ll wt[N]; ll val[N]; for (ll i = 0; i < N; i++) { cin >> wt[i] >> val[i]; } ll dp[W + 1][N + 1]; for (ll w = 0; w <= W; w++) { for (ll i = 0; i <= N; i++) { if (w == 0 || i == 0) { dp[w][i] = 0; } else { if (wt[i - 1] <= w) { dp[w][i] = max(dp[w][i - 1], dp[w - wt[i]][i - 1] + val[i - 1]); } else { dp[w][i] = dp[w][i - 1]; } } } } cout << dp[W][N] << endl; }
#include <bits/stdc++.h> #include <iostream> using namespace std; #define ll long long int main() { ll N, W; cin >> N >> W; ll wt[N]; ll val[N]; for (ll i = 0; i < N; i++) { cin >> wt[i] >> val[i]; } ll dp[W + 1][N + 1]; for (ll w = 0; w <= W; w++) { for (ll i = 0; i <= N; i++) { if (w == 0 || i == 0) { dp[w][i] = 0; } else { if (wt[i - 1] <= w) { dp[w][i] = max(dp[w][i - 1], dp[w - wt[i - 1]][i - 1] + val[i - 1]); } else { dp[w][i] = dp[w][i - 1]; } } } } cout << dp[W][N] << endl; }
replace
27
28
27
28
-11
p03163
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long int #define MOD 1000000007 #define MAX 10000000 using namespace std; int main() { ll n, W, w, v; cin >> n >> W; vector<pair<ll, ll>> knap; for (int i = 0; i < n; i++) { cin >> w >> v; knap.push_back({w, v}); } ll k[n + 1][W + 1]; for (int i = 0; i <= n; i++) { for (int s = 0; s <= W; s++) { if (i == 0 || s == 0) { k[i][s] = 0; } else if (knap[i - 1].first <= s) { k[i][s] = max(knap[i].second + k[i - 1][s - knap[i].first], k[i - 1][s]); } else { k[i][s] = k[i - 1][s]; } } } cout << k[n][W] << endl; return 0; }
#include <bits/stdc++.h> #define ll long long int #define MOD 1000000007 #define MAX 10000000 using namespace std; int main() { ll n, W, w, v; cin >> n >> W; vector<pair<ll, ll>> knap; for (int i = 0; i < n; i++) { cin >> w >> v; knap.push_back({w, v}); } ll k[n + 1][W + 1]; for (int i = 0; i <= n; i++) { for (int s = 0; s <= W; s++) { if (i == 0 || s == 0) { k[i][s] = 0; } else if (knap[i - 1].first <= s) { k[i][s] = max(knap[i - 1].second + k[i - 1][s - knap[i - 1].first], k[i - 1][s]); } else { k[i][s] = k[i - 1][s]; } } } cout << k[n][W] << endl; return 0; }
replace
20
22
20
22
0
p03163
C++
Runtime Error
// #include <iostream> #include <queue> #include <stdio.h> #include <vector> // #include <algorithm> #define pb push_back #define boost \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define f first #define s second // #define cerr if(0)cerr using namespace std; typedef long long LL; typedef pair<int, LL> PILL; const int N = 110; const LL I = 1e18; int n, W; PILL T[N]; LL DP[N]; LL maxi(LL x, LL y) { if (y > x) return y; return x; } int main() { // boost; scanf("%d%d", &n, &W); for (int i = 1; i <= n; i++) { scanf("%d%lld", &T[i].f, &T[i].s); } for (int i = 1; i <= n; i++) { for (int j = W; j >= T[i].f; j--) { DP[j] = maxi(DP[j], DP[j - T[i].f] + T[i].s); } } printf("%lld", DP[W]); return 0; }
// #include <iostream> #include <queue> #include <stdio.h> #include <vector> // #include <algorithm> #define pb push_back #define boost \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define f first #define s second // #define cerr if(0)cerr using namespace std; typedef long long LL; typedef pair<int, LL> PILL; const int N = 110; const LL I = 1e18; int n, W; PILL T[N]; LL DP[100010]; LL maxi(LL x, LL y) { if (y > x) return y; return x; } int main() { // boost; scanf("%d%d", &n, &W); for (int i = 1; i <= n; i++) { scanf("%d%lld", &T[i].f, &T[i].s); } for (int i = 1; i <= n; i++) { for (int j = W; j >= T[i].f; j--) { DP[j] = maxi(DP[j], DP[j - T[i].f] + T[i].s); } } printf("%lld", DP[W]); return 0; }
replace
21
22
21
22
0
p03163
C++
Runtime Error
#include <iostream> #define min(a, b) ((a < b) ? a : b) #define max(a, b) ((a > b) ? a : b) #define ll long long int #define BIG 1e9 using namespace std; int main() { ll dp[200][200000] = {}, v[200], w[200], W, N, ans; cin >> N >> W; for (int i = 0; i < N; i++) { cin >> w[i] >> v[i]; } for (int i = 0; i < N; i++) { for (int j = 1; j <= W; j++) { if (w[i] <= j) { dp[i + 1][j] = max(dp[i][j - w[i]] + v[i], dp[i][j]); } else { dp[i + 1][j] = dp[i][j]; } } } ans = dp[N][W]; cout << ans << endl; return 0; }
#include <iostream> #define min(a, b) ((a < b) ? a : b) #define max(a, b) ((a > b) ? a : b) #define ll long long int #define BIG 1e9 using namespace std; int main() { ll dp[101][100001] = {}, v[200], w[200], W, N, ans; cin >> N >> W; for (int i = 0; i < N; i++) { cin >> w[i] >> v[i]; } for (int i = 0; i < N; i++) { for (int j = 1; j <= W; j++) { if (w[i] <= j) { dp[i + 1][j] = max(dp[i][j - w[i]] + v[i], dp[i][j]); } else { dp[i + 1][j] = dp[i][j]; } } } ans = dp[N][W]; cout << ans << endl; return 0; }
replace
8
9
8
9
-11
p03163
C++
Runtime Error
#include <bits/stdc++.h> #define pes first #define val second #define ll long long using namespace std; int const MMM = 105; int n, m; pair<ll, ll> arr[MMM]; ll dp[MMM]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) cin >> arr[i].first >> arr[i].second; for (int i = 1; i <= n; i++) { for (int j = m; j >= arr[i].pes; j--) { dp[j] = max(dp[j], dp[j - arr[i].pes] + arr[i].val); } } cout << dp[m]; return 0; }
#include <bits/stdc++.h> #define pes first #define val second #define ll long long using namespace std; int const MMM = 105; int n, m; pair<ll, ll> arr[MMM]; ll dp[100099]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) cin >> arr[i].first >> arr[i].second; for (int i = 1; i <= n; i++) { for (int j = m; j >= arr[i].pes; j--) { dp[j] = max(dp[j], dp[j - arr[i].pes] + arr[i].val); } } cout << dp[m]; return 0; }
replace
10
11
10
11
0
p03163
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int n, w, value[105] = {}, weight[105] = {}; long long dp[101][100002]; long long findSolution(int pos, int W) { if (pos < 0 || W < 0) { return 0; } if (weight[pos] > W) { long long x = findSolution(pos - 1, W); // cout << "phase 1: "<<x <<" "<< pos << " " << W << endl; // cout << x << endl; return x; } else { long long x = value[pos] + findSolution(pos - 1, W - weight[pos]); // cout << "phase 2: "<< x << " "<< pos << " " << W << endl; long long y = findSolution(pos - 1, W); // cout << "phase 1: "<<y << " "<< pos << " " << W << endl; // cout << x << " " << y << endl; return dp[pos][W] = max(x, y); } // cout << pos << " " << W << endl; } int main() { scanf("%d %d", &n, &w); for (int i = 0; i < n; i++) { scanf("%d %d", &weight[i], &value[i]); } long long Max = findSolution(n, w); printf("%lld\n", Max); return 0; }
#include <bits/stdc++.h> using namespace std; int n, w, value[105] = {}, weight[105] = {}; long long dp[101][100002]; long long findSolution(int pos, int W) { if (pos < 0 || W < 0) { return 0; } if (dp[pos][W] != 0) { return dp[pos][W]; } if (weight[pos] > W) { long long x = findSolution(pos - 1, W); // cout << "phase 1: "<<x <<" "<< pos << " " << W << endl; // cout << x << endl; return x; } else { long long x = value[pos] + findSolution(pos - 1, W - weight[pos]); // cout << "phase 2: "<< x << " "<< pos << " " << W << endl; long long y = findSolution(pos - 1, W); // cout << "phase 1: "<<y << " "<< pos << " " << W << endl; // cout << x << " " << y << endl; return dp[pos][W] = max(x, y); } // cout << pos << " " << W << endl; } int main() { scanf("%d %d", &n, &w); for (int i = 0; i < n; i++) { scanf("%d %d", &weight[i], &value[i]); } long long Max = findSolution(n, w); printf("%lld\n", Max); return 0; }
insert
7
7
7
10
TLE
p03163
C++
Runtime Error
// in progress #include <bits/stdc++.h> using namespace std; #define ll long long int #define fr(i, a, b) for (int i = (int)(a); i <= (int)(b); i++) #define forn(i, n) for (int i = 0; i < (int)(n); i++) #define rfr(i, a, b) for (int i = (int)(a); i >= (int)(b); i--) #define PI 3.14159265 ll n, w; ll a[1005][2], rec[105][100010]; ll recur(ll ind, ll cw) { if (ind >= n) return 0; if (rec[ind + 1][cw] == -1) rec[ind + 1][cw] = recur(ind + 1, cw); if (rec[ind + 1][cw + a[ind][0]] == -1) rec[ind + 1][cw + a[ind][0]] = recur(ind + 1, cw + a[ind][0]); if (cw + a[ind][0] <= w) return max(rec[ind + 1][cw + a[ind][0]] + a[ind][1], rec[ind + 1][cw]); else return rec[ind + 1][cw]; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); // freopen("a.in", "r", stdin); // freopen("a.out", "w", stdout); cin >> n >> w; memset(rec, -1, sizeof(rec)); for (int i = 0; i < n; i++) cin >> a[i][0] >> a[i][1]; cout << recur(0, 0) << "\n"; return 0; }
// in progress #include <bits/stdc++.h> using namespace std; #define ll long long int #define fr(i, a, b) for (int i = (int)(a); i <= (int)(b); i++) #define forn(i, n) for (int i = 0; i < (int)(n); i++) #define rfr(i, a, b) for (int i = (int)(a); i >= (int)(b); i--) #define PI 3.14159265 ll n, w; ll a[1005][2], rec[105][100010]; ll recur(ll ind, ll cw) { if (ind >= n) return 0; if (rec[ind + 1][cw] == -1) rec[ind + 1][cw] = recur(ind + 1, cw); if (cw + a[ind][0] <= w && rec[ind + 1][cw + a[ind][0]] == -1) rec[ind + 1][cw + a[ind][0]] = recur(ind + 1, cw + a[ind][0]); if (cw + a[ind][0] <= w) return max(rec[ind + 1][cw + a[ind][0]] + a[ind][1], rec[ind + 1][cw]); else return rec[ind + 1][cw]; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); // freopen("a.in", "r", stdin); // freopen("a.out", "w", stdout); cin >> n >> w; memset(rec, -1, sizeof(rec)); for (int i = 0; i < n; i++) cin >> a[i][0] >> a[i][1]; cout << recur(0, 0) << "\n"; return 0; }
replace
15
16
15
16
0
p03163
C++
Runtime Error
#include <iostream> using namespace std; int main() { int n, lim; long long w[105], v[105], dp[105][1005]; cin >> n >> lim; for (int i = 1; i <= n; i++) { cin >> w[i] >> v[i]; } for (int i = 1; i <= lim; i++) { if (i < w[1]) dp[1][i] = 0; else dp[1][i] = v[1]; } for (int i = 2; i <= n; i++) { for (int j = 1; j <= lim; j++) { if (j < w[i]) dp[i][j] = dp[i - 1][j]; else dp[i][j] = max(dp[i - 1][j - w[i]] + v[i], dp[i - 1][j]); } } cout << dp[n][lim]; }
#include <iostream> using namespace std; int main() { int n, lim; long long w[105], v[105], dp[105][100005]; cin >> n >> lim; for (int i = 1; i <= n; i++) { cin >> w[i] >> v[i]; } for (int i = 1; i <= lim; i++) { if (i < w[1]) dp[1][i] = 0; else dp[1][i] = v[1]; } for (int i = 2; i <= n; i++) { for (int j = 1; j <= lim; j++) { if (j < w[i]) dp[i][j] = dp[i - 1][j]; else dp[i][j] = max(dp[i - 1][j - w[i]] + v[i], dp[i - 1][j]); } } cout << dp[n][lim]; }
replace
4
5
4
5
0
p03163
C++
Runtime Error
#include <algorithm> #include <bits/stdc++.h> #include <iostream> #include <vector> using namespace std; struct item { long val; long weight; }; int compare(item left_item, item right_item) { return left_item.weight <= right_item.weight; } int main() { int N, W; cin >> N >> W; vector<vector<long long>> DP(W + 1, vector<long long>(N, 0)); item temp = {0, 0}; vector<item> items_arr(N, temp); // vector<int> Weights(N,0); // vector<int> Value(N,0); for (int k = 0; k < N; k++) { cin >> items_arr[k].weight >> items_arr[k].val; } // init sort(items_arr.begin(), items_arr.end(), compare); // for(auto x:items_arr)cout<<x.weight<<endl; for (int i = 1; i <= W; i++) { if (i >= items_arr[0].weight) { DP[i][0] = items_arr[0].val; } } for (int i = 1; i <= W; i++) { for (int j = 1; j < N; j++) { if (i >= items_arr[j].weight) { DP[i][j] = max(items_arr[j].val + DP[i - items_arr[j].weight][j - 1], DP[i][j - 1]); // if(i==3&&j==2)cout<<items_arr[j].val+DP[i-items_arr[j].weight][j-1]<<" // "<<DP[i][j-1]<<endl; } else { DP[i][j] = DP[i][j - 1]; } // cout<<i<<" "<<j<<" "<<DP[i][j]<<endl; } } cout << DP[W][N - 1] << endl; // system("pause"); return 0; }
#include <algorithm> #include <bits/stdc++.h> #include <iostream> #include <vector> using namespace std; struct item { long val; long weight; }; int compare(item left_item, item right_item) { return left_item.weight <= right_item.weight; } int main() { int N, W; cin >> N >> W; vector<vector<long long>> DP(W + 1, vector<long long>(N, 0)); item temp = {0, 0}; vector<item> items_arr(N, temp); // vector<int> Weights(N,0); // vector<int> Value(N,0); for (int k = 0; k < N; k++) { cin >> items_arr[k].weight >> items_arr[k].val; } // init // sort(items_arr.begin(),items_arr.end(),compare); // for(auto x:items_arr)cout<<x.weight<<endl; for (int i = 1; i <= W; i++) { if (i >= items_arr[0].weight) { DP[i][0] = items_arr[0].val; } } for (int i = 1; i <= W; i++) { for (int j = 1; j < N; j++) { if (i >= items_arr[j].weight) { DP[i][j] = max(items_arr[j].val + DP[i - items_arr[j].weight][j - 1], DP[i][j - 1]); // if(i==3&&j==2)cout<<items_arr[j].val+DP[i-items_arr[j].weight][j-1]<<" // "<<DP[i][j-1]<<endl; } else { DP[i][j] = DP[i][j - 1]; } // cout<<i<<" "<<j<<" "<<DP[i][j]<<endl; } } cout << DP[W][N - 1] << endl; // system("pause"); return 0; }
replace
36
37
36
37
0
p03163
C++
Runtime Error
#include <algorithm> #include <cstdio> using namespace std; typedef long long int ll; static const int MAX_N = 100; static const int MAX_W = 10000; int N, W; int w[MAX_N], v[MAX_N]; ll memo[MAX_N + 1][MAX_W + 1]; int main() { scanf("%d %d", &N, &W); for (int i = 0; i < N; i++) scanf("%d %d", &w[i], &v[i]); for (int i = 0; i < N; i++) { for (int j = 0; j <= W; j++) { if (j < w[i]) memo[i + 1][j] = memo[i][j]; else memo[i + 1][j] = max(memo[i][j], memo[i][j - w[i]] + v[i]); } } printf("%lld\n", memo[N][W]); return 0; }
#include <algorithm> #include <cstdio> using namespace std; typedef long long int ll; static const int MAX_N = 100; static const int MAX_W = 100000; int N, W; int w[MAX_N], v[MAX_N]; ll memo[MAX_N + 1][MAX_W + 1]; int main() { scanf("%d %d", &N, &W); for (int i = 0; i < N; i++) scanf("%d %d", &w[i], &v[i]); for (int i = 0; i < N; i++) { for (int j = 0; j <= W; j++) { if (j < w[i]) memo[i + 1][j] = memo[i][j]; else memo[i + 1][j] = max(memo[i][j], memo[i][j - w[i]] + v[i]); } } printf("%lld\n", memo[N][W]); return 0; }
replace
5
6
5
6
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> typedef long long ll; typedef long double ld; #define F first #define S second #define pb emplace_back #define pi pair<int, int> using namespace std; const int maxn = 1e2 + 4; int n, w; int W[maxn], V[maxn]; ll dp[maxn][10004]; int main() { cin >> n >> w; for (int i = 0; i < n; ++i) { cin >> W[i] >> V[i]; } dp[0][W[0]] = V[0]; for (int i = 1; i < n; ++i) { for (int j = 0; j <= w; ++j) { if (W[i] <= j) { dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - W[i]] + V[i] * 1ll); } else { dp[i][j] = dp[i - 1][j]; } } } ll ans = 0; for (int i = 0; i <= w; ++i) { ans = max(ans, dp[n - 1][i]); } cout << ans << '\n'; }
#include <bits/stdc++.h> typedef long long ll; typedef long double ld; #define F first #define S second #define pb emplace_back #define pi pair<int, int> using namespace std; const int maxn = 1e2 + 4; int n, w; int W[maxn], V[maxn]; ll dp[maxn][100004]; int main() { cin >> n >> w; for (int i = 0; i < n; ++i) { cin >> W[i] >> V[i]; } dp[0][W[0]] = V[0]; for (int i = 1; i < n; ++i) { for (int j = 0; j <= w; ++j) { if (W[i] <= j) { dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - W[i]] + V[i] * 1ll); } else { dp[i][j] = dp[i - 1][j]; } } } ll ans = 0; for (int i = 0; i <= w; ++i) { ans = max(ans, dp[n - 1][i]); } cout << ans << '\n'; }
replace
12
13
12
13
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; #define ff first #define ss second #define int long long #define pb push_back #define mp make_pair #define pii pair<int, int> #define vi vector<int> #define mii map<int, int> #define pqb priority_queue<int> #define pqs priority_queue<int, vi, greater<int>> #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define mod 1000000007 #define inf 1e18 #define ps(x, y) fixed << setprecision(y) << x #define mk(arr, n, type) type *arr = new type[n]; #define w(x) \ int x; \ cin >> x; \ while (x--) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; void dk() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } int knapsack(int a[], int val[], int n, int k) { int dp[k + 1][n + 1]; for (int i = 0; i <= n; i++) { dp[0][i] = 0; } for (int i = 0; i <= k; i++) { dp[i][0] = 0; } for (int i = 1; i <= k; i++) { for (int j = 1; j <= n; j++) { if (a[j - 1] <= i) { dp[i][j] = max(dp[i][j - 1], dp[i - a[j - 1]][j - 1] + val[j - 1]); } else { dp[i][j] = dp[i][j - 1]; } // cout << dp[i][j] << " "; } // cout << endl; } return dp[k][n]; } int32_t main() { dk(); int n, k; cin >> n >> k; int val[n], a[n]; for (int i = 0; i < n; i++) { cin >> a[i] >> val[i]; } int m = knapsack(a, val, n, k); cout << m; return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; #define ff first #define ss second #define int long long #define pb push_back #define mp make_pair #define pii pair<int, int> #define vi vector<int> #define mii map<int, int> #define pqb priority_queue<int> #define pqs priority_queue<int, vi, greater<int>> #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define mod 1000000007 #define inf 1e18 #define ps(x, y) fixed << setprecision(y) << x #define mk(arr, n, type) type *arr = new type[n]; #define w(x) \ int x; \ cin >> x; \ while (x--) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; void dk() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } int knapsack(int a[], int val[], int n, int k) { int dp[k + 1][n + 1]; for (int i = 0; i <= n; i++) { dp[0][i] = 0; } for (int i = 0; i <= k; i++) { dp[i][0] = 0; } for (int i = 1; i <= k; i++) { for (int j = 1; j <= n; j++) { if (a[j - 1] <= i) { dp[i][j] = max(dp[i][j - 1], dp[i - a[j - 1]][j - 1] + val[j - 1]); } else { dp[i][j] = dp[i][j - 1]; } // cout << dp[i][j] << " "; } // cout << endl; } return dp[k][n]; } int32_t main() { // dk(); int n, k; cin >> n >> k; int val[n], a[n]; for (int i = 0; i < n; i++) { cin >> a[i] >> val[i]; } int m = knapsack(a, val, n, k); cout << m; return 0; }
replace
62
63
62
63
-11
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define fastio() \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define pb push_back #define show(x) cout << (#x) << " : " << x << endl; #define ll long long #define int long long #define ld long double #define fill(a, val) memset(a, val, sizeof(a)) #define mp make_pair #define ff first #define ss second #define pii pair<ll, ll> #define sq(x) ((x) * (x)) #define all(v) v.begin(), v.end() #define fone for (i = 0; i < n; i++) #define endl "\n" const ll MOD = 1000 * 1000 * 1000 + 7; const ll INF = 1ll * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 + 7; const ll MOD2 = 998244353; const ll N = 5000 * 100 + 1; const ll N2 = 1000 * 1000 + 5; const ld PI = 3.14159265; const ll R = 250; ll gcd(ll a, ll b) { if (!b) return a; return gcd(b, a % b); } ll power(ll x, ll y, ll p) { ll res = 1; x %= p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { return (a.second < b.second); } signed main() { fastio(); int n, k, W; vector<pair<int, int>> vv; cin >> n >> W; int v[n], w[n]; for (int i = 0; i < n; i++) { cin >> w[i] >> v[i]; } // cout<<"*"; vector<vector<ll>> dp(N + 1, vector<ll>(W + 1, 0LL)); // int dp[1000][1000]; for (int i = 1; i <= n; i++) { for (int j = 0; j <= W; j++) { if (j - w[i - 1] < 0) dp[i][j] = dp[i - 1][j]; else dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i - 1]] + v[i - 1]); } } ll ans = 0; for (int j = 0; j <= W; j++) { ans = max(ans, dp[n][j]); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define fastio() \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define pb push_back #define show(x) cout << (#x) << " : " << x << endl; #define ll long long #define int long long #define ld long double #define fill(a, val) memset(a, val, sizeof(a)) #define mp make_pair #define ff first #define ss second #define pii pair<ll, ll> #define sq(x) ((x) * (x)) #define all(v) v.begin(), v.end() #define fone for (i = 0; i < n; i++) #define endl "\n" const ll MOD = 1000 * 1000 * 1000 + 7; const ll INF = 1ll * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 + 7; const ll MOD2 = 998244353; const ll N = 5000 * 100 + 1; const ll N2 = 1000 * 1000 + 5; const ld PI = 3.14159265; const ll R = 250; ll gcd(ll a, ll b) { if (!b) return a; return gcd(b, a % b); } ll power(ll x, ll y, ll p) { ll res = 1; x %= p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { return (a.second < b.second); } signed main() { fastio(); int n, k, W; vector<pair<int, int>> vv; cin >> n >> W; int v[n], w[n]; for (int i = 0; i < n; i++) { cin >> w[i] >> v[i]; } // cout<<"*"; vector<vector<ll>> dp(n + 1, vector<ll>(W + 1, 0LL)); // int dp[1000][1000]; for (int i = 1; i <= n; i++) { for (int j = 0; j <= W; j++) { if (j - w[i - 1] < 0) dp[i][j] = dp[i - 1][j]; else dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i - 1]] + v[i - 1]); } } ll ans = 0; for (int j = 0; j <= W; j++) { ans = max(ans, dp[n][j]); } cout << ans << endl; return 0; }
replace
59
60
59
60
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long struct node { ll val, wt; }; int cmp(node a, node b) { return a.wt < b.wt; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif // ONLINE_JUDGE ll n, w; cin >> n >> w; node arr[n + 1]; for (int i = 1; i <= n; i++) cin >> arr[i].wt >> arr[i].val; sort(arr + 1, arr + n, cmp); ll dp[n + 1][w + 1]; memset(dp, 0, sizeof(dp)); for (ll i = 1; i <= n; i++) { for (ll j = 1; j <= w; j++) { if (j - arr[i].wt >= 0) { dp[i][j] = max((arr[i].val + dp[i - 1][j - arr[i].wt]), max(dp[i - 1][j], dp[i][j - 1])); } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } cout << dp[n][w]; // code goes here return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long struct node { ll val, wt; }; int cmp(node a, node b) { return a.wt < b.wt; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n, w; cin >> n >> w; node arr[n + 1]; for (int i = 1; i <= n; i++) cin >> arr[i].wt >> arr[i].val; sort(arr + 1, arr + n, cmp); ll dp[n + 1][w + 1]; memset(dp, 0, sizeof(dp)); for (ll i = 1; i <= n; i++) { for (ll j = 1; j <= w; j++) { if (j - arr[i].wt >= 0) { dp[i][j] = max((arr[i].val + dp[i - 1][j - arr[i].wt]), max(dp[i - 1][j], dp[i][j - 1])); } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } cout << dp[n][w]; // code goes here return 0; }
replace
10
14
10
11
-11
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long int dp[123][123]; int N, W, price[123], wt[123]; int32_t main() { cin >> N >> W; for (int i = 0; i < N; i++) { cin >> wt[i] >> price[i]; } for (int i = 1; i <= N; i++) { for (int j = 1; j <= W; j++) { if (wt[i - 1] <= j) { dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - wt[i - 1]] + price[i - 1]); } else dp[i][j] = dp[i - 1][j]; } } cout << dp[N][W] << "\n"; }
#include <bits/stdc++.h> using namespace std; #define int long long int dp[123][123456]; int N, W, price[123], wt[123]; int32_t main() { cin >> N >> W; for (int i = 0; i < N; i++) { cin >> wt[i] >> price[i]; } for (int i = 1; i <= N; i++) { for (int j = 1; j <= W; j++) { if (wt[i - 1] <= j) { dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - wt[i - 1]] + price[i - 1]); } else dp[i][j] = dp[i - 1][j]; } } cout << dp[N][W] << "\n"; }
replace
3
4
3
4
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> #include <cstdlib> #include <iostream> #include <math.h> #include <stdio.h> #include <string> #include <vector> using namespace std; using ll = long long; const int INF = 1e9 + 5; void max_self(ll &a, ll b) { a = max(a, b); } int main() { ll N, W; cin >> N >> W; vector<ll> dp(W + 1); // dp[i] the maximum total value with weight exactly for (ll item = 0; item < N; item++) { ll weight, value; cin >> weight >> value; for (int weight_already = W; weight_already >= 0; weight_already--) { max_self(dp[weight_already + weight], dp[weight_already] + value); } } ll answer = 0; for (ll i = 0; i <= W; i++) { max_self(answer, dp[i]); } cout << answer; return 0; }
#include <bits/stdc++.h> #include <cstdlib> #include <iostream> #include <math.h> #include <stdio.h> #include <string> #include <vector> using namespace std; using ll = long long; const int INF = 1e9 + 5; void max_self(ll &a, ll b) { a = max(a, b); } int main() { ll N, W; cin >> N >> W; vector<ll> dp(W + 1); // dp[i] the maximum total value with weight exactly for (ll item = 0; item < N; item++) { ll weight, value; cin >> weight >> value; for (int weight_already = W - weight; weight_already >= 0; weight_already--) { max_self(dp[weight_already + weight], dp[weight_already] + value); } } ll answer = 0; for (ll i = 0; i <= W; i++) { max_self(answer, dp[i]); } cout << answer; return 0; }
replace
22
23
22
24
0
p03163
C++
Runtime Error
/* tarun360 IIT Indore */ #include <bits/stdc++.h> using namespace std; #define ll long long #define ll_s long #define mod 1000000007 #define forn(i, start, lim) for (ll i = start; i < lim; i++) #define forn_d(i, start, lim) for (ll i = start; i >= lim; i--) #define forn_s(i, start, lim) for (ll_s i = start; i < lim; i++) #define forn_d_s(i, start, lim) for (ll_s i = start; i >= lim; i--) #define f first #define s second #define pb push_back #define mp make_pair #define debug1(a) cout << a << endl #define debug2(a, b) cout << a << " " << b << endl #define debug3(a, b, c) cout << a << " " << b << " " << c << endl #define debug4(a) cout << "chu " << a << endl ll w[105], v[105], dp[105][105]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll n, W; cin >> n >> W; forn(i, 1, n + 1) cin >> w[i] >> v[i]; forn(i, 1, n + 1) { forn(j, 1, W + 1) { if (j - w[i] < 0) dp[i][j] = dp[i - 1][j]; else dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]); } } cout << dp[n][W] << endl; return 0; }
/* tarun360 IIT Indore */ #include <bits/stdc++.h> using namespace std; #define ll long long #define ll_s long #define mod 1000000007 #define forn(i, start, lim) for (ll i = start; i < lim; i++) #define forn_d(i, start, lim) for (ll i = start; i >= lim; i--) #define forn_s(i, start, lim) for (ll_s i = start; i < lim; i++) #define forn_d_s(i, start, lim) for (ll_s i = start; i >= lim; i--) #define f first #define s second #define pb push_back #define mp make_pair #define debug1(a) cout << a << endl #define debug2(a, b) cout << a << " " << b << endl #define debug3(a, b, c) cout << a << " " << b << " " << c << endl #define debug4(a) cout << "chu " << a << endl ll w[105], v[105], dp[105][100005]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll n, W; cin >> n >> W; forn(i, 1, n + 1) cin >> w[i] >> v[i]; forn(i, 1, n + 1) { forn(j, 1, W + 1) { if (j - w[i] < 0) dp[i][j] = dp[i - 1][j]; else dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]); } } cout << dp[n][W] << endl; return 0; }
replace
22
23
22
23
0
p03163
C++
Runtime Error
// Program.cpp // Author : Aaryan Srivastava #include <bits/stdc++.h> #include <chrono> #include <random> #define pb push_back #define popb pop_back #define ull unsigned long long #define beg begin #define mp make_pair #define pii pair<int, int> #define piii pair<pii, int> #define rep(i, n) for (int(i) = 0; i < (n); i++) #define repA(i, x, y) for (int i = (x); i <= (y); i++) #define repD(i, x, y) for (int i = (x); i >= (y); i--) #define all(c) (c).begin(), (c).end() #define ff first #define ss second #define fill(a, val) memset(a, val, sizeof(a)) #define Randomize \ mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()) using ll = long long; #define int ll const int N = 1e5 + 5; const int mod = 1e9 + 5; const int inf = 1e18; const int SZ = 101; const double eps = 1e-9; using namespace std; #ifdef AaryanS #define mycout cout #define mycerr cerr #endif #ifndef AaryanS #define mycout \ if (false) \ cout #define mycerr \ if (false) \ cerr #define AaryanS 0 #endif #define debug(x) mycout << #x << " " << x << endl; // 17:54 stop // 19:08 start // 19:16 stop pii arr[105]; int dp[105][(int)1e4 + 5], n, W; int knap(int curr, int w) { if (curr >= n || w >= W) return 0; if (dp[curr][w] != -1) return dp[curr][w]; int res = knap(curr + 1, w); if (arr[curr].ff + w <= W) res = max(res, arr[curr].ss + knap(curr + 1, arr[curr].ff + w)); return dp[curr][w] = res; } void solve() { cin >> n >> W; repA(i, 0, n - 1) { cin >> arr[i].ff >> arr[i].ss; } repA(i, 0, n + 2) { repA(j, 0, W + 2) { dp[i][j] = -1; } } sort(arr, arr + n); // reverse(arr+1,arr+n+1) ; // repD(i,n-1,0){ // repA(j,0,w){ // if(j+arr[i+1].ff > w) // break ; // dp[i][j] += max(dp[i+1][j], arr[i].ss + dp[i+1][j+arr[i].ff]) ;//take // i+1 or not. // } // } cout << knap(0, 0) << endl; rep(i, n + 1) { rep(j, W + 1) mycout << dp[i][j] << " "; mycout << endl; } } int32_t main(int32_t argc, char *argv[]) { double t1 = clock(); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int TC = 1, t = 0; // cin >> TC ; while (t++ < TC) { // cout << "Case " << t << ":\n" ; solve(); } mycerr << "Time : " << 1000 * (clock() - t1) / CLOCKS_PER_SEC << " ms\n"; return 0; }
// Program.cpp // Author : Aaryan Srivastava #include <bits/stdc++.h> #include <chrono> #include <random> #define pb push_back #define popb pop_back #define ull unsigned long long #define beg begin #define mp make_pair #define pii pair<int, int> #define piii pair<pii, int> #define rep(i, n) for (int(i) = 0; i < (n); i++) #define repA(i, x, y) for (int i = (x); i <= (y); i++) #define repD(i, x, y) for (int i = (x); i >= (y); i--) #define all(c) (c).begin(), (c).end() #define ff first #define ss second #define fill(a, val) memset(a, val, sizeof(a)) #define Randomize \ mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()) using ll = long long; #define int ll const int N = 1e5 + 5; const int mod = 1e9 + 5; const int inf = 1e18; const int SZ = 101; const double eps = 1e-9; using namespace std; #ifdef AaryanS #define mycout cout #define mycerr cerr #endif #ifndef AaryanS #define mycout \ if (false) \ cout #define mycerr \ if (false) \ cerr #define AaryanS 0 #endif #define debug(x) mycout << #x << " " << x << endl; // 17:54 stop // 19:08 start // 19:16 stop pii arr[105]; int dp[105][(int)1e5 + 5], n, W; int knap(int curr, int w) { if (curr >= n || w >= W) return 0; if (dp[curr][w] != -1) return dp[curr][w]; int res = knap(curr + 1, w); if (arr[curr].ff + w <= W) res = max(res, arr[curr].ss + knap(curr + 1, arr[curr].ff + w)); return dp[curr][w] = res; } void solve() { cin >> n >> W; repA(i, 0, n - 1) { cin >> arr[i].ff >> arr[i].ss; } repA(i, 0, n + 2) { repA(j, 0, W + 2) { dp[i][j] = -1; } } sort(arr, arr + n); // reverse(arr+1,arr+n+1) ; // repD(i,n-1,0){ // repA(j,0,w){ // if(j+arr[i+1].ff > w) // break ; // dp[i][j] += max(dp[i+1][j], arr[i].ss + dp[i+1][j+arr[i].ff]) ;//take // i+1 or not. // } // } cout << knap(0, 0) << endl; rep(i, n + 1) { rep(j, W + 1) mycout << dp[i][j] << " "; mycout << endl; } } int32_t main(int32_t argc, char *argv[]) { double t1 = clock(); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int TC = 1, t = 0; // cin >> TC ; while (t++ < TC) { // cout << "Case " << t << ":\n" ; solve(); } mycerr << "Time : " << 1000 * (clock() - t1) / CLOCKS_PER_SEC << " ms\n"; return 0; }
replace
50
51
50
51
0
p03163
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cstdlib> #include <ext/pb_ds/assoc_container.hpp> #include <iostream> #include <map> #include <math.h> #include <random> #include <set> #include <stdio.h> #include <vector> using namespace std; using namespace __gnu_pbds; typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set; typedef long long ll; const ll INF = 1e18, MOD = 1e9 + 7; const int INT_MAX = 1e9; ll n, W; ll w[100], v[100], d[100], mx; int main() { cin >> n >> W; for (int i = 0; i < n; i++) { scanf("%lld%lld", &w[i], &v[i]); } ll sum = 0; for (int i = 0; i < n; i++) for (int j = W; j >= 0; j--) { if (j + w[i] <= W) { d[j + w[i]] = max(d[j + w[i]], d[j] + v[i]); mx = max(mx, d[j + w[i]]); } } cout << mx; }
#include <algorithm> #include <bitset> #include <cstdlib> #include <ext/pb_ds/assoc_container.hpp> #include <iostream> #include <map> #include <math.h> #include <random> #include <set> #include <stdio.h> #include <vector> using namespace std; using namespace __gnu_pbds; typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set; typedef long long ll; const ll INF = 1e18, MOD = 1e9 + 7; const int INT_MAX = 1e9; ll n, W; ll w[100], v[100], d[1000000], mx; int main() { cin >> n >> W; for (int i = 0; i < n; i++) { scanf("%lld%lld", &w[i], &v[i]); } ll sum = 0; for (int i = 0; i < n; i++) for (int j = W; j >= 0; j--) { if (j + w[i] <= W) { d[j + w[i]] = max(d[j + w[i]], d[j] + v[i]); mx = max(mx, d[j + w[i]]); } } cout << mx; }
replace
26
27
26
27
0
p03163
C++
Time Limit Exceeded
// By Zank100 #include <bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long #define mod 1000000007 #define din(t) \ long long t; \ cin >> t; #define in(t) cin >> t; ll k(ll W, ll w[], ll v[], ll i) { if (W == 0 || i == -1) return 0; if (w[i] > W) { return k(W, w, v, i - 1); } else { return max(v[i] + k(W - w[i], w, v, i - 1), k(W, w, v, i - 1)); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); din(n); din(W); ll w[n], v[n]; for (ll i = 0; i < n; i++) { in(w[i]); in(v[i]); } ll ans = k(W, w, v, n - 1); cout << ans << endl; return 0; }
// By Zank100 #include <bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long #define mod 1000000007 #define din(t) \ long long t; \ cin >> t; #define in(t) cin >> t; ll k(ll W, ll w[], ll v[], ll i) { if (W == 0 || i == -1) return 0; if (w[i] > W) { return k(W, w, v, i - 1); } else { return max(v[i] + k(W - w[i], w, v, i - 1), k(W, w, v, i - 1)); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); din(n); din(W); ll w[n], v[n]; for (ll i = 0; i < n; i++) { in(w[i]); in(v[i]); } ll k[n + 1][W + 1]; for (ll i = 0; i <= n; i++) { for (ll j = 0; j <= W; j++) { if (i == 0 || j == 0) { k[i][j] = 0; } else if (w[i - 1] > j) { k[i][j] = k[i - 1][j]; } else { k[i][j] = max(v[i - 1] + k[i - 1][j - w[i - 1]], k[i - 1][j]); } } } ll ans = k[n][W]; cout << ans << endl; return 0; }
replace
29
30
29
42
TLE
p03163
C++
Runtime Error
#include <bits/stdc++.h> #define fly \ ios::sync_with_stdio(false); \ cin.tie(nullptr); \ cout.tie(nullptr) #define ll long long const ll mod = 1e9 + 7; using namespace std; ll dp[100010][101] = {0}; int main(int argc, char const *argv[]) { fly; ll n, w; cin >> n >> w; vector<ll> weigth(n); vector<ll> cst(n); for (int i = 1; i <= n; i++) { cin >> weigth[i] >> cst[i]; } for (ll i = 0; i <= w; i++) { for (ll j = 0; j <= n; j++) { dp[i][j] = 0; if (i == 0 || j == 0) continue; dp[i][j] = max(dp[i][j], dp[i][j - 1]); if (i >= weigth[j]) { dp[i][j] = max(dp[i][j], dp[i - weigth[j]][j - 1] + cst[j]); } } } ll ans = 0; for (ll i = 0; i <= w; i++) { ans = max(ans, dp[i][n]); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define fly \ ios::sync_with_stdio(false); \ cin.tie(nullptr); \ cout.tie(nullptr) #define ll long long const ll mod = 1e9 + 7; using namespace std; ll dp[100010][101] = {0}; int main(int argc, char const *argv[]) { fly; ll n, w; cin >> n >> w; vector<ll> weigth(n + 1); vector<ll> cst(n + 1); for (int i = 1; i <= n; i++) { cin >> weigth[i] >> cst[i]; } for (ll i = 0; i <= w; i++) { for (ll j = 0; j <= n; j++) { dp[i][j] = 0; if (i == 0 || j == 0) continue; dp[i][j] = max(dp[i][j], dp[i][j - 1]); if (i >= weigth[j]) { dp[i][j] = max(dp[i][j], dp[i - weigth[j]][j - 1] + cst[j]); } } } ll ans = 0; for (ll i = 0; i <= w; i++) { ans = max(ans, dp[i][n]); } cout << ans << endl; return 0; }
replace
14
16
14
16
-6
free(): invalid pointer
p03163
C++
Time Limit Exceeded
/* ALLAH is Almighty.... */ #include <bits/stdc++.h> #define ll long long using namespace std; const ll mx = 10e5 + 9; ll n, k, weight; ll w[mx], v[mx]; ll dp[101][mx]; ll calculation(ll item, ll need) { if (dp[item][weight] != -1) return dp[item][weight]; if (item == n) return 0; ll t1, t2; t1 = t2 = 0; if (need - w[item] >= 0) t1 = v[item] + calculation(item + 1, need - w[item]); t2 = calculation(item + 1, need); return dp[item][need] = max(t1, t2); } void solve() { cin >> n >> weight; for (ll i = 0; i < n; ++i) cin >> w[i] >> v[i]; for (ll i = 0; i < 101; ++i) for (ll j = 0; j < mx; ++j) dp[i][j] = -1; cout << calculation(0, weight) << "\n"; } int main() // Md. Inzamam-ul Haque { solve(); }
/* ALLAH is Almighty.... */ #include <bits/stdc++.h> #define ll long long using namespace std; const ll mx = 10e5 + 9; ll n, k, weight; ll w[mx], v[mx]; ll dp[101][mx]; ll calculation(ll item, ll need) { if (dp[item][need] != -1) return dp[item][need]; if (item == n) return 0; ll t1, t2; t1 = t2 = 0; if (need - w[item] >= 0) t1 = v[item] + calculation(item + 1, need - w[item]); t2 = calculation(item + 1, need); return dp[item][need] = max(t1, t2); } void solve() { cin >> n >> weight; for (ll i = 0; i < n; ++i) cin >> w[i] >> v[i]; for (ll i = 0; i < 101; ++i) for (ll j = 0; j < mx; ++j) dp[i][j] = -1; cout << calculation(0, weight) << "\n"; } int main() // Md. Inzamam-ul Haque { solve(); }
replace
14
16
14
16
TLE
p03163
C++
Runtime Error
#include <algorithm> #include <iostream> int n, w; long long arr[10005]; int main() { std::ios::sync_with_stdio(false); std::cin.tie(NULL); std::cin >> n >> w; for (int i = 0; i < n; i++) { long long a, b; std::cin >> a >> b; for (int j = w; j > a - 1; j--) arr[j] = std::max(arr[j], arr[j - a] + b); } std::cout << *std::max_element(&arr[0], &arr[w + 1]) << '\n'; }
#include <algorithm> #include <iostream> int n, w; long long arr[100005]; int main() { std::ios::sync_with_stdio(false); std::cin.tie(NULL); std::cin >> n >> w; for (int i = 0; i < n; i++) { long long a, b; std::cin >> a >> b; for (int j = w; j > a - 1; j--) arr[j] = std::max(arr[j], arr[j - a] + b); } std::cout << *std::max_element(&arr[0], &arr[w + 1]) << '\n'; }
replace
5
6
5
6
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7; int n, w; long long ans[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); // freopen("in.in" , "r" , stdin) ; cin >> n >> w; for (int i = 1; i < N; i++) { ans[i] = -1e9; } for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; for (int j = w; j >= 0; j--) { ans[j + a] = max(ans[j + a], ans[j] + b); } } long long sol = 0; for (int i = 0; i <= w; i++) { sol = max(sol, ans[i]); } cout << sol; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7; int n, w; long long ans[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); // freopen("in.in" , "r" , stdin) ; cin >> n >> w; for (int i = 1; i < N; i++) { ans[i] = -1e9; } for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; for (int j = w - a; j >= 0; j--) { ans[j + a] = max(ans[j + a], ans[j] + b); } } long long sol = 0; for (int i = 0; i <= w; i++) { sol = max(sol, ans[i]); } cout << sol; return 0; }
replace
24
25
24
25
0
p03163
C++
Runtime Error
#include <cstdio> #define ll long long #define max(a, b) (a > b ? a : b) #define maxn 10050 using namespace std; ll dp[maxn]; int main() { int n, W; scanf("%d%d", &n, &W); for (int i = 1; i <= n; i++) { int a, b; scanf("%d%d", &a, &b); for (int j = W; j >= a; j--) dp[j] = max(dp[j - a] + b, dp[j]); } ll ans = 0; for (int j = 0; j <= W; j++) if (dp[j] > ans) ans = dp[j]; printf("%lld\n", ans); return 0; }
#include <cstdio> #define ll long long #define max(a, b) (a > b ? a : b) #define maxn 100050 using namespace std; ll dp[maxn]; int main() { int n, W; scanf("%d%d", &n, &W); for (int i = 1; i <= n; i++) { int a, b; scanf("%d%d", &a, &b); for (int j = W; j >= a; j--) dp[j] = max(dp[j - a] + b, dp[j]); } ll ans = 0; for (int j = 0; j <= W; j++) if (dp[j] > ans) ans = dp[j]; printf("%lld\n", ans); return 0; }
replace
3
4
3
4
0
p03163
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <string> #include <vector> using namespace std; typedef long long ll; int v[101], w[101]; ll dp[102][10002]; int main() { int N, W; cin >> N >> W; for (int i = 0; i < N; i++) cin >> w[i] >> v[i]; 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 if (w[i] <= j) { dp[i + 1][j] = max(dp[i][j - w[i]] + v[i], dp[i][j]); } } } /* for ( int i = 0; i < N; i++ ) { for ( int j = 0; j <= W; j++ ) { cout << dp[i][j] << " "; } cout << endl; } */ cout << dp[N][W] << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <string> #include <vector> using namespace std; typedef long long ll; int v[101], w[101]; ll dp[102][100002]; int main() { int N, W; cin >> N >> W; for (int i = 0; i < N; i++) cin >> w[i] >> v[i]; 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 if (w[i] <= j) { dp[i + 1][j] = max(dp[i][j - w[i]] + v[i], dp[i][j]); } } } /* for ( int i = 0; i < N; i++ ) { for ( int j = 0; j <= W; j++ ) { cout << dp[i][j] << " "; } cout << endl; } */ cout << dp[N][W] << endl; return 0; }
replace
11
12
11
12
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> #include <fstream> #include <string> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define ll long long int N; vector<int> v(101), w(101); vector<vector<ll>> dp(101, vector<ll>(101, -1)); // i番目以降の品物から重さの和がj以下なるように選んだときの、 // 取りうる価値の総和の最大値を返す関数 // i=品物 j=容量 ll rec(int i, int j) { if (dp[i][j] != -1) { return dp[i][j]; } ll res; if (i == N) { // 品物がもう残っていないときは、価値の和の最大値は0で確定 res = 0; } else if (j < w[i]) { // 残りの容量が足りず品物iを入れられないので、入れないパターンだけ処理 // i+1 以降の品物のみを使ったときの最大値をそのままこの場合の最大値にする res = rec(i + 1, j); } else { // 品物iを入れるか入れないか選べるので、両方試して価値の和が大きい方を選ぶ res = max(rec(i + 1, j), rec(i + 1, j - w[i]) + v[i]); } dp[i][j] = res; return res; } int main() { // 入力 int W; cin >> N >> W; rep(i, N) cin >> w[i] >> v[i]; // 答え cout << rec(0, W) << endl; }
#include <bits/stdc++.h> #include <fstream> #include <string> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define ll long long int N; vector<int> v(101), w(101); vector<vector<ll>> dp(101, vector<ll>(1000001, -1)); // i番目以降の品物から重さの和がj以下なるように選んだときの、 // 取りうる価値の総和の最大値を返す関数 // i=品物 j=容量 ll rec(int i, int j) { if (dp[i][j] != -1) { return dp[i][j]; } ll res; if (i == N) { // 品物がもう残っていないときは、価値の和の最大値は0で確定 res = 0; } else if (j < w[i]) { // 残りの容量が足りず品物iを入れられないので、入れないパターンだけ処理 // i+1 以降の品物のみを使ったときの最大値をそのままこの場合の最大値にする res = rec(i + 1, j); } else { // 品物iを入れるか入れないか選べるので、両方試して価値の和が大きい方を選ぶ res = max(rec(i + 1, j), rec(i + 1, j - w[i]) + v[i]); } dp[i][j] = res; return res; } int main() { // 入力 int W; cin >> N >> W; rep(i, N) cin >> w[i] >> v[i]; // 答え cout << rec(0, W) << endl; }
replace
10
11
10
11
0
p03163
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define lli long long int #define fr(i, j, p) for (int i = j; i < p; i++) #define frr(i, j, p) for (int i = j; i >= p; i--) #define PI 3.14159265358979323846 #define pb push_back #define pbk pop_back() #define mkp make_pair #define print(m) std::cout << std::fixed << std::setprecision(11) << m << endl; #define ff first #define ss second #define mod 1000000007 #define removeDuplicates(a) a.resize(unique(all(a)) - a.begin()) lli knap(lli tot, lli w[], lli v[], lli n) { if (n == 0 || tot == 0) return 0; if (w[n - 1] > tot) return knap(tot, w, v, n - 1); else return max(v[n - 1] + knap(tot - w[n - 1], w, v, n - 1), knap(tot, w, v, n - 1)); } int main() { lli n, tot; cin >> n >> tot; lli w[n], v[n]; fr(i, 0, n) cin >> w[i] >> v[i]; cout << knap(tot, w, v, n); return 0; }
#include <bits/stdc++.h> using namespace std; #define lli long long int #define fr(i, j, p) for (int i = j; i < p; i++) #define frr(i, j, p) for (int i = j; i >= p; i--) #define PI 3.14159265358979323846 #define pb push_back #define pbk pop_back() #define mkp make_pair #define print(m) std::cout << std::fixed << std::setprecision(11) << m << endl; #define ff first #define ss second #define mod 1000000007 #define removeDuplicates(a) a.resize(unique(all(a)) - a.begin()) lli knap(lli tot, lli w[], lli v[], lli n) { lli k[n + 1][tot + 1]; fr(i, 0, n + 1) { fr(j, 0, tot + 1) { if (i == 0 || j == 0) k[i][j] = 0; else if (w[i - 1] <= j) k[i][j] = max(v[i - 1] + k[i - 1][j - w[i - 1]], k[i - 1][j]); else k[i][j] = k[i - 1][j]; } } return k[n][tot]; } int main() { lli n, tot; cin >> n >> tot; lli w[n], v[n]; fr(i, 0, n) cin >> w[i] >> v[i]; cout << knap(tot, w, v, n); return 0; }
replace
16
23
16
28
TLE
p03163
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; signed main() { int n, w; cin >> n >> w; vector<int> x(n, 0), v(n, 0); for (int i = 1; i <= n; i++) cin >> x[i] >> v[i]; vector<vector<long long>> f(n + 1, vector<long long>(w * 2 + 5, 0)); for (int i = 1; i <= n; i++) { for (int j = 0; j < x[i]; j++) f[i][j] = f[i - 1][j]; for (int j = x[i]; j <= w; j++) { f[i][j] = max(f[i - 1][j], f[i - 1][j - x[i]] + v[i]); } } cout << f[n][w]; return 0; }
#include <iostream> #include <vector> using namespace std; signed main() { int n, w; cin >> n >> w; vector<int> x(n + 1, 0), v(n + 1, 0); for (int i = 1; i <= n; i++) cin >> x[i] >> v[i]; vector<vector<long long>> f(n + 1, vector<long long>(w * 2 + 5, 0)); for (int i = 1; i <= n; i++) { for (int j = 0; j < x[i]; j++) f[i][j] = f[i - 1][j]; for (int j = x[i]; j <= w; j++) { f[i][j] = max(f[i - 1][j], f[i - 1][j - x[i]] + v[i]); } } cout << f[n][w]; return 0; }
replace
7
8
7
8
0
p03163
C++
Runtime Error
#include <fstream> #include <iostream> long long n, S, w[1000], v[1000], F[100][100000]; using namespace std; int main() { // freopen("KNAPSACK1.INP","r",stdin); cin >> n >> S; for (long long i = 1; i <= n; i++) cin >> w[i] >> v[i]; for (long long i = 1; i <= n; i++) for (long long j = 1; j <= S; j++) if (w[i] <= j) F[i][j] = max(F[i - 1][j - w[i]] + v[i], F[i - 1][j]); else F[i][j] = F[i - 1][j]; cout << F[n][S]; return 0; }
#include <fstream> #include <iostream> long long n, S, w[1000], v[1000], F[101][100001]; using namespace std; int main() { // freopen("KNAPSACK1.INP","r",stdin); cin >> n >> S; for (long long i = 1; i <= n; i++) cin >> w[i] >> v[i]; for (long long i = 1; i <= n; i++) for (long long j = 1; j <= S; j++) if (w[i] <= j) F[i][j] = max(F[i - 1][j - w[i]] + v[i], F[i - 1][j]); else F[i][j] = F[i - 1][j]; cout << F[n][S]; return 0; }
replace
2
3
2
3
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> #include <iostream> using namespace std; int N, W, weight[104]; long long value[104]; long long dp[104][10005]; long long solve(int i, int w) { if (i >= N) { return 0; } if (dp[i][w] != -1) { return dp[i][w]; } long long X = solve(i + 1, w); int nw = w - weight[i]; if (nw >= 0) { X = max(X, value[i] + solve(i + 1, nw)); } return dp[i][w] = X; } int main() { cin >> N >> W; for (int i = 0; i < N; i++) { cin >> weight[i] >> value[i]; } for (int i = 0; i < N; i++) { for (int j = 0; j < 100005; j++) { dp[i][j] = -1; } } for (int i = 0; i <= W; i++) { if (i >= weight[N - 1]) { dp[N - 1][i] = value[N - 1]; } else { dp[N - 1][i] = 0; } } cout << solve(0, W); return 0; }
#include <bits/stdc++.h> #include <iostream> using namespace std; int N, W, weight[104]; long long value[104]; long long dp[104][100005]; long long solve(int i, int w) { if (i >= N) { return 0; } if (dp[i][w] != -1) { return dp[i][w]; } long long X = solve(i + 1, w); int nw = w - weight[i]; if (nw >= 0) { X = max(X, value[i] + solve(i + 1, nw)); } return dp[i][w] = X; } int main() { cin >> N >> W; for (int i = 0; i < N; i++) { cin >> weight[i] >> value[i]; } for (int i = 0; i < N; i++) { for (int j = 0; j < 100005; j++) { dp[i][j] = -1; } } for (int i = 0; i <= W; i++) { if (i >= weight[N - 1]) { dp[N - 1][i] = value[N - 1]; } else { dp[N - 1][i] = 0; } } cout << solve(0, W); return 0; }
replace
6
7
6
7
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long int #define ld long double #define ff first #define ss second #define P pair<int, int> #define pb push_back #define mod 1000000007 #define inf 1e18 #define endl '\n' const int N = 105; // change it when needed const int W = 1e5; int a[N], b[N]; int dp[N]; int n, w; int go() { for (int i = 0; i < n; i++) { for (int j = w; j >= 0; j--) { if (j - a[i] >= 0) { dp[j] = max(dp[j], b[i] + dp[j - a[i]]); } } } return dp[w]; } void solve() { cin >> n >> w; for (int i = 0; i < n; i++) { cin >> a[i] >> b[i]; dp[i] = 0; } cout << go(); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); // int t; cin >> t; while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long int #define ld long double #define ff first #define ss second #define P pair<int, int> #define pb push_back #define mod 1000000007 #define inf 1e18 #define endl '\n' const int N = 105; // change it when needed const int W = 1e5; int a[N], b[N]; int dp[W]; int n, w; int go() { for (int i = 0; i < n; i++) { for (int j = w; j >= 0; j--) { if (j - a[i] >= 0) { dp[j] = max(dp[j], b[i] + dp[j - a[i]]); } } } return dp[w]; } void solve() { cin >> n >> w; for (int i = 0; i < n; i++) { cin >> a[i] >> b[i]; dp[i] = 0; } cout << go(); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); // int t; cin >> t; while (t--) solve(); return 0; }
replace
14
15
14
15
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> #include <iostream> using namespace std; int v[105], w[100005]; long long dp[105] = {0}; long long knapsack(int n, int W) { int i, j; for (i = 0; i < n; i++) { for (j = W; j >= w[i]; j--) dp[j] = max(dp[j], dp[j - w[i]] + v[i]); } return dp[W]; } int main() { int i, n, W; cin >> n >> W; for (i = 0; i < n; i++) { cin >> w[i] >> v[i]; } cout << knapsack(n, W) << endl; }
#include <bits/stdc++.h> #include <iostream> using namespace std; int v[105], w[100005]; long long dp[100005] = {0}; long long knapsack(int n, int W) { int i, j; for (i = 0; i < n; i++) { for (j = W; j >= w[i]; j--) dp[j] = max(dp[j], dp[j - w[i]] + v[i]); } return dp[W]; } int main() { int i, n, W; cin >> n >> W; for (i = 0; i < n; i++) { cin >> w[i] >> v[i]; } cout << knapsack(n, W) << endl; }
replace
4
5
4
5
0
p03163
C++
Runtime Error
// author: Harsh Nema #include <bits/stdc++.h> #define ll long long int #define F first #define S second const ll mod = 1e9 + 7; const ll INF = 10000000000000; #define pb push_back #define deb(x) cout << '>' << #x << ':' << x << endl; #define fastio \ ios_base::sync_with_stdio(0); \ cin.tie(0); using namespace std; int main() { fastio; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ll n = 0, w = 0; cin >> n >> w; vector<ll> wt(n), val(n); for (ll i = 0; i < n; ++i) { cin >> wt[i] >> val[i]; } ll dp[n + 1][w + 1]; for (ll i = 0; i <= n; ++i) { for (ll j = 0; j <= w; ++j) { if (i == 0 || j == 0) dp[i][j] = 0; else if (wt[i - 1] <= j) dp[i][j] = max(val[i - 1] + dp[i - 1][j - wt[i - 1]], dp[i - 1][j]); else dp[i][j] = dp[i - 1][j]; } } cout << dp[n][w]; return 0; }
// author: Harsh Nema #include <bits/stdc++.h> #define ll long long int #define F first #define S second const ll mod = 1e9 + 7; const ll INF = 10000000000000; #define pb push_back #define deb(x) cout << '>' << #x << ':' << x << endl; #define fastio \ ios_base::sync_with_stdio(0); \ cin.tie(0); using namespace std; int main() { fastio; /*#ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif*/ ll n = 0, w = 0; cin >> n >> w; vector<ll> wt(n), val(n); for (ll i = 0; i < n; ++i) { cin >> wt[i] >> val[i]; } ll dp[n + 1][w + 1]; for (ll i = 0; i <= n; ++i) { for (ll j = 0; j <= w; ++j) { if (i == 0 || j == 0) dp[i][j] = 0; else if (wt[i - 1] <= j) dp[i][j] = max(val[i - 1] + dp[i - 1][j - wt[i - 1]], dp[i - 1][j]); else dp[i][j] = dp[i - 1][j]; } } cout << dp[n][w]; return 0; }
replace
15
19
15
19
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define int long long typedef long double ld; #define pb push_back #define f(i, a, b) for (int i = a; i < b; i++) #define fd(i, a, b) for (int i = a - 1; i >= b; i--) #define pf push_front #define fi first #define se second #define INF 1e18 const int mod = 1e9 + 7; int cnt = 0; int dp[100 + 5][100000 + 5] = {}; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } void swap(int a, int b) { int temp = a; a = b; b = temp; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE // for getting input from input.txt freopen("INPUT.txt", "r", stdin); // for writing output to output.txt freopen("OUTPUT.txt", "w", stdout); #endif // -------------------------------------Code starts // here--------------------------------------------------------------------- int t = 1; // cin>>t; while (t--) { int n, w; cin >> n >> w; pair<int, int> p[n + 1]; f(i, 1, n + 1) { cin >> p[i].fi; cin >> p[i].se; } f(i, 0, n + 1) { dp[i][0] = 0; } f(i, 0, w + 1) { dp[0][i] = 0; } f(i, 1, n + 1) { f(j, 1, w + 1) { if (j - p[i].fi >= 0) dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - p[i].fi] + p[i].se); else dp[i][j] = dp[i - 1][j]; } } cout << dp[n][w] << endl; } }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define int long long typedef long double ld; #define pb push_back #define f(i, a, b) for (int i = a; i < b; i++) #define fd(i, a, b) for (int i = a - 1; i >= b; i--) #define pf push_front #define fi first #define se second #define INF 1e18 const int mod = 1e9 + 7; int cnt = 0; int dp[100 + 5][100000 + 5] = {}; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } void swap(int a, int b) { int temp = a; a = b; b = temp; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); /* #ifndef ONLINE_JUDGE // for getting input from input.txt freopen("INPUT.txt", "r", stdin); // for writing output to output.txt freopen("OUTPUT.txt", "w", stdout); #endif */ // -------------------------------------Code starts // here--------------------------------------------------------------------- int t = 1; // cin>>t; while (t--) { int n, w; cin >> n >> w; pair<int, int> p[n + 1]; f(i, 1, n + 1) { cin >> p[i].fi; cin >> p[i].se; } f(i, 0, n + 1) { dp[i][0] = 0; } f(i, 0, w + 1) { dp[0][i] = 0; } f(i, 1, n + 1) { f(j, 1, w + 1) { if (j - p[i].fi >= 0) dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - p[i].fi] + p[i].se); else dp[i][j] = dp[i - 1][j]; } } cout << dp[n][w] << endl; } }
replace
34
41
34
41
-11
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long ll n, we; ll w[101], v[101]; ll memo[101][100001]; ll findans(ll i, ll n, ll sum) { if (i == n) { if (sum <= we) { return 0; } else { return -1000000000000000; } } /*if(i==n-1) { if(sum+w[i]<=we) { return v[i]; } else { return 0; } }*/ if (memo[i][sum] != -1) { return memo[i][sum]; } ll ans1 = v[i] + findans(i + 1, n, sum + w[i]); ll ans2 = findans(i + 1, n, sum); return memo[i][sum] = max(ans1, ans2); } int main() { // Your code here! cin >> n >> we; for (ll i = 0; i < n; i++) { cin >> w[i] >> v[i]; } for (ll i = 0; i <= 100; i++) { for (ll j = 0; j <= 100000; j++) { memo[i][j] = -1; } } cout << findans(0, n, 0); }
#include <bits/stdc++.h> using namespace std; #define ll long long ll n, we; ll w[101], v[101]; ll memo[101][100001]; ll findans(ll i, ll n, ll sum) { if (i == n) { if (sum <= we) { return 0; } else { return -1000000000000000; } } /*if(i==n-1) { if(sum+w[i]<=we) { return v[i]; } else { return 0; } }*/ if (memo[i][sum] != -1) { return memo[i][sum]; } ll ans1 = -1000000000000000; if (sum + w[i] <= we) { ans1 = v[i] + findans(i + 1, n, sum + w[i]); } ll ans2 = findans(i + 1, n, sum); return memo[i][sum] = max(ans1, ans2); } int main() { // Your code here! cin >> n >> we; for (ll i = 0; i < n; i++) { cin >> w[i] >> v[i]; } for (ll i = 0; i <= 100; i++) { for (ll j = 0; j <= 100000; j++) { memo[i][j] = -1; } } cout << findans(0, n, 0); }
replace
29
30
29
33
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int mxN = 100; int wt[mxN], v[mxN], n, W; long long dp[mxN + 1][mxN + 1]; array<int, 3> s[mxN]; int main() { cin >> n >> W; for (int i = 0; i < n; ++i) { cin >> wt[i] >> v[i]; } for (int i = 0; i < n + 1; i++) { for (int w = 0; w <= W; ++w) { if (i == 0 || w == 0) { dp[i][w] = 0; } else if (wt[i - 1] <= w) { dp[i][w] = max(dp[i - 1][w], v[i - 1] + dp[i - 1][w - wt[i - 1]]); } else { dp[i][w] = dp[i - 1][w]; } } } cout << dp[n][W]; }
#include <bits/stdc++.h> using namespace std; const int mxN = 100, mxW = 1e5; long long wt[mxN], v[mxN], n, W; long long dp[mxN + 1][mxW + 1]; int main() { cin >> n >> W; for (int i = 0; i < n; ++i) { cin >> wt[i] >> v[i]; } for (int i = 0; i < n + 1; i++) { for (int w = 0; w <= W; ++w) { if (i == 0 || w == 0) { dp[i][w] = 0; } else if (wt[i - 1] <= w) { dp[i][w] = max(dp[i - 1][w], v[i - 1] + dp[i - 1][w - wt[i - 1]]); } else { dp[i][w] = dp[i - 1][w]; } } } cout << dp[n][W]; }
replace
3
7
3
6
0
p03163
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i = a; i < b; i++) #define rep(i, b) FOR(i, 0, b) #define INF mugen #define dump(x) cerr << #x << "=" << x << endl #define all(a) (a).begin(), (a).end() typedef vector<int> vi; typedef vector<vi> vii; typedef vector<vii> viii; typedef pair<int, int> P; template <class T> void chmin(T &a, T const &b) { if (b < a) a = b; } template <typename T> string to_string(const T &n) { ostringstream stm; stm << n; return stm.str(); } inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } using ll = long long; const ll mod = LLONG_MAX; int N, W; ll ans; ll dp[212345]; ll w[110], v[110]; signed main() { cin >> N >> W; rep(i, N) cin >> w[i] >> v[i]; for (int j = 0; j < N; j++) { for (int i = W; i >= 0; i) { dp[i + w[j]] = max(dp[i] + v[j], dp[i + w[j]]); } } for (int i = 0; i <= W; i++) { ans = max(ans, dp[i]); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i = a; i < b; i++) #define rep(i, b) FOR(i, 0, b) #define INF mugen #define dump(x) cerr << #x << "=" << x << endl #define all(a) (a).begin(), (a).end() typedef vector<int> vi; typedef vector<vi> vii; typedef vector<vii> viii; typedef pair<int, int> P; template <class T> void chmin(T &a, T const &b) { if (b < a) a = b; } template <typename T> string to_string(const T &n) { ostringstream stm; stm << n; return stm.str(); } inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } using ll = long long; const ll mod = LLONG_MAX; int N, W; ll ans; ll dp[212345]; ll w[110], v[110]; signed main() { cin >> N >> W; rep(i, N) cin >> w[i] >> v[i]; for (int j = 0; j < N; j++) { for (int i = W; i >= 0; i--) { dp[i + w[j]] = max(dp[i] + v[j], dp[i + w[j]]); } } for (int i = 0; i <= W; i++) { ans = max(ans, dp[i]); } cout << ans << endl; }
replace
38
39
38
39
TLE
p03163
C++
Runtime Error
/* """Bismillahir Rahmanur Rahim""" */ #include <bits/stdc++.h> using namespace std; #define pi 2 * acos(0.0) #define ll long long int #define pb push_back #define pf push_front const ll sz = 1000001; #define ek(x) __builtin_popcount(x) #define ses '\n' #define stm istringstream #define sob(x) x.begin(), x.end() #define ghora \ ios_base::sync_with_stdio(0); \ cin.tie(0); #define gcd __gcd ll lcm(ll x, ll y) { return (x * y) / gcd(x, y); } #define tin \ ll T; \ cin >> T; \ for (ll o = 1; o <= T; o++) #define tout cout << "Case " << o << ": "; ll val[102]; ll wt[103]; ll dp[102][103]; ll n, W; ll knapsack(ll i, ll w) { if (i > n - 1) return 0; if (dp[i][w] > 0) return dp[i][w]; ll x = -5, y; if (w + wt[i] <= W) x = val[i] + knapsack(i + 1, w + wt[i]); y = knapsack(i + 1, w); return dp[i][w] = max(x, y); } int main() { memset(dp, -1, sizeof(dp)); cin >> n >> W; for (ll i = 0; i < n; i++) { cin >> wt[i] >> val[i]; } ll x = knapsack(0, 0); cout << x << ses; return 0; } /* -------------------- | ~SOHAN~ | | ~Chandler68~ | -------------------- || VALAR MORGULIS||==|| ALL MEN MUST DIE || \\ Power Is Power// || I Can Do This All day || // We are on a Break \\ // How you doin'? \\ || Say My Name || ~~ || I Am The Who Knocks || // I Am Ted Mosby Architect \\ || It Is Legen --wait for it -- dary ,Legendary || \\ Penny - Penny - Penny // -- Bazinga */
/* """Bismillahir Rahmanur Rahim""" */ #include <bits/stdc++.h> using namespace std; #define pi 2 * acos(0.0) #define ll long long int #define pb push_back #define pf push_front const ll sz = 1000001; #define ek(x) __builtin_popcount(x) #define ses '\n' #define stm istringstream #define sob(x) x.begin(), x.end() #define ghora \ ios_base::sync_with_stdio(0); \ cin.tie(0); #define gcd __gcd ll lcm(ll x, ll y) { return (x * y) / gcd(x, y); } #define tin \ ll T; \ cin >> T; \ for (ll o = 1; o <= T; o++) #define tout cout << "Case " << o << ": "; ll val[102]; ll wt[103]; ll dp[105][100050]; ll n, W; ll knapsack(ll i, ll w) { if (i > n - 1) return 0; if (dp[i][w] > 0) return dp[i][w]; ll x = -5, y; if (w + wt[i] <= W) x = val[i] + knapsack(i + 1, w + wt[i]); y = knapsack(i + 1, w); return dp[i][w] = max(x, y); } int main() { memset(dp, -1, sizeof(dp)); cin >> n >> W; for (ll i = 0; i < n; i++) { cin >> wt[i] >> val[i]; } ll x = knapsack(0, 0); cout << x << ses; return 0; } /* -------------------- | ~SOHAN~ | | ~Chandler68~ | -------------------- || VALAR MORGULIS||==|| ALL MEN MUST DIE || \\ Power Is Power// || I Can Do This All day || // We are on a Break \\ // How you doin'? \\ || Say My Name || ~~ || I Am The Who Knocks || // I Am Ted Mosby Architect \\ || It Is Legen --wait for it -- dary ,Legendary || \\ Penny - Penny - Penny // -- Bazinga */
replace
29
30
29
30
0
p03163
C++
Runtime Error
#include <cmath> #include <cstring> #include <iostream> #include <string> #include <vector> #define ll long long using namespace std; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } // const ll INF = 1LL << 60; int N, W; vector<ll> weight, value; int main() { cin >> N >> W; weight.resize(N); value.resize(N); for (int i = 0; i < N; i++) { cin >> weight[i] >> value[i]; } // dp[i][w]: // i番目(0-index)までの品物の中から、重さの総和がw以下である時の価値の総和の最大値 vector<vector<ll>> dp(N, vector<ll>(W, 0)); for (int w = 0; w <= W; w++) { if (w >= weight[0]) { dp[0][w] = value[0]; } } for (int i = 1; i < N; i++) { for (int w = 0; w <= W; w++) { if (w >= weight[i]) { chmax(dp[i][w], dp[i - 1][w - weight[i]] + value[i]); } chmax(dp[i][w], dp[i - 1][w]); } } cout << dp[N - 1][W] << endl; }
#include <cmath> #include <cstring> #include <iostream> #include <string> #include <vector> #define ll long long using namespace std; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } // const ll INF = 1LL << 60; int N, W; vector<ll> weight, value; int main() { cin >> N >> W; weight.resize(N); value.resize(N); for (int i = 0; i < N; i++) { cin >> weight[i] >> value[i]; } // dp[i][w]: // i番目(0-index)までの品物の中から、重さの総和がw以下である時の価値の総和の最大値 vector<vector<ll>> dp(N, vector<ll>(W + 1, 0)); for (int w = 0; w <= W; w++) { if (w >= weight[0]) { dp[0][w] = value[0]; } } for (int i = 1; i < N; i++) { for (int w = 0; w <= W; w++) { if (w >= weight[i]) { chmax(dp[i][w], dp[i - 1][w - weight[i]] + value[i]); } chmax(dp[i][w], dp[i - 1][w]); } } cout << dp[N - 1][W] << endl; }
replace
39
40
39
41
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll knapsack(vector<ll> &val, vector<ll> &wt, ll W, ll n, vector<vector<ll>> &dp) { if (n == 0 || W == 0) return dp[n][W] = 0; if (dp[n][W] != -1) return dp[n][W]; if (wt[n - 1] <= W) return dp[n][W] = max(val[n - 1] + knapsack(val, wt, W - wt[n - 1], n - 1, dp), knapsack(val, wt, W, n - 1, dp)); else return dp[n][W] = knapsack(val, wt, W, n - 1, dp); } int main() { ll n, W; cin >> n >> W; vector<ll> wt(n), val(n); for (ll i = 0; i < n; i++) cin >> wt[i] >> val[i]; vector<vector<ll>> dp(n, vector<ll>(W + 1, -1)); cout << knapsack(val, wt, W, n, dp); cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll knapsack(vector<ll> &val, vector<ll> &wt, ll W, ll n, vector<vector<ll>> &dp) { if (n == 0 || W == 0) return dp[n][W] = 0; if (dp[n][W] != -1) return dp[n][W]; if (wt[n - 1] <= W) return dp[n][W] = max(val[n - 1] + knapsack(val, wt, W - wt[n - 1], n - 1, dp), knapsack(val, wt, W, n - 1, dp)); else return dp[n][W] = knapsack(val, wt, W, n - 1, dp); } int main() { ll n, W; cin >> n >> W; vector<ll> wt(n), val(n); for (ll i = 0; i < n; i++) cin >> wt[i] >> val[i]; vector<vector<ll>> dp(n + 1, vector<ll>(W + 1, -1)); cout << knapsack(val, wt, W, n, dp); cout << endl; return 0; }
replace
26
27
26
27
-11
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define MOD 1000000007 #define N 102 #define M 100005 #define ll long long #define ld long double #define pb push_back #define ff first #define ss second ll dp[N][M]; ll v[N], w[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ll n, W; cin >> n >> W; ll i, j, ans; for (i = 1; i <= n; i++) { cin >> w[i] >> v[i]; } memset(dp, 0, sizeof dp); for (j = 0; j <= W; j++) { for (i = 1; i <= n; i++) { dp[i][j] = max(dp[i][j], dp[i - 1][j]); if (j >= w[i]) { dp[i][j] = max(dp[i][j], dp[i - 1][j - w[i]] + v[i]); } } } ans = 0; for (j = 0; j <= W; j++) { ans = max(ans, dp[n][j]); } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; #define MOD 1000000007 #define N 102 #define M 100005 #define ll long long #define ld long double #define pb push_back #define ff first #define ss second ll dp[N][M]; ll v[N], w[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll n, W; cin >> n >> W; ll i, j, ans; for (i = 1; i <= n; i++) { cin >> w[i] >> v[i]; } memset(dp, 0, sizeof dp); for (j = 0; j <= W; j++) { for (i = 1; i <= n; i++) { dp[i][j] = max(dp[i][j], dp[i - 1][j]); if (j >= w[i]) { dp[i][j] = max(dp[i][j], dp[i - 1][j - w[i]] + v[i]); } } } ans = 0; for (j = 0; j <= W; j++) { ans = max(ans, dp[n][j]); } cout << ans; return 0; }
delete
20
24
20
20
TLE
p03163
C++
Runtime Error
/** WARNING This Code Sucks Just Like My Life :( **/ #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // #pragma comment(linker, "/STACK:1024000000,1024000000") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/rope> using namespace std; using namespace __gnu_pbds; using namespace __gnu_cxx; template <class T> using orderd_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; const int N = 1e5 + 5; const int mod = 1e9 + 7; int n, w, weight[101], v[101]; long long dp[101][N]; int main() { scanf("%d%d", &n, &w); for (int i = 1; i <= n; i++) { scanf("%d%d", &weight[i], &v[i]); } for (int i = 1; i <= n + 1; i++) { for (int j = 1; j <= w; j++) { dp[i][j] = dp[i - 1][j]; if (weight[i - 1] <= j) dp[i][j] = max(dp[i - 1][j - weight[i - 1]] + v[i - 1], dp[i - 1][j]); } } printf("%lld", dp[n + 1][w]); return 0; }
/** WARNING This Code Sucks Just Like My Life :( **/ #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // #pragma comment(linker, "/STACK:1024000000,1024000000") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/rope> using namespace std; using namespace __gnu_pbds; using namespace __gnu_cxx; template <class T> using orderd_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; const int N = 1e5 + 5; const int mod = 1e9 + 7; int n, w, weight[102], v[102]; long long dp[102][N]; int main() { scanf("%d%d", &n, &w); for (int i = 1; i <= n; i++) { scanf("%d%d", &weight[i], &v[i]); } for (int i = 1; i <= n + 1; i++) { for (int j = 1; j <= w; j++) { dp[i][j] = dp[i - 1][j]; if (weight[i - 1] <= j) dp[i][j] = max(dp[i - 1][j - weight[i - 1]] + v[i - 1], dp[i - 1][j]); } } printf("%lld", dp[n + 1][w]); return 0; }
replace
24
26
24
26
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { long long int n, w; cin >> n >> w; long long int a[n + 1][2]; for (int i = 1; i <= n; i++) cin >> a[i][0] >> a[i][1]; vector<long long int> dp; for (int i = 1; i <= n; i++) { for (int j = w - a[i][0]; j > 0; j -= 1) { dp[j + a[i][0]] = max(dp[j + a[i][0]], a[i][1] + dp[j]); } } cout << dp[w]; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long int n, w; cin >> n >> w; long long int wi, va; vector<long long int> dp(w + 1); for (long long int i = 1; i <= n; i++) { cin >> wi >> va; for (long long int j = w - wi; j >= 0; j -= 1) { dp[j + wi] = max(dp[j + wi], va + dp[j]); } } cout << dp[w]; return 0; }
replace
5
12
5
11
-11
p03163
C++
Runtime Error
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; const int N = 105; long long dp[N][N]; int main() { long long n, W, v[N], w[N]; cin >> n >> W; for (int i = 1; i <= n; i++) { cin >> w[i] >> v[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], v[i] + dp[i - 1][j - w[i]]); } } } cout << dp[n][W] << endl; }
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; const int N = 105; long long dp[N][100005]; int main() { long long n, W, v[N], w[N]; cin >> n >> W; for (int i = 1; i <= n; i++) { cin >> w[i] >> v[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], v[i] + dp[i - 1][j - w[i]]); } } } cout << dp[n][W] << endl; }
replace
6
7
6
7
0
p03163
C++
Runtime Error
/* Author - linpaws07 */ #include <bits/stdc++.h> using namespace std; using namespace std::chrono; #define mod 1000000007 #define mp make_pair #define pb push_back #define inf (int)1e9 #define f first #define s second #define eps 1e-9 #define PI 3.1415926535897932384626433832795 #define scd(t) scanf("%d", &t) #define sclld(t) scanf("%lld", &t) #define scc(t) scanf("%c", &t) #define scs(t) scanf("%s", t) #define scf(t) scanf("%f", &t) #define sclf(t) scanf("%lf", &t) #define memst(a, b) memset(a, (b), sizeof(a)) typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<string> vs; typedef vector<pii> vii; typedef vector<vi> vvi; typedef map<int, int> mpii; typedef set<int> seti; typedef multiset<int> mseti; typedef long int int32; typedef unsigned long int uint32; typedef long long int int64; typedef unsigned long long int uint64; #define FAST \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define DEBUG(x) cout << '>' << #x << ':' << x << endl; #define rp(i, n) for (int i = 0; i < (n); i++) #define fr(i, a, b) for (int i = (a); i < (b); i++) #define fre(i, a, b) for (int i = (a); i <= (b); i++) #define frd(i, a, b) for (int i = (a); i >= (b); i--) inline bool eq(double a, double b) { return fabs(a - b) < 1e-9; } inline int two(int n) { return 1 << n; } inline int test(int n, int b) { return (n >> b) & 1; } inline void set_bit(int &n, int b) { n |= two(b); } inline void unset_bit(int &n, int b) { n &= ~two(b); } inline int last_bit(int n) { return n & (-n); } inline int ones(int n) { int res = 0; while (n && ++res) n -= n & (-n); return res; } auto start = high_resolution_clock::now(); inline void measure(); int64 w[102], v[102]; int64 dp[100][100002]; int main() { FAST // Do awesome things here int n, wt; cin >> n >> wt; fre(i, 1, n) { cin >> w[i] >> v[i]; } fre(i, 1, n) { fre(j, 1, wt) { if (j >= w[i]) { dp[i][j] = v[i] + dp[i - 1][j - w[i]]; } dp[i][j] = max(dp[i][j], dp[i - 1][j]); } } cout << dp[n][wt] << endl; return 0; } inline void measure() { auto stop = high_resolution_clock::now(); auto duration = duration_cast<milliseconds>(stop - start); cout << duration.count() << endl; }
/* Author - linpaws07 */ #include <bits/stdc++.h> using namespace std; using namespace std::chrono; #define mod 1000000007 #define mp make_pair #define pb push_back #define inf (int)1e9 #define f first #define s second #define eps 1e-9 #define PI 3.1415926535897932384626433832795 #define scd(t) scanf("%d", &t) #define sclld(t) scanf("%lld", &t) #define scc(t) scanf("%c", &t) #define scs(t) scanf("%s", t) #define scf(t) scanf("%f", &t) #define sclf(t) scanf("%lf", &t) #define memst(a, b) memset(a, (b), sizeof(a)) typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<string> vs; typedef vector<pii> vii; typedef vector<vi> vvi; typedef map<int, int> mpii; typedef set<int> seti; typedef multiset<int> mseti; typedef long int int32; typedef unsigned long int uint32; typedef long long int int64; typedef unsigned long long int uint64; #define FAST \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define DEBUG(x) cout << '>' << #x << ':' << x << endl; #define rp(i, n) for (int i = 0; i < (n); i++) #define fr(i, a, b) for (int i = (a); i < (b); i++) #define fre(i, a, b) for (int i = (a); i <= (b); i++) #define frd(i, a, b) for (int i = (a); i >= (b); i--) inline bool eq(double a, double b) { return fabs(a - b) < 1e-9; } inline int two(int n) { return 1 << n; } inline int test(int n, int b) { return (n >> b) & 1; } inline void set_bit(int &n, int b) { n |= two(b); } inline void unset_bit(int &n, int b) { n &= ~two(b); } inline int last_bit(int n) { return n & (-n); } inline int ones(int n) { int res = 0; while (n && ++res) n -= n & (-n); return res; } auto start = high_resolution_clock::now(); inline void measure(); int64 w[102], v[102]; int64 dp[102][100002]; int main() { FAST // Do awesome things here int n, wt; cin >> n >> wt; fre(i, 1, n) { cin >> w[i] >> v[i]; } fre(i, 1, n) { fre(j, 1, wt) { if (j >= w[i]) { dp[i][j] = v[i] + dp[i - 1][j - w[i]]; } dp[i][j] = max(dp[i][j], dp[i - 1][j]); } } cout << dp[n][wt] << endl; return 0; } inline void measure() { auto stop = high_resolution_clock::now(); auto duration = duration_cast<milliseconds>(stop - start); cout << duration.count() << endl; }
replace
58
59
58
59
0
p03163
C++
Runtime Error
#include <algorithm> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stdlib.h> #include <string.h> #include <string> #include <tuple> #include <vector> using namespace std; #define PI 3.14159265358979 typedef long long int ll; int main() { int dp[101][10000000] = {}; int N, W; cin >> N >> W; vector<int> w(N), v(N); for (int i = 0; i < N; i++) cin >> w[i] >> v[i]; for (int i = 1; i <= N; i++) { for (int j = 1; j <= W; j++) { 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]); } } cout << dp[N][W]; }
#include <algorithm> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stdlib.h> #include <string.h> #include <string> #include <tuple> #include <vector> using namespace std; #define PI 3.14159265358979 typedef long long int ll; int main() { long long int dp[110][100010] = {}; int N, W; cin >> N >> W; vector<int> w(N), v(N); for (int i = 0; i < N; i++) cin >> w[i] >> v[i]; for (int i = 1; i <= N; i++) { for (int j = 1; j <= W; j++) { 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]); } } cout << dp[N][W]; }
replace
16
17
16
17
-11
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define Ull unsigned long long #define pb push_back #define INF 2e16 #define PI acos(-1.0) #define mk make_pair #define pii pair<int, int> #define pll pair<ll, ll> #define min3(a, b, c) min(a, min(b, c)) #define max3(a, b, c) max(a, max(b, c)) #define min4(a, b, c, d) min(a, min(b, min(c, d))) #define max4(a, b, c, d) max(a, max(b, max(c, d))) #define SQR(a) ((a) * (a)) #define MEM(a, x) memset(a, x, sizeof(a)) #define ABS(x) ((x) < 0 ? -(x) : (x)) #define SORT(v) sort(v.begin(), v.end()) #define REV(v) reverse(v.begin(), v.end()) #define FastRead \ ios_base::sync_with_stdio(0); \ cin.tie(nullptr); const int N = 2; const ll MOD = 1e9 + 7; void Solve() { ll n, w; cin >> n >> w; ll dp[n][w + 1]; for (ll i = 0; i < n; i++) { for (ll j = 0; j <= w; j++) dp[i][j] = 0; } ll wt[n], val[n]; for (ll i = 0; i < n; i++) cin >> wt[i] >> val[i]; for (ll i = 0; i <= w; i++) { if (i >= wt[0]) dp[0][i] = val[0]; } for (ll i = 1; i <= n; i++) { for (ll j = 0; j <= w; j++) { if (j >= wt[i]) dp[i][j] = max(dp[i - 1][j], val[i] + dp[i - 1][j - wt[i]]); else dp[i][j] = dp[i - 1][j]; } } /*for (ll i = 0; i < n; i++) { for (ll j = 0; j <= w; j++) { cout << dp[i][j] << " "; } cout << "\n"; }*/ cout << dp[n - 1][w] << "\n"; } int main() { FastRead; int T = 1; // cin>>T; while (T--) { Solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define Ull unsigned long long #define pb push_back #define INF 2e16 #define PI acos(-1.0) #define mk make_pair #define pii pair<int, int> #define pll pair<ll, ll> #define min3(a, b, c) min(a, min(b, c)) #define max3(a, b, c) max(a, max(b, c)) #define min4(a, b, c, d) min(a, min(b, min(c, d))) #define max4(a, b, c, d) max(a, max(b, max(c, d))) #define SQR(a) ((a) * (a)) #define MEM(a, x) memset(a, x, sizeof(a)) #define ABS(x) ((x) < 0 ? -(x) : (x)) #define SORT(v) sort(v.begin(), v.end()) #define REV(v) reverse(v.begin(), v.end()) #define FastRead \ ios_base::sync_with_stdio(0); \ cin.tie(nullptr); const int N = 2; const ll MOD = 1e9 + 7; void Solve() { ll n, w; cin >> n >> w; ll dp[n][w + 1]; for (ll i = 0; i < n; i++) { for (ll j = 0; j <= w; j++) dp[i][j] = 0; } ll wt[n], val[n]; for (ll i = 0; i < n; i++) cin >> wt[i] >> val[i]; for (ll i = 0; i <= w; i++) { if (i >= wt[0]) dp[0][i] = val[0]; } for (ll i = 1; i < n; i++) { for (ll j = 0; j <= w; j++) { if (j >= wt[i]) dp[i][j] = max(dp[i - 1][j], val[i] + dp[i - 1][j - wt[i]]); else dp[i][j] = dp[i - 1][j]; } } /*for (ll i = 0; i < n; i++) { for (ll j = 0; j <= w; j++) { cout << dp[i][j] << " "; } cout << "\n"; }*/ cout << dp[n - 1][w] << "\n"; } int main() { FastRead; int T = 1; // cin>>T; while (T--) { Solve(); } return 0; }
replace
47
48
47
48
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define mod 1000000007 int main() { ll n, W; cin >> n >> W; ll w[n], v[n]; for (ll i = 0; i < n; i++) cin >> w[i] >> v[i]; ll dp[n + 1][n + 1]; for (ll i = 0; i <= n; i++) for (ll j = 0; j <= W; j++) { if (i == 0 || j == 0) dp[i][j] = 0; else if (w[i - 1] <= j) dp[i][j] = max(dp[i - 1][j], v[i - 1] + dp[i - 1][j - w[i - 1]]); else dp[i][j] = dp[i - 1][j]; } cout << dp[n][W]; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define mod 1000000007 int main() { ll n, W; cin >> n >> W; ll w[n], v[n]; for (ll i = 0; i < n; i++) cin >> w[i] >> v[i]; ll dp[n + 1][W + 1]; for (ll i = 0; i <= n; i++) for (ll j = 0; j <= W; j++) { if (i == 0 || j == 0) dp[i][j] = 0; else if (w[i - 1] <= j) dp[i][j] = max(dp[i - 1][j], v[i - 1] + dp[i - 1][j - w[i - 1]]); else dp[i][j] = dp[i - 1][j]; } cout << dp[n][W]; return 0; }
replace
12
13
12
13
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef long double ld; typedef vector<ll> vec; #define vecc vector<vector<ll>> #define LM LLONG_MAX #define int long long int #define P pair<ll, ll> #define ff first #define ss second #define FAST \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define endl "\n" #define N 200005 #define MOD (ll)1000000007 ll dp[105][N], a[105][3]; ll solve(ll n, ll w) { if (n < 1 && w < 1) return 0; ll &ans = dp[n][w]; if (ans != 0) return ans; ans = solve(n - 1, w); if (w >= a[n][1]) ans = max(ans, a[n][2] + solve(n - 1, w - a[n][1])); return ans; } signed main() { FAST; ll n, w; cin >> n >> w; memset(dp, 0, sizeof(dp)); for (int i = 1; i <= n; i++) cin >> a[i][1] >> a[i][2]; cout << solve(n, w) << "\n"; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef long double ld; typedef vector<ll> vec; #define vecc vector<vector<ll>> #define LM LLONG_MAX #define int long long int #define P pair<ll, ll> #define ff first #define ss second #define FAST \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define endl "\n" #define N 200005 #define MOD (ll)1000000007 ll dp[105][N], a[105][3]; ll solve(ll n, ll w) { if (n < 1 || w < 1) return 0; ll &ans = dp[n][w]; if (ans != 0) return ans; ans = solve(n - 1, w); if (w >= a[n][1]) ans = max(ans, a[n][2] + solve(n - 1, w - a[n][1])); return ans; } signed main() { FAST; ll n, w; cin >> n >> w; memset(dp, 0, sizeof(dp)); for (int i = 1; i <= n; i++) cin >> a[i][1] >> a[i][2]; cout << solve(n, w) << "\n"; }
replace
28
29
28
29
-11
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long int ull; #define endl "\n" #define pb push_back #define sq(a) (a) * (a) #define debug(x) cerr << #x << '=' << (x) << endl; #define debugv(v) \ cerr << #v << " : "; \ for (auto x : v) \ cerr << x << ' '; \ cerr << endl; #define MOD 1000000007 #define PI 3.141592653589793238 #define IOS \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll exp(ll x, ll n, ll mod) { ll result = 1; while (n > 0) { if (n & 1 == 1) result = (result * x) % mod; x = (x * x) % mod; n = n >> 1; } return result; } int32_t main() { IOS ll n, w; cin >> n >> w; ll v[n], wt[n]; for (ll i = 0; i < n; i++) { cin >> wt[i] >> v[i]; } ll dp[w + 1][n + 1]; for (ll i = 0; i <= w; i++) { for (ll j = 0; j <= n; j++) { if (i == 0 || j == 0) dp[i][j] = 0; else if (wt[j - 1] > i) dp[i][j] = dp[i][j - 1]; else dp[i][j] = max(v[i - 1] + dp[i - wt[j - 1]][j - 1], dp[i][j - 1]); } } cout << dp[w][n] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long int ull; #define endl "\n" #define pb push_back #define sq(a) (a) * (a) #define debug(x) cerr << #x << '=' << (x) << endl; #define debugv(v) \ cerr << #v << " : "; \ for (auto x : v) \ cerr << x << ' '; \ cerr << endl; #define MOD 1000000007 #define PI 3.141592653589793238 #define IOS \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll exp(ll x, ll n, ll mod) { ll result = 1; while (n > 0) { if (n & 1 == 1) result = (result * x) % mod; x = (x * x) % mod; n = n >> 1; } return result; } int32_t main() { IOS ll n, w; cin >> n >> w; ll v[n], wt[n]; for (ll i = 0; i < n; i++) { cin >> wt[i] >> v[i]; } ll dp[w + 1][n + 1]; for (ll i = 0; i <= w; i++) { for (ll j = 0; j <= n; j++) { if (i == 0 || j == 0) dp[i][j] = 0; else if (wt[j - 1] > i) dp[i][j] = dp[i][j - 1]; else dp[i][j] = max(v[j - 1] + dp[i - wt[j - 1]][j - 1], dp[i][j - 1]); } } cout << dp[w][n] << endl; return 0; }
replace
61
62
61
62
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) #define PI 3.14159265359 #define INF 1000100100 #define all(x) (x).begin(), (x).end() typedef long long ll; using namespace std; int main() { int n, w; cin >> n >> w; int wv[n][2]; rep(i, n) cin >> wv[i][0] >> wv[i][1]; ll dp[w + 1]; rep(i, w + 1) dp[i] = 0; rep(i, n + 1) { for (int j = w; j >= wv[i][0]; j--) { dp[j] = max(dp[j - wv[i][0]] + wv[i][1], dp[j]); } } cout << dp[w] << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) #define PI 3.14159265359 #define INF 1000100100 #define all(x) (x).begin(), (x).end() typedef long long ll; using namespace std; int main() { int n, w; cin >> n >> w; ll wv[n][2]; rep(i, n) cin >> wv[i][0] >> wv[i][1]; ll dp[w + 1]; rep(i, w + 1) dp[i] = 0; rep(i, n + 1) { for (int j = w; j >= wv[i][0]; j--) { dp[j] = max(dp[j - wv[i][0]] + wv[i][1], dp[j]); } } cout << dp[w] << endl; return 0; }
replace
11
12
11
12
-11
p03163
C++
Time Limit Exceeded
#include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define bogo_sort std::sort #define bozo_sort std::stable_sort #define alles(obj) obj.begin(), obj.end() #define elif else if #define unless(flg) if (!(flg)) #define elless(flg) else if (!(flg)) #define echo std::cout << #define read std::cin >> #define endl std::endl #define fin << '\n' #define bash push_back #define makePair std::make_pair #define _ << ' ' << // type-define #define Stack std::stack #define Queue std::queue #define Set std::set #define PQueue std::priority_queue #define Vector std::vector #define Pair std::pair #define Map std::map #define Greater std::greater using String = std::string; using llong = long long; using boolean = bool; using Pii = Pair<int, int>; using Vi = Vector<int>; using Vii = Vector<Pii>; // utils constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}; constexpr int dy[] = {0, 1, 0, -1, 1, -1, 1, -1}; constexpr int INF = 0x3f3f3f3f; constexpr llong LINF = 0x3f3f3f3f3f3f3f3fLL; namespace { llong power(llong x, llong n, llong mod) { llong ans = 1; while (n > 0) { if (n & 1) ans = (ans * x) % mod; x = (x * x) % mod; n >>= 1; } return ans; } llong power(llong x, llong n) { return power(x, n, 1000000007); } llong gcd(llong x, llong y) { return x % y ? gcd(y, x % y) : y; } llong lcm(llong x, llong y) { return (x / gcd(x, y) * y); } llong abs(llong n) { return (n < 0) ? -n : n; } boolean isMovable(int x, int y, int w, int h) { return (x >= 0 && y >= 0 && x < w && y < h); } } // namespace #define int long long namespace Rlyeh { int N, W; int w[110], v[110]; int dp[110][100100]; int dfs(int pos, int left) { if (pos == N) return 0; if (~dp[pos][left]) return dp[pos][left]; if (left - w[pos] < 0) { return dp[pos][left] = dfs(pos + 1, left); } else { return std::max(dfs(pos + 1, left), dfs(pos + 1, left - w[pos]) + v[pos]); } } signed call_of_Cthulhu(signed datum) { memset(dp, -1, sizeof(dp)); read N >> W; for (int i = 0; i < N; i++) { read w[i] >> v[i]; } echo dfs(0, W) fin; return 0; } } // namespace Rlyeh signed main() { std::cin.tie(0); std::ios::sync_with_stdio(false); int main_result = Rlyeh::call_of_Cthulhu(114514); return 0; }
#include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define bogo_sort std::sort #define bozo_sort std::stable_sort #define alles(obj) obj.begin(), obj.end() #define elif else if #define unless(flg) if (!(flg)) #define elless(flg) else if (!(flg)) #define echo std::cout << #define read std::cin >> #define endl std::endl #define fin << '\n' #define bash push_back #define makePair std::make_pair #define _ << ' ' << // type-define #define Stack std::stack #define Queue std::queue #define Set std::set #define PQueue std::priority_queue #define Vector std::vector #define Pair std::pair #define Map std::map #define Greater std::greater using String = std::string; using llong = long long; using boolean = bool; using Pii = Pair<int, int>; using Vi = Vector<int>; using Vii = Vector<Pii>; // utils constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}; constexpr int dy[] = {0, 1, 0, -1, 1, -1, 1, -1}; constexpr int INF = 0x3f3f3f3f; constexpr llong LINF = 0x3f3f3f3f3f3f3f3fLL; namespace { llong power(llong x, llong n, llong mod) { llong ans = 1; while (n > 0) { if (n & 1) ans = (ans * x) % mod; x = (x * x) % mod; n >>= 1; } return ans; } llong power(llong x, llong n) { return power(x, n, 1000000007); } llong gcd(llong x, llong y) { return x % y ? gcd(y, x % y) : y; } llong lcm(llong x, llong y) { return (x / gcd(x, y) * y); } llong abs(llong n) { return (n < 0) ? -n : n; } boolean isMovable(int x, int y, int w, int h) { return (x >= 0 && y >= 0 && x < w && y < h); } } // namespace #define int long long namespace Rlyeh { int N, W; int w[110], v[110]; int dp[110][100100]; int dfs(int pos, int left) { if (pos == N) return 0; if (~dp[pos][left]) return dp[pos][left]; if (left - w[pos] < 0) { return dp[pos][left] = dfs(pos + 1, left); } else { return dp[pos][left] = std::max(dfs(pos + 1, left), dfs(pos + 1, left - w[pos]) + v[pos]); } } signed call_of_Cthulhu(signed datum) { memset(dp, -1, sizeof(dp)); read N >> W; for (int i = 0; i < N; i++) { read w[i] >> v[i]; } echo dfs(0, W) fin; return 0; } } // namespace Rlyeh signed main() { std::cin.tie(0); std::ios::sync_with_stdio(false); int main_result = Rlyeh::call_of_Cthulhu(114514); return 0; }
replace
83
84
83
85
TLE
p03163
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define bogo_sort std::sort #define bozo_sort std::stable_sort #define alles(obj) obj.begin(), obj.end() #define elif else if #define unless(flg) if (!(flg)) #define elless(flg) else if (!(flg)) #define echo std::cout << #define read std::cin >> #define endl std::endl #define fin << '\n' #define bash push_back #define makePair std::make_pair #define _ << ' ' << // type-define #define Stack std::stack #define Queue std::queue #define Set std::set #define PQueue std::priority_queue #define Vector std::vector #define Pair std::pair #define Map std::map #define Greater std::greater using String = std::string; using llong = long long; using boolean = bool; using Pii = Pair<int, int>; using Vi = Vector<int>; using Vii = Vector<Pii>; // utils constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}; constexpr int dy[] = {0, 1, 0, -1, 1, -1, 1, -1}; constexpr int INF = 0x3f3f3f3f; constexpr llong LINF = 0x3f3f3f3f3f3f3f3fLL; namespace { llong power(llong x, llong n, llong mod) { llong ans = 1; while (n > 0) { if (n & 1) ans = (ans * x) % mod; x = (x * x) % mod; n >>= 1; } return ans; } llong power(llong x, llong n) { return power(x, n, 1000000007); } llong gcd(llong x, llong y) { return x % y ? gcd(y, x % y) : y; } llong lcm(llong x, llong y) { return (x / gcd(x, y) * y); } boolean isMovable(int x, int y, int w, int h) { return (x >= 0 && y >= 0 && x < w && y < h); } llong abs(llong n) { return n < 0 ? -n : n; } } // namespace #define int long long namespace Rlyeh { int N, W; int v[110], w[110]; int dp[110][100100]; int dfs(int pos, int left) { if (~dp[pos][left]) return dp[pos][left]; if (left - w[pos] < 0) { return dp[pos][left] = dfs(pos + 1, left); } else { return dp[pos][left] = std::max(dfs(pos + 1, left), dfs(pos + 1, left - w[pos]) + v[pos]); } } signed call_of_Cthulhu(signed datum) { memset(dp, -1, sizeof(dp)); read N >> W; for (int i = 0; i < N; i++) { read w[i] >> v[i]; } echo dfs(0, W) fin; return 0; } } // namespace Rlyeh signed main() { std::cin.tie(0); std::ios::sync_with_stdio(false); int main_result = Rlyeh::call_of_Cthulhu(114514); return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define bogo_sort std::sort #define bozo_sort std::stable_sort #define alles(obj) obj.begin(), obj.end() #define elif else if #define unless(flg) if (!(flg)) #define elless(flg) else if (!(flg)) #define echo std::cout << #define read std::cin >> #define endl std::endl #define fin << '\n' #define bash push_back #define makePair std::make_pair #define _ << ' ' << // type-define #define Stack std::stack #define Queue std::queue #define Set std::set #define PQueue std::priority_queue #define Vector std::vector #define Pair std::pair #define Map std::map #define Greater std::greater using String = std::string; using llong = long long; using boolean = bool; using Pii = Pair<int, int>; using Vi = Vector<int>; using Vii = Vector<Pii>; // utils constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}; constexpr int dy[] = {0, 1, 0, -1, 1, -1, 1, -1}; constexpr int INF = 0x3f3f3f3f; constexpr llong LINF = 0x3f3f3f3f3f3f3f3fLL; namespace { llong power(llong x, llong n, llong mod) { llong ans = 1; while (n > 0) { if (n & 1) ans = (ans * x) % mod; x = (x * x) % mod; n >>= 1; } return ans; } llong power(llong x, llong n) { return power(x, n, 1000000007); } llong gcd(llong x, llong y) { return x % y ? gcd(y, x % y) : y; } llong lcm(llong x, llong y) { return (x / gcd(x, y) * y); } boolean isMovable(int x, int y, int w, int h) { return (x >= 0 && y >= 0 && x < w && y < h); } llong abs(llong n) { return n < 0 ? -n : n; } } // namespace #define int long long namespace Rlyeh { int N, W; int v[110], w[110]; int dp[110][100100]; int dfs(int pos, int left) { if (~dp[pos][left]) return dp[pos][left]; if (pos == N) return 0; if (left - w[pos] < 0) { return dp[pos][left] = dfs(pos + 1, left); } else { return dp[pos][left] = std::max(dfs(pos + 1, left), dfs(pos + 1, left - w[pos]) + v[pos]); } } signed call_of_Cthulhu(signed datum) { memset(dp, -1, sizeof(dp)); read N >> W; for (int i = 0; i < N; i++) { read w[i] >> v[i]; } echo dfs(0, W) fin; return 0; } } // namespace Rlyeh signed main() { std::cin.tie(0); std::ios::sync_with_stdio(false); int main_result = Rlyeh::call_of_Cthulhu(114514); return 0; }
insert
79
79
79
81
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define clr(i, j) memset(i, j, sizeof(i)) #define pb push_back #define PI acos(-1) #define MOD ((int)1e9 + 7) typedef long long ll; const int mx = 1e9 + 5; ll dp[100][100009]; // dp[i][w] aktr value ttgab b awl i elements w el weight bt3ha w int main() { ll n, w; cin >> n >> w; ll ww[n + 7]; ll v[n + 7]; for (ll i = 1; i <= n; i++) { cin >> ww[i] >> v[i]; dp[i][ww[i]] = v[i]; } /* 3 8 3 30 4 50 5 60*/ for (int i = 1; i <= n; i++) { for (ll j = 0; j <= w; j++) { if (j != 0) { dp[0][j] = -1000000; } dp[i][j] = -1000000; if (j - ww[i] >= 0) { dp[i][j] = max(dp[i - 1][j - ww[i]] + v[i], dp[i - 1][j]); } dp[i][j] = max(dp[i][j], dp[i - 1][j]); } } ll ans = 0; for (ll j = 0; j <= w; j++) { ans = max(ans, dp[n][j]); } cout << ans; }
#include <bits/stdc++.h> using namespace std; #define clr(i, j) memset(i, j, sizeof(i)) #define pb push_back #define PI acos(-1) #define MOD ((int)1e9 + 7) typedef long long ll; const int mx = 1e9 + 5; ll dp[107][100009]; // dp[i][w] aktr value ttgab b awl i elements w el weight bt3ha w int main() { ll n, w; cin >> n >> w; ll ww[n + 7]; ll v[n + 7]; for (ll i = 1; i <= n; i++) { cin >> ww[i] >> v[i]; dp[i][ww[i]] = v[i]; } /* 3 8 3 30 4 50 5 60*/ for (int i = 1; i <= n; i++) { for (ll j = 0; j <= w; j++) { if (j != 0) { dp[0][j] = -1000000; } dp[i][j] = -1000000; if (j - ww[i] >= 0) { dp[i][j] = max(dp[i - 1][j - ww[i]] + v[i], dp[i - 1][j]); } dp[i][j] = max(dp[i][j], dp[i - 1][j]); } } ll ans = 0; for (ll j = 0; j <= w; j++) { ans = max(ans, dp[n][j]); } cout << ans; }
replace
9
10
9
10
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long int ll; ll N, W, w[1000], v[1000], dp[100][100005]; ll fun(ll i, ll tw) { if (tw > W || i > N + 1) return -1000000000000000000; if (i == N + 1) return 0; if (dp[i][tw] != -1) return dp[i][tw]; else dp[i][tw] = max(fun(i + 1, tw), v[i] + fun(i + 1, tw + w[i])); return dp[i][tw]; } int main() { ll i, ans; cin >> N >> W; for (i = 1; i <= N; i++) cin >> w[i] >> v[i]; memset(dp, -1, sizeof dp); ans = fun(1, 0); cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; ll N, W, w[1000], v[1000], dp[105][100005]; ll fun(ll i, ll tw) { if (tw > W || i > N + 1) return -1000000000000000000; if (i == N + 1) return 0; if (dp[i][tw] != -1) return dp[i][tw]; else dp[i][tw] = max(fun(i + 1, tw), v[i] + fun(i + 1, tw + w[i])); return dp[i][tw]; } int main() { ll i, ans; cin >> N >> W; for (i = 1; i <= N; i++) cin >> w[i] >> v[i]; memset(dp, -1, sizeof dp); ans = fun(1, 0); cout << ans; return 0; }
replace
3
4
3
4
0
p03163
C++
Runtime Error
#include <algorithm> #include <cstdio> using namespace std; long long dp[100][100000]; int main() { int n, w; scanf("%d%d", &n, &w); for (int i = 1, wi, vi; i <= n; ++i) { scanf("%d%d", &wi, &vi); for (int j = 1; j <= w; ++j) if (wi <= j) dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - wi] + vi); else dp[i][j] = dp[i - 1][j]; } printf("%lld\n", dp[n][w]); }
#include <algorithm> #include <cstdio> using namespace std; long long dp[101][100001]; int main() { int n, w; scanf("%d%d", &n, &w); for (int i = 1, wi, vi; i <= n; ++i) { scanf("%d%d", &wi, &vi); for (int j = 1; j <= w; ++j) if (wi <= j) dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - wi] + vi); else dp[i][j] = dp[i - 1][j]; } printf("%lld\n", dp[n][w]); }
replace
4
5
4
5
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int number, w; long long dp[100100]; int main() { cin >> number >> w; for (int i = 1; i <= number; i++) { int weight, value; cin >> weight >> value; for (int j = w - weight; j >= 0; j--) dp[j + w] = max(dp[j + w], dp[j] + value); } cout << dp[w] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int number, w; long long dp[100100]; int main() { cin >> number >> w; for (int i = 1; i <= number; i++) { int weight, value; cin >> weight >> value; for (int j = w - weight; j >= 0; j--) dp[j + weight] = max(dp[j + weight], dp[j] + value); } cout << dp[w] << endl; return 0; }
replace
10
11
10
11
0
p03163
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define ll long long int #define pb push_back #define mp make_pair #define deb(x) cout << #x << " " << x << "\n"; #define MAX 9223372036854775807 #define MIN -9223372036854775807 #define PI 3.141592653589 #define setbits(n) __builtin_popcountll(n) const ll mod = 1e9 + 7; const int N = 100 + 1, W = 1e5 + 1; ll dp[W][N], n, p; vector<ll> w(N), v(N); ll go(ll left, ll pos) { if (pos == n) return 0; if (dp[left][n] != -1) return dp[left][n]; ll ans = 0; if (w[pos] <= left) ans = v[pos] + go(left - w[pos], pos + 1); ans = max(ans, go(left, pos + 1)); return dp[left][pos] = ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll T = clock(); cin >> n >> p; for (ll i = 0; i < n; i++) cin >> w[i] >> v[i]; memset(dp, -1, sizeof(dp)); cout << go(p, 0); cerr << "\n\nTIME: " << (double)(clock() - T) / CLOCKS_PER_SEC << " sec\n"; T = clock(); return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long int #define pb push_back #define mp make_pair #define deb(x) cout << #x << " " << x << "\n"; #define MAX 9223372036854775807 #define MIN -9223372036854775807 #define PI 3.141592653589 #define setbits(n) __builtin_popcountll(n) const ll mod = 1e9 + 7; const int N = 100 + 1, W = 1e5 + 1; ll dp[W][N], n, p; vector<ll> w(N), v(N); ll go(ll left, ll pos) { if (pos == n) return 0; if (dp[left][pos] != -1) return dp[left][pos]; ll ans = 0; if (w[pos] <= left) ans = v[pos] + go(left - w[pos], pos + 1); ans = max(ans, go(left, pos + 1)); return dp[left][pos] = ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll T = clock(); cin >> n >> p; for (ll i = 0; i < n; i++) cin >> w[i] >> v[i]; memset(dp, -1, sizeof(dp)); cout << go(p, 0); cerr << "\n\nTIME: " << (double)(clock() - T) / CLOCKS_PER_SEC << " sec\n"; T = clock(); return 0; }
replace
19
21
19
21
TLE
p03163
C++
Runtime Error
// #define DEB1 //input from input.txt // #define NDEBUG #include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <mutex> #include <queue> #include <set> #include <stack> #include <string> #include <thread> #include <vector> #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define INF(a) memset(a, 0x3f, sizeof(a)) #define ALL(t) (t).begin(), (t).end() #define FILL(a, c) fill(a, a + sizeof(a) / sizeof(*a), c) #define FILL2D(A, c) fill(A[0], A[0] + sizeof(A) / sizeof(**A), c) #define FILL3D(A, c) fill(A[0][0], A[0][0] + sizeof(A) / sizeof(***A), c) #define REP(i, m) for (int i = 0; i < (m); ++i) // closed interval, inverse repeat #define CIREP(i, start, fin) for (int i = (start); i >= (fin); i--) #define CREP(i, start, fin) for (int i = (start); i <= (fin); i++) #define RNG(i, start, fin) for (int i = (start); i < (fin); i++) #define IN(a, x, b) (a <= x && x < b) #define PB push_back #define SZ(x) (int)(x).size() using namespace std; using LL = long long; using PAIR = pair<int, int>; using PAIRLL = pair<LL, LL>; using VI = vector<int>; using VLL = vector<LL>; using VVLL = vector<VLL>; using VVI = vector<VI>; const LL LINF = 1001002003004005006ll; /* template<typename T>inline istream& operator>>(istream&i,vector<T>&v) {REP(j,SZ(v))i>>v[j];return i;} template<typename T>string join(const vector<T>&v) {stringstream s;REP(i,SZ(v))s<<' '<<v[i];return s.str().substr(1);} template<typename T>inline ostream& operator<<(ostream&o,const vector<T>&v) {if(SZ(v))o<<join(v);return o;} */ template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } /* constant values */ // const int dx[] = {1,0,-1,0}; // const int dy[] = {0,1,0,-1}; // const int MX = 31001; // const ll INF= 1LL<<60 ;//INF = 1001001001001; //1e9 // Main program void solve() { // cout << "Hello world" << endl; int N, W; cin >> N >> W; VLL W_vec; VLL V_vec; REP(i, N) { int w, v; cin >> w >> v; W_vec.PB(w); V_vec.PB(v); } VLL dp_old; REP(w, W_vec[0]) { dp_old.PB(0); } CREP(w, W_vec[0], W) { dp_old.PB(V_vec[0]); } VLL dp_new = dp_old; CREP(i, 1, N - 1) { CREP(w, 0, W) { dp_new[w] = max(dp_old[w - W_vec[i]] + V_vec[i], dp_old[w]); } dp_old = dp_new; } cout << dp_new[W] << endl; return; } // ---------------------------------------------- int main() { #ifdef DEB1 cout << "DEBUG MODE" << endl; ifstream in("input.txt"); // for debug cin.rdbuf(in.rdbuf()); // for debug #endif int T = 1; for (int i = 0; i < T; ++i) { solve(); } return 0; }
// #define DEB1 //input from input.txt // #define NDEBUG #include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <mutex> #include <queue> #include <set> #include <stack> #include <string> #include <thread> #include <vector> #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define INF(a) memset(a, 0x3f, sizeof(a)) #define ALL(t) (t).begin(), (t).end() #define FILL(a, c) fill(a, a + sizeof(a) / sizeof(*a), c) #define FILL2D(A, c) fill(A[0], A[0] + sizeof(A) / sizeof(**A), c) #define FILL3D(A, c) fill(A[0][0], A[0][0] + sizeof(A) / sizeof(***A), c) #define REP(i, m) for (int i = 0; i < (m); ++i) // closed interval, inverse repeat #define CIREP(i, start, fin) for (int i = (start); i >= (fin); i--) #define CREP(i, start, fin) for (int i = (start); i <= (fin); i++) #define RNG(i, start, fin) for (int i = (start); i < (fin); i++) #define IN(a, x, b) (a <= x && x < b) #define PB push_back #define SZ(x) (int)(x).size() using namespace std; using LL = long long; using PAIR = pair<int, int>; using PAIRLL = pair<LL, LL>; using VI = vector<int>; using VLL = vector<LL>; using VVLL = vector<VLL>; using VVI = vector<VI>; const LL LINF = 1001002003004005006ll; /* template<typename T>inline istream& operator>>(istream&i,vector<T>&v) {REP(j,SZ(v))i>>v[j];return i;} template<typename T>string join(const vector<T>&v) {stringstream s;REP(i,SZ(v))s<<' '<<v[i];return s.str().substr(1);} template<typename T>inline ostream& operator<<(ostream&o,const vector<T>&v) {if(SZ(v))o<<join(v);return o;} */ template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } /* constant values */ // const int dx[] = {1,0,-1,0}; // const int dy[] = {0,1,0,-1}; // const int MX = 31001; // const ll INF= 1LL<<60 ;//INF = 1001001001001; //1e9 // Main program void solve() { // cout << "Hello world" << endl; int N, W; cin >> N >> W; VLL W_vec; VLL V_vec; REP(i, N) { int w, v; cin >> w >> v; W_vec.PB(w); V_vec.PB(v); } VLL dp_old; REP(w, W_vec[0]) { dp_old.PB(0); } CREP(w, W_vec[0], W) { dp_old.PB(V_vec[0]); } VLL dp_new = dp_old; CREP(i, 1, N - 1) { CREP(w, 0, W) { if (w - W_vec[i] < 0) { dp_new[w] = dp_old[w]; continue; } dp_new[w] = max(dp_old[w - W_vec[i]] + V_vec[i], dp_old[w]); } dp_old = dp_new; } cout << dp_new[W] << endl; return; } // ---------------------------------------------- int main() { #ifdef DEB1 cout << "DEBUG MODE" << endl; ifstream in("input.txt"); // for debug cin.rdbuf(in.rdbuf()); // for debug #endif int T = 1; for (int i = 0; i < T; ++i) { solve(); } return 0; }
insert
101
101
101
105
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int, int>; using vvi = vector<vector<int>>; using vvl = vector<vector<ll>>; #define rep(i, n) for (int i = 0; i < (int)(n); i++) const int INF = 1001001001; int main() { int n, W; cin >> n >> W; vector<int> w(n); vector<ll> v(n); rep(i, n) cin >> w[i] >> v[i]; // dp[i][j] i番目までで、重さWを選ぶ場合の最大価値 vvl dp(n + 1, vector<ll>(W, 0)); for (int i = 0; i < n; i++) { for (int j = 0; j <= W; j++) { if (j - w[i] >= 0) dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]); else dp[i + 1][j] = dp[i][j]; } } cout << dp[n][W] << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int, int>; using vvi = vector<vector<int>>; using vvl = vector<vector<ll>>; #define rep(i, n) for (int i = 0; i < (int)(n); i++) const int INF = 1001001001; int main() { int n, W; cin >> n >> W; vector<int> w(n); vector<ll> v(n); rep(i, n) cin >> w[i] >> v[i]; // dp[i][j] i番目までで、重さWを選ぶ場合の最大価値 vvl dp(n + 1, vector<ll>(W + 1, 0)); for (int i = 0; i < n; i++) { for (int j = 0; j <= W; j++) { if (j - w[i] >= 0) dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]); else dp[i + 1][j] = dp[i][j]; } } cout << dp[n][W] << endl; }
replace
16
17
16
17
0
p03163
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstring> #include <iostream> #include <limits.h> #include <map> #include <string> #include <vector> using namespace std; #define p(s) std::cout << s << endl; #define printIf(j, s1, s2) cout << (j ? s1 : s2) << endl; #define YES(j) cout << (j ? "YES" : "NO") << endl; #define Yes(j) std::cout << (j ? "Yes" : "No") << endl; const int limit = 1e5 + 1; long long int dp[100][limit]; long long int dfs(int depth, int capacity, int n, long long int w[], long long int v[]) { // メモ化してたら返す if (dp[depth][capacity] >= 0) return dp[depth][capacity]; long long int res; if (depth == n) { // 全て考慮した res = 0; } else if (w[depth] > capacity) { // 入らないので次に行く res = dfs(depth + 1, capacity, n, w, v); } else { // 入れる場合と入れない場合の両方試す res = max(dfs(depth + 1, capacity - w[depth], n, w, v) + v[depth], dfs(depth + 1, capacity, n, w, v)); } return dp[depth][capacity] = res; } int main() { int n; long long int w[limit], v[limit]; long long int W; cin >> n >> W; for (int i = 0; i < n; i++) { cin >> w[i] >> v[i]; } memset(dp, -1, sizeof(dp)); p(dfs(0, W, n, w, v)) }
#include <algorithm> #include <cmath> #include <cstring> #include <iostream> #include <limits.h> #include <map> #include <string> #include <vector> using namespace std; #define p(s) std::cout << s << endl; #define printIf(j, s1, s2) cout << (j ? s1 : s2) << endl; #define YES(j) cout << (j ? "YES" : "NO") << endl; #define Yes(j) std::cout << (j ? "Yes" : "No") << endl; const int limit = 1e5 + 1; long long int dp[101][limit]; long long int dfs(int depth, int capacity, int n, long long int w[], long long int v[]) { // メモ化してたら返す if (dp[depth][capacity] >= 0) return dp[depth][capacity]; long long int res; if (depth == n) { // 全て考慮した res = 0; } else if (w[depth] > capacity) { // 入らないので次に行く res = dfs(depth + 1, capacity, n, w, v); } else { // 入れる場合と入れない場合の両方試す res = max(dfs(depth + 1, capacity - w[depth], n, w, v) + v[depth], dfs(depth + 1, capacity, n, w, v)); } return dp[depth][capacity] = res; } int main() { int n; long long int w[limit], v[limit]; long long int W; cin >> n >> W; for (int i = 0; i < n; i++) { cin >> w[i] >> v[i]; } memset(dp, -1, sizeof(dp)); p(dfs(0, W, n, w, v)) }
replace
17
18
17
18
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using uint = unsigned int; ll dp[100][(uint)1e5 + 1]; int main() { int n, W; cin >> n >> W; int w[100]; ll v[100]; for (int i = 0; i < n; i++) cin >> w[i] >> v[i]; for (int i = 0; i < n; i++) { for (int j = 1; j <= W; j++) { dp[i + 1][j] = dp[i][j]; if (j - w[i] < 0) continue; dp[i + 1][j] = max(dp[i + 1][j], dp[i][j - w[i]] + v[i]); } } cout << dp[n][W] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using uint = unsigned int; ll dp[101][(uint)1e5 + 1]; int main() { int n, W; cin >> n >> W; int w[100]; ll v[100]; for (int i = 0; i < n; i++) cin >> w[i] >> v[i]; for (int i = 0; i < n; i++) { for (int j = 1; j <= W; j++) { dp[i + 1][j] = dp[i][j]; if (j - w[i] < 0) continue; dp[i + 1][j] = max(dp[i + 1][j], dp[i][j - w[i]] + v[i]); } } cout << dp[n][W] << endl; return 0; }
replace
5
6
5
6
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> #define vi vector<int> #define vvi vector<vi> #define forr(i, k, n) for (int i = k; i < n; i++) #define iforr(i, k, n) for (int i = n - 1; i >= k; i--) #define rep(i, n) forr(i, 0, n) #define irep(i, n) iforr(i, 0, n) #define aut(i, arr) for (auto &i : arr) #define all(arr) arr.begin(), arr.end() #define pb push_back #define pii pair<int, int> #define vpii vector<pii> #define f first #define db double #define s second #define ll long long #define ull unsigned long long #define E '\n' #define lmax 9223372036854775807; #define MOD 1000000007 using namespace std; vi v; vvi ed; void dfs(int i) { v[i] = 1; aut(j, ed[i]) if (!v[j]) dfs(j); } void Fast_IO() { ios::sync_with_stdio(0); cin.tie(0); #ifndef ONLINE_JUDGE freopen("code.in", "r", stdin); freopen("code.out", "w", stdout); #endif } int main() { Fast_IO(); int t; // cin>>t; t = 1; while (t--) { int n; cin >> n; int k; cin >> k; vi arr(n); vi ar(n); rep(i, n) { cin >> arr[i]; cin >> ar[i]; } vector<vector<ll>> dp(2, vector<ll>(k + 1, -1)); dp[0][0] = 0; int flg = 1; forr(i, 1, n + 1) { dp[flg][0] = 0; forr(j, 1, k + 1) { dp[flg][j] = dp[flg ^ 1][j]; if (arr[i - 1] <= j) if (dp[flg ^ 1][j - arr[i - 1]] >= 0) dp[flg][j] = max(dp[flg][j], ar[i - 1] + dp[flg ^ 1][j - arr[i - 1]]); } flg ^= 1; } ll ans = -1; rep(i, k + 1) ans = max(ans, dp[flg ^ 1][i]); cout << ans; cout << E; } return 0; }
#include <bits/stdc++.h> #define vi vector<int> #define vvi vector<vi> #define forr(i, k, n) for (int i = k; i < n; i++) #define iforr(i, k, n) for (int i = n - 1; i >= k; i--) #define rep(i, n) forr(i, 0, n) #define irep(i, n) iforr(i, 0, n) #define aut(i, arr) for (auto &i : arr) #define all(arr) arr.begin(), arr.end() #define pb push_back #define pii pair<int, int> #define vpii vector<pii> #define f first #define db double #define s second #define ll long long #define ull unsigned long long #define E '\n' #define lmax 9223372036854775807; #define MOD 1000000007 using namespace std; vi v; vvi ed; void dfs(int i) { v[i] = 1; aut(j, ed[i]) if (!v[j]) dfs(j); } void Fast_IO() { ios::sync_with_stdio(0); cin.tie(0); #ifndef ONLINE_JUDGE freopen("code.in", "r", stdin); freopen("code.out", "w", stdout); #endif } int main() { // Fast_IO(); int t; // cin>>t; t = 1; while (t--) { int n; cin >> n; int k; cin >> k; vi arr(n); vi ar(n); rep(i, n) { cin >> arr[i]; cin >> ar[i]; } vector<vector<ll>> dp(2, vector<ll>(k + 1, -1)); dp[0][0] = 0; int flg = 1; forr(i, 1, n + 1) { dp[flg][0] = 0; forr(j, 1, k + 1) { dp[flg][j] = dp[flg ^ 1][j]; if (arr[i - 1] <= j) if (dp[flg ^ 1][j - arr[i - 1]] >= 0) dp[flg][j] = max(dp[flg][j], ar[i - 1] + dp[flg ^ 1][j - arr[i - 1]]); } flg ^= 1; } ll ans = -1; rep(i, k + 1) ans = max(ans, dp[flg ^ 1][i]); cout << ans; cout << E; } return 0; }
replace
37
38
37
38
-6
terminate called after throwing an instance of 'std::length_error' what(): cannot create std::vector larger than max_size()
p03163
C++
Runtime Error
#include <bits/stdc++.h> #define st first #define nd second #define mp make_pair #define pb push_back #define N 100005 using namespace std; typedef long long ll; ll n, k, v[N], w[N], dp[105][N]; ll f(ll kal, ll x) { ll &r = dp[kal][x]; if (r != -1) return r; if (x == n + 1) return 0; r = f(kal, x + 1); if (w[x] <= kal) r = max(r, v[x] + f(kal - w[x], x + 1)); return r; } int main() { // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); memset(dp, -1, sizeof dp); scanf("%lld %lld", &n, &k); for (ll i = 1; i <= n; i++) scanf("%lld %lld", w + i, v + i); printf("%lld\n", f(k, 1)); return 0; }
#include <bits/stdc++.h> #define st first #define nd second #define mp make_pair #define pb push_back #define N 100005 using namespace std; typedef long long ll; ll n, k, v[N], w[N], dp[N][105]; ll f(ll kal, ll x) { ll &r = dp[kal][x]; if (r != -1) return r; if (x == n + 1) return 0; r = f(kal, x + 1); if (w[x] <= kal) r = max(r, v[x] + f(kal - w[x], x + 1)); return r; } int main() { // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); memset(dp, -1, sizeof dp); scanf("%lld %lld", &n, &k); for (ll i = 1; i <= n; i++) scanf("%lld %lld", w + i, v + i); printf("%lld\n", f(k, 1)); return 0; }
replace
8
9
8
9
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long int32_t main() { int n, W; cin >> n >> W; int *dp = new int[W + 1]; for (int i = 1; i <= n; i++) { int weight, value; cin >> weight >> value; for (int j = W; j >= 0; j--) { dp[j + weight] = max(dp[j] + value, dp[j + weight]); } } cout << dp[W]; }
#include <bits/stdc++.h> using namespace std; #define int long long int32_t main() { int n, W; cin >> n >> W; int *dp = new int[W + 1]; for (int i = 1; i <= n; i++) { int weight, value; cin >> weight >> value; for (int j = W; j >= weight; j--) { dp[j] = max(dp[j - weight] + value, dp[j]); } } cout << dp[W]; }
replace
10
12
10
12
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long int ll dp[101][10005] = {0}; ll knapsack(ll a[][2], ll i, ll w, ll n) { if (w <= 0) return 0; if (i == n) return 0; if (dp[i][w] != 0) return dp[i][w]; ll op1 = INT_MIN; if (w - a[i][0] >= 0) op1 = a[i][1] + knapsack(a, i + 1, w - a[i][0], n); ll op2 = knapsack(a, i + 1, w, n); return dp[i][w] = max(op1, op2); } int main() { ll n, w; cin >> n >> w; //{weight, value} ll a[n][2]; // memset(dp,-1,sizeof dp); for (ll i = 0; i < n; i++) cin >> a[i][0] >> a[i][1]; cout << knapsack(a, 0, w, n); }
#include <bits/stdc++.h> using namespace std; #define ll long long int ll dp[101][100005] = {0}; ll knapsack(ll a[][2], ll i, ll w, ll n) { if (w <= 0) return 0; if (i == n) return 0; if (dp[i][w] != 0) return dp[i][w]; ll op1 = INT_MIN; if (w - a[i][0] >= 0) op1 = a[i][1] + knapsack(a, i + 1, w - a[i][0], n); ll op2 = knapsack(a, i + 1, w, n); return dp[i][w] = max(op1, op2); } int main() { ll n, w; cin >> n >> w; //{weight, value} ll a[n][2]; // memset(dp,-1,sizeof dp); for (ll i = 0; i < n; i++) cin >> a[i][0] >> a[i][1]; cout << knapsack(a, 0, w, n); }
replace
3
4
3
4
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #define ALL(x) (x).begin(), (x).end() #define RALL(x) (x).rbegin(), (x).rend() #define DEBUG(x) cout << #x << " = " << (x) << endl; #define SORT(x) sort(ALL(x)); #define RSORT(x) sort(RALL(x)); #define SUM(x) accumulate(ALL(x), 0); using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using pii = pair<int, int>; using tiii = tuple<int, int, int>; const ll mod = 1000000007; const int INF = 1e+9; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; // cout << fixed << setprecision(10); ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } int w[110], v[110]; int dp[110][110] = {}; int main() { int N, W; cin >> N >> W; rep(i, N) cin >> w[i] >> v[i]; for (int n = 0; n < N; n++) { for (int sum_w = 0; sum_w <= W; sum_w++) { if (sum_w - w[n] >= 0) { dp[n + 1][sum_w] = max(dp[n + 1][sum_w], dp[n][sum_w - w[n]] + v[n]); } dp[n + 1][sum_w] = max(dp[n + 1][sum_w], dp[n][sum_w]); } } cout << dp[N][W] << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #define ALL(x) (x).begin(), (x).end() #define RALL(x) (x).rbegin(), (x).rend() #define DEBUG(x) cout << #x << " = " << (x) << endl; #define SORT(x) sort(ALL(x)); #define RSORT(x) sort(RALL(x)); #define SUM(x) accumulate(ALL(x), 0); using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using pii = pair<int, int>; using tiii = tuple<int, int, int>; const ll mod = 1000000007; const int INF = 1e+9; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; // cout << fixed << setprecision(10); ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } int w[110], v[110]; ll dp[110][100100] = {}; int main() { int N, W; cin >> N >> W; rep(i, N) cin >> w[i] >> v[i]; for (int n = 0; n < N; n++) { for (int sum_w = 0; sum_w <= W; sum_w++) { if (sum_w - w[n] >= 0) { dp[n + 1][sum_w] = max(dp[n + 1][sum_w], dp[n][sum_w - w[n]] + v[n]); } dp[n + 1][sum_w] = max(dp[n + 1][sum_w], dp[n][sum_w]); } } cout << dp[N][W] << endl; }
replace
24
25
24
25
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int(i) = (0); (i) < (int)(n); ++(i)) using ll = long long; using namespace std; ll dp[101][101010]; int main() { int N, W; cin >> N >> W; vector<ll> w(N), v(N); rep(i, N) cin >> w[i + 1] >> v[i + 1]; for (int i = 1; i <= N; ++i) { for (int j = W; j >= 0; --j) { if (j - w[i] >= 0) { dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]); } else { dp[i][j] = dp[i - 1][j]; } } } ll ans = 0; for (int i = 0; i <= W; ++i) ans = max(ans, dp[N][i]); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int(i) = (0); (i) < (int)(n); ++(i)) using ll = long long; using namespace std; ll dp[101][101010]; int main() { int N, W; cin >> N >> W; vector<ll> w(N + 1), v(N + 1); rep(i, N) cin >> w[i + 1] >> v[i + 1]; for (int i = 1; i <= N; ++i) { for (int j = W; j >= 0; --j) { if (j - w[i] >= 0) { dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]); } else { dp[i][j] = dp[i - 1][j]; } } } ll ans = 0; for (int i = 0; i <= W; ++i) ans = max(ans, dp[N][i]); cout << ans << endl; }
replace
11
12
11
12
-6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 100, W = 1e5 + 1; ll dp[N][W]; ll v[N], w[N]; ll n, cap; ll rec(int i, ll rem) { if (i > n) { return 0; } ll &ret = dp[i][rem]; if (ret != -1) { return ret; } ret = max(ret, rec(i + 1, rem)); if (w[i] <= rem) { ret = max(ret, rec(i + 1, rem - w[i]) + v[i]); } return ret; } int main() { cin >> n >> cap; memset(dp, -1, sizeof dp); for (int i = 1; i <= n; i++) { cin >> w[i] >> v[i]; } cout << rec(1, cap); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 101, W = 1e5 + 1; ll dp[N][W]; ll v[N], w[N]; ll n, cap; ll rec(int i, ll rem) { if (i > n) { return 0; } ll &ret = dp[i][rem]; if (ret != -1) { return ret; } ret = max(ret, rec(i + 1, rem)); if (w[i] <= rem) { ret = max(ret, rec(i + 1, rem - w[i]) + v[i]); } return ret; } int main() { cin >> n >> cap; memset(dp, -1, sizeof dp); for (int i = 1; i <= n; i++) { cin >> w[i] >> v[i]; } cout << rec(1, cap); return 0; }
replace
4
5
4
5
0