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
p03162
C++
Time Limit Exceeded
// shan61916 #include <bits/stdc++.h> using namespace std; #define IOS \ ios::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define endl "\n" typedef long long ll; typedef unsigned long long ull; typedef double dll; ll n; ll a[100020][3]; ll dp[100020][4]; ll recurse(ll idx, ll type) { if (idx == n) return 0; ll &ans = dp[idx][type]; ans = 0; if (type == 0) { return ans = max({a[idx][0] + recurse(idx + 1, 1), a[idx][1] + recurse(idx + 1, 2), a[idx][2] + recurse(idx + 1, 3)}); } if (type == 1) { return ans = max({a[idx][1] + recurse(idx + 1, 2), a[idx][2] + recurse(idx + 1, 3)}); } if (type == 2) { return ans = max({a[idx][0] + recurse(idx + 1, 1), a[idx][2] + recurse(idx + 1, 3)}); } else { return ans = max({a[idx][0] + recurse(idx + 1, 1), a[idx][1] + recurse(idx + 1, 2)}); } } int main() { IOS #ifdef SHAN freopen("input.txt", "r", stdin); #endif cin >> n; memset(dp, -1, sizeof(dp)); for (ll i = 0; i < n; i++) { for (ll j = 0; j < 3; j++) cin >> a[i][j]; } ll ans = recurse(0, 0); cout << ans << endl; return 0; } // good night.
// shan61916 #include <bits/stdc++.h> using namespace std; #define IOS \ ios::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define endl "\n" typedef long long ll; typedef unsigned long long ull; typedef double dll; ll n; ll a[100020][3]; ll dp[100020][4]; ll recurse(ll idx, ll type) { if (idx == n) return 0; ll &ans = dp[idx][type]; if (ans != -1) return ans; ans = 0; if (type == 0) { return ans = max({a[idx][0] + recurse(idx + 1, 1), a[idx][1] + recurse(idx + 1, 2), a[idx][2] + recurse(idx + 1, 3)}); } if (type == 1) { return ans = max({a[idx][1] + recurse(idx + 1, 2), a[idx][2] + recurse(idx + 1, 3)}); } if (type == 2) { return ans = max({a[idx][0] + recurse(idx + 1, 1), a[idx][2] + recurse(idx + 1, 3)}); } else { return ans = max({a[idx][0] + recurse(idx + 1, 1), a[idx][1] + recurse(idx + 1, 2)}); } } int main() { IOS #ifdef SHAN freopen("input.txt", "r", stdin); #endif cin >> n; memset(dp, -1, sizeof(dp)); for (ll i = 0; i < n; i++) { for (ll j = 0; j < 3; j++) cin >> a[i][j]; } ll ans = recurse(0, 0); cout << ans << endl; return 0; } // good night.
insert
18
18
18
20
TLE
p03162
C++
Runtime Error
// 01/06/2020 @ 12:20:00 // ***************************************************************** // ***************************************************************** #include <bits/stdc++.h> // ***************************************************************** // ***************************************************************** #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define fr(i, n) for (llu i = 0; i < n; i++) #define fl(j, a, b) for (llu j = a; j <= b; j++) #define pi 3.14159265 #define mod 1e9 + 7 #define MID(a, b) ((a + b) >> 1) #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b)) // ***************************************************************** // ***************************************************************** typedef long long unsigned int llu; typedef long long int ll; // ***************************************************************** // ***************************************************************** using namespace std; // ***************************************************************** // ***************************************************************** // Dream it. Wish it. Do it. // tannatsri // // Without tact you can learn nothing. // ***************************************************************** // ***************************************************************** bool rev(int a, int b) { return a > b; } // ***************************************************************** // ***************************************************************** int main() { IOS; #ifndef ONLINE_JUDGE // for getting input from input.txt freopen("C:/Users/Mr.Tanishq/Desktop/noc/input.txt", "r", stdin); // for writing output to output.txt freopen("C:/Users/Mr.Tanishq/Desktop/noc/output.txt", "w", stdout); #endif // ***************************************************************** // ***************************************************************** int t = 1; while (t--) { int n; cin >> n; ll dp[n][3]; fr(i, n) { fr(j, 3) cin >> dp[i][j]; } for (int i = n - 2; i > -1; i--) { for (int j = 0; j < 3; ++j) { int x = 0; if (j == 0) { x = MAX(dp[i + 1][1], dp[i + 1][2]); } else if (j == 1) { x = MAX(dp[i + 1][0], dp[i + 1][2]); } else { x = MAX(dp[i + 1][0], dp[i + 1][1]); } dp[i][j] += x; } } cout << MAX(dp[0][1], MAX(dp[0][2], dp[0][0])) << "\n"; } return 0; }
// 01/06/2020 @ 12:20:00 // ***************************************************************** // ***************************************************************** #include <bits/stdc++.h> // ***************************************************************** // ***************************************************************** #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define fr(i, n) for (llu i = 0; i < n; i++) #define fl(j, a, b) for (llu j = a; j <= b; j++) #define pi 3.14159265 #define mod 1e9 + 7 #define MID(a, b) ((a + b) >> 1) #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b)) // ***************************************************************** // ***************************************************************** typedef long long unsigned int llu; typedef long long int ll; // ***************************************************************** // ***************************************************************** using namespace std; // ***************************************************************** // ***************************************************************** // Dream it. Wish it. Do it. // tannatsri // // Without tact you can learn nothing. // ***************************************************************** // ***************************************************************** bool rev(int a, int b) { return a > b; } // ***************************************************************** // ***************************************************************** int main() { IOS; // #ifndef ONLINE_JUDGE // // for getting input from input.txt // freopen("C:/Users/Mr.Tanishq/Desktop/noc/input.txt", "r", stdin); // // for writing output to output.txt // freopen("C:/Users/Mr.Tanishq/Desktop/noc/output.txt", "w", stdout); // #endif // ***************************************************************** // ***************************************************************** int t = 1; while (t--) { int n; cin >> n; ll dp[n][3]; fr(i, n) { fr(j, 3) cin >> dp[i][j]; } for (int i = n - 2; i > -1; i--) { for (int j = 0; j < 3; ++j) { int x = 0; if (j == 0) { x = MAX(dp[i + 1][1], dp[i + 1][2]); } else if (j == 1) { x = MAX(dp[i + 1][0], dp[i + 1][2]); } else { x = MAX(dp[i + 1][0], dp[i + 1][1]); } dp[i][j] += x; } } cout << MAX(dp[0][1], MAX(dp[0][2], dp[0][0])) << "\n"; } return 0; }
replace
40
46
40
46
0
p03162
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<vi> vii; typedef vector<ll> vl; typedef vector<vl> vll; typedef set<int> si; typedef map<int, int> mii; typedef map<ll, ll> mll; typedef map<string, int> msi; typedef set<ll> sl; typedef set<char> sc; typedef pair<int, int> pii; #define REP(i, n) for (int i = 0; i < (n); i++) #define FOR(i, a, b) for (int i = (a); i <= (b); i++) #define FORD(i, a, b) for (int i = (a); i >= (b); i--) const int INF = 1 << 29; #define MOD (ll)(1e9 + 7) #define eps 1e-6 #define pb push_back #define mp make_pair #define DEBUG(x) cout << '>' << #x << ':' << x << endl; #define F first #define S second int main() { ios::sync_with_stdio(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int n; cin >> n; int a[n][3]; REP(i, n) { cin >> a[i][0] >> a[i][1] >> a[i][2]; } int dp[n + 5][3]; dp[0][0] = 0; dp[0][1] = 0; dp[0][2] = 0; dp[1][0] = a[0][0]; dp[1][1] = a[0][1]; dp[1][2] = a[0][2]; for (int i = 2; i <= n; ++i) { dp[i][0] = a[i - 1][0] + max(dp[i - 1][1], dp[i - 1][2]); dp[i][1] = a[i - 1][1] + max(dp[i - 1][0], dp[i - 1][2]); dp[i][2] = a[i - 1][2] + max(dp[i - 1][0], dp[i - 1][1]); } cout << max(dp[n][0], max(dp[n][1], dp[n][2])) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<vi> vii; typedef vector<ll> vl; typedef vector<vl> vll; typedef set<int> si; typedef map<int, int> mii; typedef map<ll, ll> mll; typedef map<string, int> msi; typedef set<ll> sl; typedef set<char> sc; typedef pair<int, int> pii; #define REP(i, n) for (int i = 0; i < (n); i++) #define FOR(i, a, b) for (int i = (a); i <= (b); i++) #define FORD(i, a, b) for (int i = (a); i >= (b); i--) const int INF = 1 << 29; #define MOD (ll)(1e9 + 7) #define eps 1e-6 #define pb push_back #define mp make_pair #define DEBUG(x) cout << '>' << #x << ':' << x << endl; #define F first #define S second int main() { ios::sync_with_stdio(0); int n; cin >> n; int a[n][3]; REP(i, n) { cin >> a[i][0] >> a[i][1] >> a[i][2]; } int dp[n + 5][3]; dp[0][0] = 0; dp[0][1] = 0; dp[0][2] = 0; dp[1][0] = a[0][0]; dp[1][1] = a[0][1]; dp[1][2] = a[0][2]; for (int i = 2; i <= n; ++i) { dp[i][0] = a[i - 1][0] + max(dp[i - 1][1], dp[i - 1][2]); dp[i][1] = a[i - 1][1] + max(dp[i - 1][0], dp[i - 1][2]); dp[i][2] = a[i - 1][2] + max(dp[i - 1][0], dp[i - 1][1]); } cout << max(dp[n][0], max(dp[n][1], dp[n][2])) << endl; return 0; }
delete
31
35
31
31
-11
p03162
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long ll a[10005], b[10005], c[10005], n; ll dp[10005][4]; ll solve(ll day, ll activity) { if (day == n + 1) { return 0; } if (dp[day][activity] != -1) { return dp[day][activity]; } if (activity == 1) { return dp[day][activity] = a[day] + max(solve(day + 1, 2), solve(day + 1, 3)); } else if (activity == 2) { return dp[day][activity] = b[day] + max(solve(day + 1, 1), solve(day + 1, 3)); } else if (activity == 3) { return dp[day][activity] = c[day] + max(solve(day + 1, 1), solve(day + 1, 2)); } } int main() { cin >> n; for (ll i = 1; i <= n; i++) { cin >> a[i] >> b[i] >> c[i]; } memset(dp, -1, sizeof(dp)); cout << max(max(solve(1, 1), solve(1, 2)), solve(1, 3)); return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long ll a[100005], b[100005], c[100005], n; ll dp[100005][4]; ll solve(ll day, ll activity) { if (day == n + 1) { return 0; } if (dp[day][activity] != -1) { return dp[day][activity]; } if (activity == 1) { return dp[day][activity] = a[day] + max(solve(day + 1, 2), solve(day + 1, 3)); } else if (activity == 2) { return dp[day][activity] = b[day] + max(solve(day + 1, 1), solve(day + 1, 3)); } else if (activity == 3) { return dp[day][activity] = c[day] + max(solve(day + 1, 1), solve(day + 1, 2)); } } int main() { cin >> n; for (ll i = 1; i <= n; i++) { cin >> a[i] >> b[i] >> c[i]; } memset(dp, -1, sizeof(dp)); cout << max(max(solve(1, 1), solve(1, 2)), solve(1, 3)); return 0; }
replace
4
6
4
6
0
p03162
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define FAST_IO \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define mod 1000000007 #define f(i, n) for (int i = 0; i < n; i++) #define INF 1e9 + 5 #define LINF 1e18L + 5; #define pb push_back #define mk make_pair using namespace std; /************************BELIAL**********DEVIL*********BELIAL***********DEVIL*************************/ int main() { int n; cin >> n; // vector<int> arr(n); vector<int> dp(3, 0); // maintain ans of prev state for (int days = 0; days < n; days++) { vector<int> c(3); // for a,b,c vector<int> new_dp(3, 0); for (int i = 0; i < 3; i++) cin >> c[i]; for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) { if (i != j) { new_dp[j] = max(new_dp[j], dp[i] + c[j]); // ans of cur state } } } dp = new_dp; } cout << max(max(dp[0], dp[1]), dp[2]); }
#include <bits/stdc++.h> #define ll long long #define FAST_IO \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define mod 1000000007 #define f(i, n) for (int i = 0; i < n; i++) #define INF 1e9 + 5 #define LINF 1e18L + 5; #define pb push_back #define mk make_pair using namespace std; /************************BELIAL**********DEVIL*********BELIAL***********DEVIL*************************/ int main() { int n; cin >> n; // vector<int> arr(n); vector<int> dp(3, 0); // maintain ans of prev state for (int days = 0; days < n; days++) { vector<int> c(3); // for a,b,c vector<int> new_dp(3, 0); for (int i = 0; i < 3; i++) cin >> c[i]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (i != j) { new_dp[j] = max(new_dp[j], dp[i] + c[j]); // ans of cur state } } } dp = new_dp; } cout << max(max(dp[0], dp[1]), dp[2]); }
replace
28
29
28
29
0
p03162
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int dpa[10000], dpb[10000], dpc[10000]; dpa[0] = dpb[0] = dpc[0] = 0; for (int i = 1; i <= n; i++) { int a, b, c; cin >> a >> b >> c; dpa[i] = a + max(dpb[i - 1], dpc[i - 1]); dpb[i] = b + max(dpc[i - 1], dpa[i - 1]); dpc[i] = c + max(dpa[i - 1], dpb[i - 1]); } cout << max({dpa[n], dpb[n], dpc[n]}) << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int dpa[100001], dpb[100001], dpc[100001]; dpa[0] = dpb[0] = dpc[0] = 0; for (int i = 1; i <= n; i++) { int a, b, c; cin >> a >> b >> c; dpa[i] = a + max(dpb[i - 1], dpc[i - 1]); dpb[i] = b + max(dpc[i - 1], dpa[i - 1]); dpc[i] = c + max(dpa[i - 1], dpb[i - 1]); } cout << max({dpa[n], dpb[n], dpc[n]}) << endl; }
replace
6
7
6
7
0
p03162
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll inf = 1e18 + 10; const ll N = 1e3 + 10; ll n, x, y, z, pr, mx; vector<ll> v[N]; ll dp[N][3]; ll func(ll ind, ll pr) { ll sum = 0; if (ind == n) return 0; if (dp[ind][pr] != -1) return dp[ind][pr]; if (pr == 0) { sum += max(v[ind][1] + func(ind + 1, 1), v[ind][2] + func(ind + 1, 2)); } else if (pr == 1) { sum += max(v[ind][0] + func(ind + 1, 0), v[ind][2] + func(ind + 1, 2)); } else { sum += max(v[ind][0] + func(ind + 1, 0), v[ind][1] + func(ind + 1, 1)); } dp[ind][pr] = sum; return sum; } void solve() { cin >> n; memset(dp, -1, sizeof dp); for (ll i = 0; i < n; i++) { cin >> x >> y >> z; // cout << x << " " << y << " " << z << endl; v[i].push_back(x); v[i].push_back(y); v[i].push_back(z); } for (ll i = 0; i < 3; i++) { mx = max(mx, func(1, i) + v[0][i]); } cout << mx << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll t; t = 1; // cin >> t ; while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll inf = 1e18 + 10; const ll N = 1e5 + 10; ll n, x, y, z, pr, mx; vector<ll> v[N]; ll dp[N][3]; ll func(ll ind, ll pr) { ll sum = 0; if (ind == n) return 0; if (dp[ind][pr] != -1) return dp[ind][pr]; if (pr == 0) { sum += max(v[ind][1] + func(ind + 1, 1), v[ind][2] + func(ind + 1, 2)); } else if (pr == 1) { sum += max(v[ind][0] + func(ind + 1, 0), v[ind][2] + func(ind + 1, 2)); } else { sum += max(v[ind][0] + func(ind + 1, 0), v[ind][1] + func(ind + 1, 1)); } dp[ind][pr] = sum; return sum; } void solve() { cin >> n; memset(dp, -1, sizeof dp); for (ll i = 0; i < n; i++) { cin >> x >> y >> z; // cout << x << " " << y << " " << z << endl; v[i].push_back(x); v[i].push_back(y); v[i].push_back(z); } for (ll i = 0; i < 3; i++) { mx = max(mx, func(1, i) + v[0][i]); } cout << mx << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll t; t = 1; // cin >> t ; while (t--) solve(); return 0; }
replace
4
5
4
5
0
p03162
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long int const int INF = 1e9 + 5; #undef int int main() { int n, k; scanf("%d", &n); vector<int> dp(3, 0); vector<int> arr(3, 0); dp[0] = 0; for (int i = 0; i < n; i++) { vector<int> new_dp(3, 0); for (int j = 0; j < 3; j++) scanf("%d", &arr[j]); for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { if (j != k) { new_dp[j] = max(new_dp[j], dp[i] + arr[j]); } } } dp = new_dp; } printf("%d", max(max(dp[0], dp[1]), dp[2])); }
#include <bits/stdc++.h> using namespace std; #define int long long int const int INF = 1e9 + 5; #undef int int main() { int n, k; scanf("%d", &n); vector<int> dp(3, 0); vector<int> arr(3, 0); dp[0] = 0; for (int i = 0; i < n; i++) { vector<int> new_dp(3, 0); for (int j = 0; j < 3; j++) scanf("%d", &arr[j]); for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { if (j != k) { new_dp[j] = max(new_dp[j], dp[k] + arr[j]); } } } dp = new_dp; } printf("%d", max(max(dp[0], dp[1]), dp[2])); }
replace
24
25
24
25
0
p03162
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll unsigned long long map<pair<int, int>, int> m; int ans; int solve(vector<vector<int>> &mat, int day, int item) { if (day >= mat.size()) { return 0; } if (m.find({day, item}) != m.end()) return m[{day, item}]; int temp = 0; for (int j = 0; j < 3; j++) { if (j != item) temp = max(temp, mat[day][item] + solve(mat, day + 1, j)); } return m[{day, item}] = temp; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #ifndef ONLINE_JUDGE freopen("D:/CP/input.txt", "r", stdin); freopen("D:/CP/output.txt", "w", stdout); #endif int n; cin >> n; vector<vector<int>> mat(n, vector<int>(3)); for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) { cin >> mat[i][j]; } } ans = 0; for (int i = 0; i < 3; i++) { ans = max(ans, solve(mat, 0, i)); } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll unsigned long long map<pair<int, int>, int> m; int ans; int solve(vector<vector<int>> &mat, int day, int item) { if (day >= mat.size()) { return 0; } if (m.find({day, item}) != m.end()) return m[{day, item}]; int temp = 0; for (int j = 0; j < 3; j++) { if (j != item) temp = max(temp, mat[day][item] + solve(mat, day + 1, j)); } return m[{day, item}] = temp; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; vector<vector<int>> mat(n, vector<int>(3)); for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) { cin >> mat[i][j]; } } ans = 0; for (int i = 0; i < 3; i++) { ans = max(ans, solve(mat, 0, i)); } cout << ans; return 0; }
delete
22
26
22
22
0
p03162
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define mod 1000000007 #define pll pair<long long, long long> #define pdd pair<long double, long double> #define mp make_pair #define pb push_back priority_queue<pll, vector<pll>, greater<pll>> pq; #define ll long long int main() { int n; cin >> n; vector<ll> a(n + 1), b(n + 1), c(n + 1); for (int i = 1; i <= n; i++) { cin >> a[i] >> b[i] >> c[i]; } vector<vector<ll>> dp(4, vector<ll>(n + 1, 0)); for (int i = 1; i <= n; i++) { // cout<<"sgfd"; dp[i][1] = a[i] + max(dp[i - 1][2], dp[i - 1][3]); dp[i][2] = b[i] + max(dp[i - 1][1], dp[i - 1][3]); dp[i][3] = c[i] + max(dp[i - 1][2], dp[i - 1][1]); } cout << max(dp[n][1], max(dp[n][2], dp[n][3])); return 0; }
#include <bits/stdc++.h> using namespace std; #define mod 1000000007 #define pll pair<long long, long long> #define pdd pair<long double, long double> #define mp make_pair #define pb push_back priority_queue<pll, vector<pll>, greater<pll>> pq; #define ll long long int main() { int n; cin >> n; vector<ll> a(n + 1), b(n + 1), c(n + 1); for (int i = 1; i <= n; i++) { cin >> a[i] >> b[i] >> c[i]; } vector<vector<ll>> dp(n + 1, vector<ll>(4, 0)); for (int i = 1; i <= n; i++) { // cout<<"sgfd"; dp[i][1] = a[i] + max(dp[i - 1][2], dp[i - 1][3]); dp[i][2] = b[i] + max(dp[i - 1][1], dp[i - 1][3]); dp[i][3] = c[i] + max(dp[i - 1][2], dp[i - 1][1]); } cout << max(dp[n][1], max(dp[n][2], dp[n][3])); return 0; }
replace
18
19
18
19
0
p03162
C++
Runtime Error
#include <algorithm> #include <climits> #include <cmath> #include <iostream> #include <set> #include <string> #include <unordered_map> #include <vector> #define loopi(x, y) for (int i = x; i < (y); i++) #define loopj(x, y) for (int j = x; j < (y); j++) #define nl cout << "\n"; #define ll long long int using namespace std; #define mod 1000000007 int main() { ios ::sync_with_stdio(false); cin.tie(nullptr); // ios_base :: sync_with_stdio(false); // cin.tie(NULL) #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int n; cin >> n; int a, b, c; int dp[n][3]; cin >> dp[0][0] >> dp[0][1] >> dp[0][2]; loopi(1, n) { cin >> a >> b >> c; dp[i][0] = a + max(dp[i - 1][1], dp[i - 1][2]); dp[i][1] = b + max(dp[i - 1][0], dp[i - 1][2]); dp[i][2] = c + max(dp[i - 1][0], dp[i - 1][1]); } cout << max(dp[n - 1][0], max(dp[n - 1][1], dp[n - 1][2])); return 0; }
#include <algorithm> #include <climits> #include <cmath> #include <iostream> #include <set> #include <string> #include <unordered_map> #include <vector> #define loopi(x, y) for (int i = x; i < (y); i++) #define loopj(x, y) for (int j = x; j < (y); j++) #define nl cout << "\n"; #define ll long long int using namespace std; #define mod 1000000007 int main() { ios ::sync_with_stdio(false); cin.tie(nullptr); // ios_base :: sync_with_stdio(false); // cin.tie(NULL) // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif int n; cin >> n; int a, b, c; int dp[n][3]; cin >> dp[0][0] >> dp[0][1] >> dp[0][2]; loopi(1, n) { cin >> a >> b >> c; dp[i][0] = a + max(dp[i - 1][1], dp[i - 1][2]); dp[i][1] = b + max(dp[i - 1][0], dp[i - 1][2]); dp[i][2] = c + max(dp[i - 1][0], dp[i - 1][1]); } cout << max(dp[n - 1][0], max(dp[n - 1][1], dp[n - 1][2])); return 0; }
replace
24
28
24
28
0
p03162
C++
Runtime Error
#include <cstdlib> #include <iostream> #include <vector> const long long MOD = 10e9 + 7; using namespace std; typedef long long ll; long long f(long long i) { long long n = i; int mul = 1; while (i / 10 != 0) { int i1 = i % 10; int i2 = (i / 10) % 10; if (i1 == i2) { n = n - i1 * mul; } i = i / 10; mul *= 10; } return n; } int main() { // int mi=1000000000; // cout<<mi<<endl; int N, a, b, c; vector<int> va, vb, vc, aa, ab, ac; cin >> N; for (int i = 0; i < N; i++) { cin >> a >> b >> c; va.push_back(a); vc.push_back(c); vb.push_back(b); } aa.push_back(va[0]); ab.push_back(vb[0]); ac.push_back(vc[0]); for (int i = 1; i < N; i++) { aa[i] = max(ab[i - 1], ac[i - 1]) + va[i]; ab[i] = max(aa[i - 1], ac[i - 1]) + vb[i]; ac[i] = max(ab[i - 1], aa[i - 1]) + vc[i]; } cout << max(max(aa[N - 1], ab[N - 1]), ac[N - 1]) << endl; }
#include <cstdlib> #include <iostream> #include <vector> const long long MOD = 10e9 + 7; using namespace std; typedef long long ll; long long f(long long i) { long long n = i; int mul = 1; while (i / 10 != 0) { int i1 = i % 10; int i2 = (i / 10) % 10; if (i1 == i2) { n = n - i1 * mul; } i = i / 10; mul *= 10; } return n; } int main() { // int mi=1000000000; // cout<<mi<<endl; int N, a, b, c; vector<int> va, vb, vc, aa, ab, ac; cin >> N; for (int i = 0; i < N; i++) { cin >> a >> b >> c; va.push_back(a); vc.push_back(c); vb.push_back(b); } aa.push_back(va[0]); ab.push_back(vb[0]); ac.push_back(vc[0]); for (int i = 1; i < N; i++) { aa.push_back(max(ab[i - 1], ac[i - 1]) + va[i]); ab.push_back(max(aa[i - 1], ac[i - 1]) + vb[i]); ac.push_back(max(ab[i - 1], aa[i - 1]) + vc[i]); } cout << max(max(aa[N - 1], ab[N - 1]), ac[N - 1]) << endl; }
replace
42
45
42
45
0
p03162
C++
Runtime Error
/* ███╗ ███╗ █████╗ ███████╗███╗ ███╗ ██████╗ ██╗ ██╗██████╗ ███████╗ █████╗ ███╗ ███╗██████╗ ██╗ ██╗ ████╗ ████║██╔══██╗╚════██║████╗ ████║██╔═══██╗██║ ██║██╔══██╗╚════██║██╔══██╗████╗ ████║██╔══██╗╚██╗ ██╔╝ ██╔████╔██║███████║ ██╔╝██╔████╔██║██║ ██║██║ ██║██║ ██║ ██╔╝███████║██╔████╔██║██║ ██║ ╚████╔╝ ██║╚██╔╝██║██╔══██║ ██╔╝ ██║╚██╔╝██║██║ ██║██║ ██║██║ ██║ ██╔╝ ██╔══██║██║╚██╔╝██║██║ ██║ ╚██╔╝ ██║ ╚═╝ ██║██║ ██║ ██║ ██║ ╚═╝ ██║╚██████╔╝╚██████╔╝██████╔╝██╗██║ ██║ ██║██║ ╚═╝ ██║██████╔╝ ██║ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═════╝ ╚═╝╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═════╝ ╚═╝ “Nobody but you have to believe in your dreams to make them a reality.” ― Germany Kent */ #define _CRT_SECURE_NO_WARNINGS #include <bits/stdc++.h> using namespace std; #define Ma7moud_7amdy \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) #define Open_Sesame Open() #define pb push_back #define all(v) ((v).begin()), ((v).end()) #define allr(v) ((v).rbegin()), ((v).rend()) #define sz(v) ((int)((v).size())) #define clr(arr, x) memset(arr, x, sizeof arr) #define endl "\n" #define Accepted 0 #define watch(x) cout << #x << " = " << x << endl; #define un_map unordered_map #define RT(x) return cout << (x), 0; #define sc(a) scanf("%d", &a) #define scll(x) scanf("%I64d", &x) #define scan(s) scanf("%[^\n]%*c", &s) #define pr(s) printf("%s", s.c_str()); // print string typedef long long ll; typedef unsigned long long ull; const double PI = acos(-1.0); const double EPS = -1e-2; const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}; const int dy[] = {0, 1, 0, -1, 1, -1, 1, -1}; const ll mod = 998244353; const int Mx = INT_MAX; const int Mn = INT_MIN; const ll MX = LLONG_MAX; const ll MN = LLONG_MIN; const int N = 1e5 + 5; void Open() { #ifndef ONLINE_JUDGE freopen("Input.txt", "r", stdin); freopen("Output.txt", "w", stdout); #endif } struct Vacation { int Swim, bugs, homework; }; int n; vector<Vacation> v; int mem[N][4]; int solve(int idx = 0, int prev = 0) { if (idx == n) return 0; int &ret = mem[idx][prev]; if (~ret) return ret; ret = 0; if (prev != 1) ret = max(ret, solve(idx + 1, 1) + v[idx].Swim); if (prev != 2) ret = max(ret, solve(idx + 1, 2) + v[idx].bugs); if (prev != 3) ret = max(ret, solve(idx + 1, 3) + v[idx].homework); return ret; } int main() { Ma7moud_7amdy; Open_Sesame; clr(mem, -1); cin >> n; v = vector<Vacation>(n); for (int i = 0; i < n; i++) cin >> v[i].Swim >> v[i].bugs >> v[i].homework; cout << solve(); }
/* ███╗ ███╗ █████╗ ███████╗███╗ ███╗ ██████╗ ██╗ ██╗██████╗ ███████╗ █████╗ ███╗ ███╗██████╗ ██╗ ██╗ ████╗ ████║██╔══██╗╚════██║████╗ ████║██╔═══██╗██║ ██║██╔══██╗╚════██║██╔══██╗████╗ ████║██╔══██╗╚██╗ ██╔╝ ██╔████╔██║███████║ ██╔╝██╔████╔██║██║ ██║██║ ██║██║ ██║ ██╔╝███████║██╔████╔██║██║ ██║ ╚████╔╝ ██║╚██╔╝██║██╔══██║ ██╔╝ ██║╚██╔╝██║██║ ██║██║ ██║██║ ██║ ██╔╝ ██╔══██║██║╚██╔╝██║██║ ██║ ╚██╔╝ ██║ ╚═╝ ██║██║ ██║ ██║ ██║ ╚═╝ ██║╚██████╔╝╚██████╔╝██████╔╝██╗██║ ██║ ██║██║ ╚═╝ ██║██████╔╝ ██║ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═════╝ ╚═╝╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═════╝ ╚═╝ “Nobody but you have to believe in your dreams to make them a reality.” ― Germany Kent */ #define _CRT_SECURE_NO_WARNINGS #include <bits/stdc++.h> using namespace std; #define Ma7moud_7amdy \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) #define Open_Sesame Open() #define pb push_back #define all(v) ((v).begin()), ((v).end()) #define allr(v) ((v).rbegin()), ((v).rend()) #define sz(v) ((int)((v).size())) #define clr(arr, x) memset(arr, x, sizeof arr) #define endl "\n" #define Accepted 0 #define watch(x) cout << #x << " = " << x << endl; #define un_map unordered_map #define RT(x) return cout << (x), 0; #define sc(a) scanf("%d", &a) #define scll(x) scanf("%I64d", &x) #define scan(s) scanf("%[^\n]%*c", &s) #define pr(s) printf("%s", s.c_str()); // print string typedef long long ll; typedef unsigned long long ull; const double PI = acos(-1.0); const double EPS = -1e-2; const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}; const int dy[] = {0, 1, 0, -1, 1, -1, 1, -1}; const ll mod = 998244353; const int Mx = INT_MAX; const int Mn = INT_MIN; const ll MX = LLONG_MAX; const ll MN = LLONG_MIN; const int N = 1e5 + 5; void Open() { #ifndef ONLINE_JUDGE freopen("Input.txt", "r", stdin); freopen("Output.txt", "w", stdout); #endif } struct Vacation { int Swim, bugs, homework; }; int n; vector<Vacation> v; int mem[N][4]; int solve(int idx = 0, int prev = 0) { if (idx == n) return 0; int &ret = mem[idx][prev]; if (~ret) return ret; ret = 0; if (prev != 1) ret = max(ret, solve(idx + 1, 1) + v[idx].Swim); if (prev != 2) ret = max(ret, solve(idx + 1, 2) + v[idx].bugs); if (prev != 3) ret = max(ret, solve(idx + 1, 3) + v[idx].homework); return ret; } int main() { Ma7moud_7amdy; // Open_Sesame; clr(mem, -1); cin >> n; v = vector<Vacation>(n); for (int i = 0; i < n; i++) cin >> v[i].Swim >> v[i].bugs >> v[i].homework; cout << solve(); }
replace
78
79
78
79
0
p03162
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define rep2(i, x, n) for (int i = x; i < (n); i++) #define all(n) begin(n), end(n) struct cww { cww() { ios::sync_with_stdio(false); cin.tie(0); } } star; const long long INF = numeric_limits<long long>::max(); int a[11000], b[11000], c[11000]; long long dp[110000][3]; // dp[i][j]:= // 最後にjをした時のi日目までの幸福度の最大値 int main() { int n; cin >> n; rep(i, n) { cin >> a[i] >> b[i] >> c[i]; } dp[0][0] = 0; dp[0][1] = 0; dp[0][2] = 0; rep(i, n) { dp[i + 1][0] = max(dp[i][1] + b[i], dp[i][2] + c[i]); dp[i + 1][1] = max(dp[i][0] + a[i], dp[i][2] + c[i]); dp[i + 1][2] = max(dp[i][1] + b[i], dp[i][0] + a[i]); } long long ans = -100000000000; rep(i, 3) { ans = max(ans, dp[n][i]); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define rep2(i, x, n) for (int i = x; i < (n); i++) #define all(n) begin(n), end(n) struct cww { cww() { ios::sync_with_stdio(false); cin.tie(0); } } star; const long long INF = numeric_limits<long long>::max(); int a[110000], b[110000], c[110000]; long long dp[110000][3]; // dp[i][j]:= // 最後にjをした時のi日目までの幸福度の最大値 int main() { int n; cin >> n; rep(i, n) { cin >> a[i] >> b[i] >> c[i]; } dp[0][0] = 0; dp[0][1] = 0; dp[0][2] = 0; rep(i, n) { dp[i + 1][0] = max(dp[i][1] + b[i], dp[i][2] + c[i]); dp[i + 1][1] = max(dp[i][0] + a[i], dp[i][2] + c[i]); dp[i + 1][2] = max(dp[i][1] + b[i], dp[i][0] + a[i]); } long long ans = -100000000000; rep(i, 3) { ans = max(ans, dp[n][i]); } cout << ans << endl; return 0; }
replace
12
13
12
13
0
p03162
C++
Time 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; cin >> N; vector<ll> a(N + 5), b(N + 5), c(N + 5); vector<vector<ll>> dp(N + 5, vector<ll>(N + 5)); rep(i, N) cin >> a[i] >> b[i] >> c[i]; dp[0][0] = a[0]; dp[0][1] = b[0]; dp[0][2] = c[0]; if (N <= 1) { cout << max(max(dp[0][0], dp[0][1]), dp[0][2]) << endl; } else { REP(i, 1, N) { dp[i][0] = max(dp[i - 1][1] + a[i], dp[i - 1][2] + a[i]); dp[i][1] = max(dp[i - 1][2] + b[i], dp[i - 1][0] + b[i]); dp[i][2] = max(dp[i - 1][0] + c[i], dp[i - 1][1] + c[i]); } cout << max(max(dp[N - 1][0], dp[N - 1][1]), dp[N - 1][2]) << 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; cin >> N; vector<ll> a(N + 5), b(N + 5), c(N + 5); vector<vector<ll>> dp(N + 5, vector<ll>(3)); rep(i, N) cin >> a[i] >> b[i] >> c[i]; dp[0][0] = a[0]; dp[0][1] = b[0]; dp[0][2] = c[0]; if (N <= 1) { cout << max(max(dp[0][0], dp[0][1]), dp[0][2]) << endl; } else { REP(i, 1, N) { dp[i][0] = max(dp[i - 1][1] + a[i], dp[i - 1][2] + a[i]); dp[i][1] = max(dp[i - 1][2] + b[i], dp[i - 1][0] + b[i]); dp[i][2] = max(dp[i - 1][0] + c[i], dp[i - 1][1] + c[i]); } cout << max(max(dp[N - 1][0], dp[N - 1][1]), dp[N - 1][2]) << endl; } return 0; }
replace
32
33
32
33
TLE
p03162
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int arr[n][3], dp[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) cin >> arr[i][j]; } dp[0][0] = arr[0][0]; dp[0][1] = arr[0][1]; dp[0][2] = arr[0][2]; for (int i = 1; i < n; i++) { dp[i][0] = arr[i][0] + max(dp[i - 1][1], dp[i - 1][2]); dp[i][1] = arr[i][1] + max(dp[i - 1][0], dp[i - 1][2]); dp[i][2] = arr[i][2] + max(dp[i - 1][0], dp[i - 1][1]); } cout << max(dp[n - 1][0], max(dp[n - 1][1], dp[n - 1][2])); }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int arr[n][3], dp[n][3]; for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) cin >> arr[i][j]; } dp[0][0] = arr[0][0]; dp[0][1] = arr[0][1]; dp[0][2] = arr[0][2]; for (int i = 1; i < n; i++) { dp[i][0] = arr[i][0] + max(dp[i - 1][1], dp[i - 1][2]); dp[i][1] = arr[i][1] + max(dp[i - 1][0], dp[i - 1][2]); dp[i][2] = arr[i][2] + max(dp[i - 1][0], dp[i - 1][1]); } cout << max(dp[n - 1][0], max(dp[n - 1][1], dp[n - 1][2])); }
replace
5
6
5
6
0
p03162
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define MOD 1000000007 #define PI 3.1415926535897932 #define rep(i, n) for (int i = 0; i < n; i++) #define repe(i, j, n) for (int i = j; i < n; i++) #define repi(i, n) for (int i = 0; i <= n; i++) #define repie(i, j, n) for (int i = j; i <= n; i++) #define all(x) x.begin(), x.end() #define println() cout << endl #define P pair<int, int> #define fi first #define se second typedef long long ll; long long modinv(long long a, long long m) { long long b = m, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } void solve1() { int n; cin >> n; vector<int> a(n), b(n), c(n); rep(i, n) { cin >> a[i] >> b[i] >> c[i]; } int dp[n][3]; dp[0][0] = a[0]; dp[0][1] = b[0]; dp[0][2] = c[0]; for (int i = 1; i < n + 1; i++) { dp[i][0] = max(dp[i - 1][1] + a[i], dp[i - 1][2] + a[i]); dp[i][1] = max(dp[i - 1][0] + b[i], dp[i - 1][2] + b[i]); dp[i][2] = max(dp[i - 1][0] + c[i], dp[i - 1][1] + c[i]); } int ans = max({dp[n][0], dp[n][1], dp[n][2]}); cout << ans << endl; } int main() { solve1(); }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define MOD 1000000007 #define PI 3.1415926535897932 #define rep(i, n) for (int i = 0; i < n; i++) #define repe(i, j, n) for (int i = j; i < n; i++) #define repi(i, n) for (int i = 0; i <= n; i++) #define repie(i, j, n) for (int i = j; i <= n; i++) #define all(x) x.begin(), x.end() #define println() cout << endl #define P pair<int, int> #define fi first #define se second typedef long long ll; long long modinv(long long a, long long m) { long long b = m, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } void solve1() { int n; cin >> n; vector<int> a(n), b(n), c(n); rep(i, n) { cin >> a[i] >> b[i] >> c[i]; } int dp[n + 1][3]; dp[0][0] = a[0]; dp[0][1] = b[0]; dp[0][2] = c[0]; for (int i = 1; i < n + 1; i++) { dp[i][0] = max(dp[i - 1][1] + a[i], dp[i - 1][2] + a[i]); dp[i][1] = max(dp[i - 1][0] + b[i], dp[i - 1][2] + b[i]); dp[i][2] = max(dp[i - 1][0] + c[i], dp[i - 1][1] + c[i]); } int ans = max({dp[n][0], dp[n][1], dp[n][2]}); cout << ans << endl; } int main() { solve1(); }
replace
53
54
53
54
0
p03162
C++
Runtime Error
#include <bits/stdc++.h> #define REP(i, n) for (ll i = 0; i < n; i++) #define REPN(i, n) for (ll i = 0; i < n; i++, cout << "\n") #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define mod1 1000000007 #define ll long long #define clr0(arr) memset(arr, 0, sizeof(arr)) #define clrval(arr, val) memset(arr, val, sizeof(arr)) using namespace std; ll dp[10010][3]; ll memo(ll arr[][3], ll n, ll prev) { if (n <= 0) return 0; if (prev != -1 && dp[n - 1][prev] != -1) return dp[n - 1][prev]; ll tempAns = INT_MIN; REP(i, 3) { if (i == prev) continue; ll val = arr[n - 1][i] + memo(arr, n - 1, i); tempAns = max(tempAns, val); } if (prev != -1) dp[n - 1][prev] = tempAns; return tempAns; } void solve() { ll n; cin >> n; ll arr[n][3]; REP(i, n) cin >> arr[i][0] >> arr[i][1] >> arr[i][2]; clrval(dp, -1); cout << memo(arr, n, -1) << "\n"; // cout<<rec(arr,n,-1)<<"\n"; } int main() { // int t; // cin>>t; // while(t--){ solve(); // } return 0; }
#include <bits/stdc++.h> #define REP(i, n) for (ll i = 0; i < n; i++) #define REPN(i, n) for (ll i = 0; i < n; i++, cout << "\n") #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define mod1 1000000007 #define ll long long #define clr0(arr) memset(arr, 0, sizeof(arr)) #define clrval(arr, val) memset(arr, val, sizeof(arr)) using namespace std; ll dp[100100][3]; ll memo(ll arr[][3], ll n, ll prev) { if (n <= 0) return 0; if (prev != -1 && dp[n - 1][prev] != -1) return dp[n - 1][prev]; ll tempAns = INT_MIN; REP(i, 3) { if (i == prev) continue; ll val = arr[n - 1][i] + memo(arr, n - 1, i); tempAns = max(tempAns, val); } if (prev != -1) dp[n - 1][prev] = tempAns; return tempAns; } void solve() { ll n; cin >> n; ll arr[n][3]; REP(i, n) cin >> arr[i][0] >> arr[i][1] >> arr[i][2]; clrval(dp, -1); cout << memo(arr, n, -1) << "\n"; // cout<<rec(arr,n,-1)<<"\n"; } int main() { // int t; // cin>>t; // while(t--){ solve(); // } return 0; }
replace
14
15
14
15
0
p03162
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define fi(n) for (int i = 0; i < n; i++) #define fj(n) for (int j = 0; j < n; j++) #define fr(n) for (int i = n - 1; i >= 0; i--) #define chlo(i, a, n) for (int i = a; i < n; i++) #define vi vector<int> #define vs vector<string> #define int long long int #define all(a) (a).begin(), (a).end() #define sortall(x) sort(all(x)) #define itr(a) for (auto it = a.begin(); it != a.end(); it++) #define pb push_back #define setbits(x) __builtin_popcountll(x) #define zerbits(x) __builtinctzll(x) #define mod 1000000007 #define sz(x) (int)x.size() #define inf 1e18 #define W(x) \ int x; \ cin >> x; \ while (x--) #define ps(x, y) fixed << setprecision(y) << x #define mp make_pair #define INT_SIZE 32 #define pr pair<int, int> #define deb(x) cout << #x << "=" << x << endl; #define F first #define S second #define pii pair<int, int> int mpow(int exp, int base); void aeh() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); #ifndef ONLINE_JUDGE freopen("aana.txt", "r", stdin); freopen("jj.txt", "w", stdout); #endif } int32_t main() { aeh(); int n; cin >> n; vi dp(3); for (int i = 1; i <= n; i++) { vi new_dp(3); int a[3]; fi(3) { cin >> a[i]; } for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (i != j) { new_dp[j] = max(new_dp[j], dp[i] + a[j]); } } } dp = new_dp; } cout << max({dp[0], dp[1], dp[2]}) << endl; } int mpow(int base, int exp) { base %= mod; int result = 1; while (exp > 0) { if (exp & 1) result = (result * base) % mod; base = (base * base) % mod; exp >>= 1; } return result; }
#include <bits/stdc++.h> using namespace std; #define fi(n) for (int i = 0; i < n; i++) #define fj(n) for (int j = 0; j < n; j++) #define fr(n) for (int i = n - 1; i >= 0; i--) #define chlo(i, a, n) for (int i = a; i < n; i++) #define vi vector<int> #define vs vector<string> #define int long long int #define all(a) (a).begin(), (a).end() #define sortall(x) sort(all(x)) #define itr(a) for (auto it = a.begin(); it != a.end(); it++) #define pb push_back #define setbits(x) __builtin_popcountll(x) #define zerbits(x) __builtinctzll(x) #define mod 1000000007 #define sz(x) (int)x.size() #define inf 1e18 #define W(x) \ int x; \ cin >> x; \ while (x--) #define ps(x, y) fixed << setprecision(y) << x #define mp make_pair #define INT_SIZE 32 #define pr pair<int, int> #define deb(x) cout << #x << "=" << x << endl; #define F first #define S second #define pii pair<int, int> int mpow(int exp, int base); void aeh() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); #ifndef ONLINE_JUDGE freopen("aana.txt", "r", stdin); freopen("jj.txt", "w", stdout); #endif } int32_t main() { int n; cin >> n; vi dp(3); for (int i = 1; i <= n; i++) { vi new_dp(3); int a[3]; fi(3) { cin >> a[i]; } for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (i != j) { new_dp[j] = max(new_dp[j], dp[i] + a[j]); } } } dp = new_dp; } cout << max({dp[0], dp[1], dp[2]}) << endl; } int mpow(int base, int exp) { base %= mod; int result = 1; while (exp > 0) { if (exp & 1) result = (result * base) % mod; base = (base * base) % mod; exp >>= 1; } return result; }
replace
44
45
44
45
0
p03162
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int, int>; int INF = 1001001001; 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; } int main() { int n; cin >> n; vector<vector<int>> a(n, vector<int>(3)); rep(i, n) rep(j, 3) cin >> a[i][j]; vector<vector<int>> dp(n + 1, vector<int>(3)); for (int i = 1; i <= n; i++) { for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { if (j == k) continue; chmax(dp[i][j], dp[i - 1][k] + a[i][j]); } } } int ans = 0; rep(i, 3) chmax(ans, dp[n][i]); cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int, int>; int INF = 1001001001; 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; } int main() { int n; cin >> n; vector<vector<int>> a(n, vector<int>(3)); rep(i, n) rep(j, 3) cin >> a[i][j]; vector<vector<int>> dp(n + 1, vector<int>(3)); for (int i = 1; i <= n; i++) { for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { if (j == k) continue; chmax(dp[i][j], dp[i - 1][k] + a[i - 1][j]); } } } int ans = 0; rep(i, 3) chmax(ans, dp[n][i]); cout << ans << endl; return 0; }
replace
35
36
35
36
-11
p03162
C++
Runtime Error
#include <iostream> #define ll long long using namespace std; int main() { ll int n; cin >> n; int a[n][3]; for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) cin >> a[i][j]; } ll int dp[n][n]; dp[0][0] = a[0][0]; dp[0][1] = a[0][1]; dp[0][2] = a[0][2]; for (int i = 1; i < n; i++) { dp[i][0] = max(dp[i - 1][1] + a[i][0], dp[i - 1][2] + a[i][0]); dp[i][1] = max(dp[i - 1][0] + a[i][1], dp[i - 1][2] + a[i][1]); dp[i][2] = max(dp[i - 1][0] + a[i][2], dp[i - 1][1] + a[i][2]); } cout << max(dp[n - 1][0], max(dp[n - 1][1], dp[n - 1][2])); return 0; }
#include <iostream> #define ll long long using namespace std; int main() { ll int n; cin >> n; int a[n][3]; for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) cin >> a[i][j]; } ll int dp[n][3]; dp[0][0] = a[0][0]; dp[0][1] = a[0][1]; dp[0][2] = a[0][2]; for (int i = 1; i < n; i++) { dp[i][0] = max(dp[i - 1][1] + a[i][0], dp[i - 1][2] + a[i][0]); dp[i][1] = max(dp[i - 1][0] + a[i][1], dp[i - 1][2] + a[i][1]); dp[i][2] = max(dp[i - 1][0] + a[i][2], dp[i - 1][1] + a[i][2]); } cout << max(dp[n - 1][0], max(dp[n - 1][1], dp[n - 1][2])); return 0; }
replace
12
13
12
13
0
p03162
C++
Runtime Error
#include <cmath> #include <iostream> using namespace std; int abc[10001][3]; int dp[10001][3]; int n; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> abc[i][0] >> abc[i][1] >> abc[i][2]; } // dp[i][j]:=前日にjを選ぶ時にi日目までに得られる幸福度の最大値 // i日目に... for (int i = 0; i <= n; i++) { // 前日にjを選んでいたとき... for (int j = 0; j < 3; j++) { // kを選ぶ for (int k = 0; k < 3; k++) { if (k == j) continue; dp[i + 1][k] = max(dp[i][j] + abc[i + 1][k], dp[i + 1][k]); } } } cout << max(max(dp[n][0], dp[n][1]), dp[n][2]) << endl; return 0; }
#include <cmath> #include <iostream> using namespace std; int abc[100001][3]; int dp[100001][3]; int n; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> abc[i][0] >> abc[i][1] >> abc[i][2]; } // dp[i][j]:=前日にjを選ぶ時にi日目までに得られる幸福度の最大値 // i日目に... for (int i = 0; i <= n; i++) { // 前日にjを選んでいたとき... for (int j = 0; j < 3; j++) { // kを選ぶ for (int k = 0; k < 3; k++) { if (k == j) continue; dp[i + 1][k] = max(dp[i][j] + abc[i + 1][k], dp[i + 1][k]); } } } cout << max(max(dp[n][0], dp[n][1]), dp[n][2]) << endl; return 0; }
replace
4
6
4
6
0
p03162
C++
Runtime Error
#include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <map> #include <queue> #include <set> #include <stdio.h> #include <string> #include <unordered_map> #include <vector> #define ll long long #define pii pair<int, int> #define pll pair<long long, long long> #define plxy pair<long long, pll> #define INT_MAX 1e18 #define Keep_calm_You_are_still_learning \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); using namespace std; using namespace std; const int mod = 1e9 + 7; const int N = 1e3 + 5; ll ans[N][3]; int main() { ll n, a[N], b[N], c[N]; cin >> n; for (ll i = 0; i < n; i++) { cin >> a[i] >> b[i] >> c[i]; } ans[0][0] = a[0]; ans[0][1] = b[0]; ans[0][2] = c[0]; for (ll i = 1; i < n; i++) { ans[i][0] = a[i] + max(ans[i - 1][1], ans[i - 1][2]); ans[i][1] = b[i] + max(ans[i - 1][0], ans[i - 1][2]); ans[i][2] = c[i] + max(ans[i - 1][0], ans[i - 1][1]); } cout << max(ans[n - 1][0], max(ans[n - 1][1], ans[n - 1][2])); return 0; }
#include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <map> #include <queue> #include <set> #include <stdio.h> #include <string> #include <unordered_map> #include <vector> #define ll long long #define pii pair<int, int> #define pll pair<long long, long long> #define plxy pair<long long, pll> #define INT_MAX 1e18 #define Keep_calm_You_are_still_learning \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); using namespace std; using namespace std; const int mod = 1e9 + 7; const int N = 1e5 + 5; ll ans[N][3]; int main() { ll n, a[N], b[N], c[N]; cin >> n; for (ll i = 0; i < n; i++) { cin >> a[i] >> b[i] >> c[i]; } ans[0][0] = a[0]; ans[0][1] = b[0]; ans[0][2] = c[0]; for (ll i = 1; i < n; i++) { ans[i][0] = a[i] + max(ans[i - 1][1], ans[i - 1][2]); ans[i][1] = b[i] + max(ans[i - 1][0], ans[i - 1][2]); ans[i][2] = c[i] + max(ans[i - 1][0], ans[i - 1][1]); } cout << max(ans[n - 1][0], max(ans[n - 1][1], ans[n - 1][2])); return 0; }
replace
21
22
21
22
0
p03162
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define int long long #define N 100011 using namespace std; const int INF = -(int)1e17; int n, arr[N][3], opt[N][3]; signed main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; for (int i = 0; i < n; i++) { cin >> arr[i][0] >> arr[i][1] >> arr[i][2]; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int res = INF; if (i == 0) res = arr[i][j]; else { if (j == 0) res = max(opt[i - 1][1], opt[i - 1][2]); else if (j == 1) res = max(opt[i - 1][0], opt[i - 1][2]); else res = max(opt[i - 1][0], opt[i - 1][1]); res += arr[i][j]; } opt[i][j] = res; } } int ans = INF; for (int i = 0; i < 3; i++) ans = max(ans, opt[n - 1][i]); cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define int long long #define N 100011 using namespace std; const int INF = -(int)1e17; int n, arr[N][3], opt[N][3]; signed main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; for (int i = 0; i < n; i++) { cin >> arr[i][0] >> arr[i][1] >> arr[i][2]; } for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) { int res = INF; if (i == 0) res = arr[i][j]; else { if (j == 0) res = max(opt[i - 1][1], opt[i - 1][2]); else if (j == 1) res = max(opt[i - 1][0], opt[i - 1][2]); else res = max(opt[i - 1][0], opt[i - 1][1]); res += arr[i][j]; } opt[i][j] = res; } } int ans = INF; for (int i = 0; i < 3; i++) ans = max(ans, opt[n - 1][i]); cout << ans << endl; return 0; }
replace
18
19
18
19
TLE
p03162
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define MAX 20000000 #define FOR(i, m, n) for (int i = (m); i < (n); i++) #define REP(i, n) FOR(i, 0, n) #define pb push_back #define mp make_pair #define fst first #define snd second #define all(v) v.begin(), v.end() typedef long long ll; typedef vector<int> vi; typedef pair<int, int> ii; int main() { int n, x, m[3][MAX]; ll dp[3][MAX]; cin >> n; REP(i, 3 * n) { cin >> x; m[i % 3][i / 3] = x; } dp[0][0] = m[0][0]; dp[1][0] = m[1][0]; dp[2][0] = m[2][0]; REP(i, n - 1) { dp[0][i + 1] = m[0][i + 1] + max(dp[1][i], dp[2][i]); dp[1][i + 1] = m[1][i + 1] + max(dp[2][i], dp[0][i]); dp[2][i + 1] = m[2][i + 1] + max(dp[0][i], dp[1][i]); } cout << max(dp[0][n - 1], max(dp[1][n - 1], dp[2][n - 1])) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define MAX 2000000 #define FOR(i, m, n) for (int i = (m); i < (n); i++) #define REP(i, n) FOR(i, 0, n) #define pb push_back #define mp make_pair #define fst first #define snd second #define all(v) v.begin(), v.end() typedef long long ll; typedef vector<int> vi; typedef pair<int, int> ii; int main() { int n, x, m[3][MAX]; ll dp[3][MAX]; cin >> n; REP(i, 3 * n) { cin >> x; m[i % 3][i / 3] = x; } dp[0][0] = m[0][0]; dp[1][0] = m[1][0]; dp[2][0] = m[2][0]; REP(i, n - 1) { dp[0][i + 1] = m[0][i + 1] + max(dp[1][i], dp[2][i]); dp[1][i + 1] = m[1][i + 1] + max(dp[2][i], dp[0][i]); dp[2][i + 1] = m[2][i + 1] + max(dp[0][i], dp[1][i]); } cout << max(dp[0][n - 1], max(dp[1][n - 1], dp[2][n - 1])) << endl; return 0; }
replace
3
4
3
4
-11
p03162
C++
Runtime Error
#include <bits/stdc++.h> #define INF INT_MAX #define MOD 1000000007 #define REP(i, n) for (int i = 0; i < n; i++) #define all(x) (x).begin(), (x).end() typedef long long ll; typedef std::pair<int, int> pii; using namespace std; int main(void) { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; int abc[10000][3]; int dp[10000][3]; REP(i, n) REP(j, 3) cin >> abc[i][j]; REP(i, 3) dp[0][i] = abc[0][i]; for (int i = 1; i < n; i++) { REP(j, 3) { int max_v = 0; REP(k, 3) { if (j == k) continue; max_v = max(dp[i - 1][k] + abc[i][j], max_v); } dp[i][j] = max_v; } } int ans = 0; REP(i, 3) if (ans < dp[n - 1][i]) ans = dp[n - 1][i]; cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define INF INT_MAX #define MOD 1000000007 #define REP(i, n) for (int i = 0; i < n; i++) #define all(x) (x).begin(), (x).end() typedef long long ll; typedef std::pair<int, int> pii; using namespace std; int main(void) { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; int abc[100000][3]; int dp[100000][3]; REP(i, n) REP(j, 3) cin >> abc[i][j]; REP(i, 3) dp[0][i] = abc[0][i]; for (int i = 1; i < n; i++) { REP(j, 3) { int max_v = 0; REP(k, 3) { if (j == k) continue; max_v = max(dp[i - 1][k] + abc[i][j], max_v); } dp[i][j] = max_v; } } int ans = 0; REP(i, 3) if (ans < dp[n - 1][i]) ans = dp[n - 1][i]; cout << ans << endl; return 0; }
replace
18
20
18
20
0
p03162
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define FAST_IO \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); typedef long long ll; /** * Uncomment the #define below if problem contains multiple test cases */ // #define MULTIPLE_TESTS 1 vector<vector<int>> cache; int recursive(int cur, int prev, const vector<vector<int>> &cost) { if (cur == cost.size()) { // all done return 0; } if (prev != -1 && cache[cur][prev] != -1) { return cache[cur][prev]; } int points = 0; // consider only ones with different prev for (int j = 0; j < 3; ++j) { if (j != prev) { points = max(points, recursive(cur + 1, j, cost) + cost[cur][j]); // prev = j } } return cache[cur][prev] = points; } void solve() { // write your code here int n; cin >> n; vector<vector<int>> cost(n, vector<int>(3)); // n*3 cost matrix for (int i = 0; i < n; ++i) { for (int j = 0; j < 3; ++j) { cin >> cost[i][j]; } } cache.assign(n, vector<int>(3, -1)); int ans = recursive(0, -1, cost); cout << ans << endl; } int main() { FAST_IO; int t = 1; #ifdef MULTIPLE_TESTS cin >> t; #endif while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; #define FAST_IO \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); typedef long long ll; /** * Uncomment the #define below if problem contains multiple test cases */ // #define MULTIPLE_TESTS 1 vector<vector<int>> cache; int recursive(int cur, int prev, const vector<vector<int>> &cost) { if (cur == cost.size()) { // all done return 0; } if (prev != -1 && cache[cur][prev] != -1) { return cache[cur][prev]; } int points = 0; // consider only ones with different prev for (int j = 0; j < 3; ++j) { if (j != prev) { points = max(points, recursive(cur + 1, j, cost) + cost[cur][j]); // prev = j } } if (prev == -1) { return points; } return cache[cur][prev] = points; } void solve() { // write your code here int n; cin >> n; vector<vector<int>> cost(n, vector<int>(3)); // n*3 cost matrix for (int i = 0; i < n; ++i) { for (int j = 0; j < 3; ++j) { cin >> cost[i][j]; } } cache.assign(n, vector<int>(3, -1)); int ans = recursive(0, -1, cost); cout << ans << endl; } int main() { FAST_IO; int t = 1; #ifdef MULTIPLE_TESTS cin >> t; #endif while (t--) { solve(); } return 0; }
insert
27
27
27
30
-6
double free or corruption (out)
p03162
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("-ffloat-store") // to restrict undesirable precision #pragma GCC optimize( \ "-fno-defer-pop") // to pop argument of function as soon as it returns #define all(a) a.begin(), a.end() #define ll long long int #define ld long double ll power(ll a, ll b, ll m) { if (b == 0) return 1; if (b == 1) return a % m; ll t = power(a, b / 2, m) % m; t = (t * t) % m; if (b & 1) t = ((t % m) * (a % m)) % m; return t; } ll modInverse(ll a, ll m) { return power(a, m - 2, m); } #define ps push_back #define fs first #define sc second #define takeline cin.ignore(); #define iactive cout.flush(); #define N 100005 #define endl "\n" #define mod 1000000007 //((1.0l)*BIG MULTIPLY MAGIC?) // string to integer stoi() // string to long long stoll() // string.substr(position,length); // integer to string to_string(); //----------------------------------------------- ll n, ar[N][4], dp[N][4], k; ll maxval(ll i, ll flg) { if (i > n) return 0; if (dp[i][flg] != -1) { return dp[i][flg]; } // cout<<i<<" "<<flg<<endl; ll j, an = 0; for (j = 1; j <= 3; j++) { if (j == flg) continue; an = max(an, maxval(i + 1, j) + ar[i][j]); } return an; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll i, j, l; cin >> n; for (i = 1; i <= n; i++) { cin >> ar[i][1] >> ar[i][2] >> ar[i][3]; } memset(dp, -1, sizeof(dp)); ll an = maxval(1, 0); cout << an; return 0; }
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("-ffloat-store") // to restrict undesirable precision #pragma GCC optimize( \ "-fno-defer-pop") // to pop argument of function as soon as it returns #define all(a) a.begin(), a.end() #define ll long long int #define ld long double ll power(ll a, ll b, ll m) { if (b == 0) return 1; if (b == 1) return a % m; ll t = power(a, b / 2, m) % m; t = (t * t) % m; if (b & 1) t = ((t % m) * (a % m)) % m; return t; } ll modInverse(ll a, ll m) { return power(a, m - 2, m); } #define ps push_back #define fs first #define sc second #define takeline cin.ignore(); #define iactive cout.flush(); #define N 100005 #define endl "\n" #define mod 1000000007 //((1.0l)*BIG MULTIPLY MAGIC?) // string to integer stoi() // string to long long stoll() // string.substr(position,length); // integer to string to_string(); //----------------------------------------------- ll n, ar[N][4], dp[N][4], k; ll maxval(ll i, ll flg) { if (i > n) return 0; if (dp[i][flg] != -1) { return dp[i][flg]; } // cout<<i<<" "<<flg<<endl; ll j, an = 0; for (j = 1; j <= 3; j++) { if (j == flg) continue; an = max(an, maxval(i + 1, j) + ar[i][j]); } return dp[i][flg] = an; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll i, j, l; cin >> n; for (i = 1; i <= n; i++) { cin >> ar[i][1] >> ar[i][2] >> ar[i][3]; } memset(dp, -1, sizeof(dp)); ll an = maxval(1, 0); cout << an; return 0; }
replace
51
52
51
52
TLE
p03162
C++
Runtime Error
#include <bits/stdc++.h> #include <iostream> #define pb push_back #define mp make_pair #define ll long long #define N 2 * 10e5 #define precise cout << fixed << setprecision(10) #pragma GCC optimize("O0") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("-ffloat-store") #define rep(i, a, b) for (int i = a; i < b; i++) #define min3(a, b, c) min(a, min(b, c)) using namespace std; void get(ll arr[], ll n) { for (ll i = 0; i < n; i++) cin >> arr[i]; } #define max4(a, b, c, d) max(a, max(b, max(c, d))) int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll t; t = 1; // cin>>t; while (t--) { ll n; cin >> n; ll a[n + 1][3]; ll dp[n + 1][n + 1]; rep(i, 1, n + 1) { rep(j, 0, 3) cin >> a[i][j]; } dp[1][0] = a[1][0]; dp[1][1] = a[1][1]; dp[1][2] = a[1][2]; for (ll i = 2; i <= n; i++) { for (ll j = 0; j < 3; j++) { if (j == 0) dp[i][j] = max(dp[i - 1][1], dp[i - 1][2]) + a[i][j]; else if (j == 2) dp[i][j] = max(dp[i - 1][0], dp[i - 1][1]) + a[i][j]; else if (j == 1) dp[i][j] = max(dp[i - 1][2], dp[i - 1][0]) + a[i][j]; } } ll ans = 0; ans = max(ans, dp[n][0]); ans = max(ans, dp[n][1]); ans = max(ans, dp[n][2]); cout << ans; } // cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << // "ms\n"; }
#include <bits/stdc++.h> #include <iostream> #define pb push_back #define mp make_pair #define ll long long #define N 2 * 10e5 #define precise cout << fixed << setprecision(10) #pragma GCC optimize("O0") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("-ffloat-store") #define rep(i, a, b) for (int i = a; i < b; i++) #define min3(a, b, c) min(a, min(b, c)) using namespace std; void get(ll arr[], ll n) { for (ll i = 0; i < n; i++) cin >> arr[i]; } #define max4(a, b, c, d) max(a, max(b, max(c, d))) int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll t; t = 1; // cin>>t; while (t--) { ll n; cin >> n; ll a[n + 1][3]; ll dp[n + 1][3]; rep(i, 1, n + 1) { rep(j, 0, 3) cin >> a[i][j]; } dp[1][0] = a[1][0]; dp[1][1] = a[1][1]; dp[1][2] = a[1][2]; for (ll i = 2; i <= n; i++) { for (ll j = 0; j < 3; j++) { if (j == 0) dp[i][j] = max(dp[i - 1][1], dp[i - 1][2]) + a[i][j]; else if (j == 2) dp[i][j] = max(dp[i - 1][0], dp[i - 1][1]) + a[i][j]; else if (j == 1) dp[i][j] = max(dp[i - 1][2], dp[i - 1][0]) + a[i][j]; } } ll ans = 0; ans = max(ans, dp[n][0]); ans = max(ans, dp[n][1]); ans = max(ans, dp[n][2]); cout << ans; } // cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << // "ms\n"; }
replace
30
31
30
31
0
p03162
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define ll long long ll ne(ll ar[], ll br[], ll cr[], ll n, int x, ll p) { if (p < n - 1) { if (x == 1) return max(br[p + 1] + ne(ar, br, cr, n, 2, p + 1), cr[p + 1] + ne(ar, br, cr, n, 3, p + 1)); else if (x == 2) return max(ar[p + 1] + ne(ar, br, cr, n, 1, p + 1), cr[p + 1] + ne(ar, br, cr, n, 3, p + 1)); else if (x == 3) return max(ar[p + 1] + ne(ar, br, cr, n, 1, p + 1), br[p + 1] + ne(ar, br, cr, n, 2, p + 1)); } else if (p == n - 1) { return 0; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll n; cin >> n; ll ar[n], br[n], cr[n]; for (ll i = 0; i < n; i++) { cin >> ar[i] >> br[i] >> cr[i]; } cout << max( max(ar[0] + ne(ar, br, cr, n, 1, 0), br[0] + ne(ar, br, cr, n, 2, 0)), cr[0] + ne(ar, br, cr, n, 3, 0)); return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long ll ne(ll ar[], ll br[], ll cr[], ll n, int x, ll p) { if (p < n - 1) { if (x == 1) return max(br[p + 1] + ne(ar, br, cr, n, 2, p + 1), cr[p + 1] + ne(ar, br, cr, n, 3, p + 1)); else if (x == 2) return max(ar[p + 1] + ne(ar, br, cr, n, 1, p + 1), cr[p + 1] + ne(ar, br, cr, n, 3, p + 1)); else if (x == 3) return max(ar[p + 1] + ne(ar, br, cr, n, 1, p + 1), br[p + 1] + ne(ar, br, cr, n, 2, p + 1)); } else if (p == n - 1) { return 0; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll n; cin >> n; ll ar[n], br[n], cr[n]; for (ll i = 0; i < n; i++) { cin >> ar[i] >> br[i] >> cr[i]; } ll dr[n][3]; dr[n - 1][0] = ar[n - 1]; dr[n - 1][1] = br[n - 1]; dr[n - 1][2] = cr[n - 1]; for (ll i = n - 2; i >= 0; i--) { dr[i][0] = ar[i] + max(dr[i + 1][1], dr[i + 1][2]); dr[i][1] = br[i] + max(dr[i + 1][0], dr[i + 1][2]); dr[i][2] = cr[i] + max(dr[i + 1][1], dr[i + 1][0]); } cout << max(max(dr[0][0], dr[0][1]), dr[0][2]); return 0; }
replace
28
31
28
38
TLE
p03162
C++
Runtime Error
#include <bits/stdc++.h> #include <iostream> #include <stdio.h> #define lli long long int using namespace std; lli getmax(vector<vector<int>> &h, int i, int prev, int n, vector<vector<int>> &cache) { if (i >= n) { return 0; } if (i != 0 && cache[i][prev] != -1) { return cache[i][prev]; } lli maxi = 0; for (int j = 0; j < 3; j++) { if (j != prev) { maxi = max(maxi, h[i][j] + getmax(h, i + 1, j, n, cache)); } } cache[i][prev] = maxi; return cache[i][prev]; } void solve() { int n; cin >> n; vector<vector<int>> h(n, vector<int>(3, 0)); vector<vector<int>> cache(n + 1, vector<int>(3, -1)); for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) { cin >> h[i][j]; } } lli res = getmax(h, 0, -1, n, cache); cout << res << endl; } int main() { solve(); }
#include <bits/stdc++.h> #include <iostream> #include <stdio.h> #define lli long long int using namespace std; lli getmax(vector<vector<int>> &h, int i, int prev, int n, vector<vector<int>> &cache) { if (i >= n) { return 0; } if (i != 0 && cache[i][prev] != -1) { return cache[i][prev]; } lli maxi = 0; for (int j = 0; j < 3; j++) { if (j != prev) { maxi = max(maxi, h[i][j] + getmax(h, i + 1, j, n, cache)); } } if (i != 0) { cache[i][prev] = maxi; return cache[i][prev]; } return maxi; } void solve() { int n; cin >> n; vector<vector<int>> h(n, vector<int>(3, 0)); vector<vector<int>> cache(n + 1, vector<int>(3, -1)); for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) { cin >> h[i][j]; } } lli res = getmax(h, 0, -1, n, cache); cout << res << endl; } int main() { solve(); }
replace
23
25
23
29
-6
double free or corruption (out)
p03162
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define PB push_back #define PPB pop_back #define MP make_pair #define IOS \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define test(x) cout << (x) << " " #define d1(x) cout << (x) << endl #define d2(x, y) cout << (x) << " " << (y) << endl #define d3(x, y, z) cout << (x) << " " << (y) << " " << (z) << endl #define loop(i, start, end) for (ll i = start; i < end; i++) #define rloop(j, start, end) for (ll j = start - 1; j >= end; j--) int main() { IOS; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ll n; cin >> n; ll a[n][3], dp[n + 1][3], maxi = INT_MIN, cnt = 0; loop(i, 0, n) cin >> a[i][0] >> a[i][1] >> a[i][2]; memset(dp, 0, sizeof(dp)); dp[0][0] = a[0][0]; dp[0][1] = a[0][1]; dp[0][2] = a[0][2]; loop(i, 1, n) { dp[i][0] = max(dp[i - 1][1] + a[i][0], dp[i - 1][2] + a[i][0]); dp[i][1] = max(dp[i - 1][0] + a[i][1], dp[i - 1][2] + a[i][1]); dp[i][2] = max(dp[i - 1][0] + a[i][2], dp[i - 1][1] + a[i][2]); } d1(max(dp[n - 1][0], max(dp[n - 1][1], dp[n - 1][2]))); }
#include <bits/stdc++.h> using namespace std; #define ll long long #define PB push_back #define PPB pop_back #define MP make_pair #define IOS \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define test(x) cout << (x) << " " #define d1(x) cout << (x) << endl #define d2(x, y) cout << (x) << " " << (y) << endl #define d3(x, y, z) cout << (x) << " " << (y) << " " << (z) << endl #define loop(i, start, end) for (ll i = start; i < end; i++) #define rloop(j, start, end) for (ll j = start - 1; j >= end; j--) int main() { IOS; ll n; cin >> n; ll a[n][3], dp[n + 1][3], maxi = INT_MIN, cnt = 0; loop(i, 0, n) cin >> a[i][0] >> a[i][1] >> a[i][2]; memset(dp, 0, sizeof(dp)); dp[0][0] = a[0][0]; dp[0][1] = a[0][1]; dp[0][2] = a[0][2]; loop(i, 1, n) { dp[i][0] = max(dp[i - 1][1] + a[i][0], dp[i - 1][2] + a[i][0]); dp[i][1] = max(dp[i - 1][0] + a[i][1], dp[i - 1][2] + a[i][1]); dp[i][2] = max(dp[i - 1][0] + a[i][2], dp[i - 1][1] + a[i][2]); } d1(max(dp[n - 1][0], max(dp[n - 1][1], dp[n - 1][2]))); }
delete
19
23
19
19
-11
p03162
C++
Runtime Error
// * // * // * Created By :hozaifa wahid // * "mai maut ko takia aur kafan ko chadar banakar sota hu" // *------------------------------------ // * OS : Ubuntu 16.04 // * Language : CPP14 // * Editor : Sublime Text 3 // * C++ compiler : g++ // * // * // https://www.geeksforgeeks.org/find-the-point-where-maximum-intervals-overlap/ #include <bits/stdc++.h> #define pb push_back // #define mp make_pair #define inf 1000000007 #define fr first #define sc second #define eps 1e-9 #define clr(a) memset(a, 0, sizeof(a)) #define sz(x) x.size() #define sni(x) scanf("%d", &x) #define snl(x) scanf("%lld", &x) #define snc(x) scanf("%c", &c); #define rep(n) for (int i = 0; i < n; i++) #define repc(i, n) for (int i = 0; i < n; i++) #define FOR(i, x, y) for (int i = x; i < y; i++) #define DEC(i, x, y) for (int i = x; i >= y; i--) #define all(v) v.begin(), v.end() #define min3(a, b, c) min(a, min(b, c)) #define max3(a, b, c) max(a, max(b, c)) #define alla(a, n) a, a + n using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll> ii; typedef vector<ll> vi; typedef vector<ii> vii; ll MOD = 1000000007; vector<ll> v[100055]; void sieve() { for (int n = 1; n <= 100055; n++) { for (int i = 1; i <= sqrt(n); i++) { if (n % i == 0) { // If divisors are equal, print only one if (n / i == i) v[n].push_back(i); else // Otherwise print both { v[n].push_back(i); v[n].push_back(n / i); } } } } } ll pw(ll a, ll b) { if (b == 0) return 1; ll x = pw(a, b / 2); x = (x * x) % MOD; if (b % 2) x = (x * a) % MOD; return x; } ll n, s; ll a[100005][3]; int ok = 0; // ll res=0; ll dp[100005]; ll mpi[3][100005]; ll go(ll i, ll prev) { if (i == n - 1) { // s=0; return a[i][prev]; } if (mpi[i][prev]) return mpi[i][prev]; else if (prev == 0) { s = a[i][prev] + (max(go(i + 1, 2), go(i + 1, 1))); mpi[i][prev] = s; } else if (prev == 1) { s = a[i][prev] + (max(go(i + 1, 2), go(i + 1, 0))); mpi[i][prev] = s; } else if (prev == 2) { s = a[i][prev] + (max(go(i + 1, 0), go(i + 1, 1))); mpi[i][prev] = s; } return s; } void solve() { ll t; t = 1; while (t--) { // ll n; cin >> n; // ll a[n]; FOR(i, 0, n) FOR(j, 0, 3) cin >> a[i][j], mpi[i][j] = 0; ll res = max(go(0, 0), max(go(0, 1), go(0, 2))); cout << res; } } int main() { std::ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int start = clock(); #ifdef ONLINE_JUDGE #else // freopen("input.in" , "r" , stdin); // freopen("output.txt", "w", stdout); #endif solve(); int stop = clock(); #ifdef ONLINE_JUDGE #else // cout << setprecision(6) << fixed << (stop - start) * 1000.00 / // double(CLOCKS_PER_SEC)<< endl; #endif }
// * // * // * Created By :hozaifa wahid // * "mai maut ko takia aur kafan ko chadar banakar sota hu" // *------------------------------------ // * OS : Ubuntu 16.04 // * Language : CPP14 // * Editor : Sublime Text 3 // * C++ compiler : g++ // * // * // https://www.geeksforgeeks.org/find-the-point-where-maximum-intervals-overlap/ #include <bits/stdc++.h> #define pb push_back // #define mp make_pair #define inf 1000000007 #define fr first #define sc second #define eps 1e-9 #define clr(a) memset(a, 0, sizeof(a)) #define sz(x) x.size() #define sni(x) scanf("%d", &x) #define snl(x) scanf("%lld", &x) #define snc(x) scanf("%c", &c); #define rep(n) for (int i = 0; i < n; i++) #define repc(i, n) for (int i = 0; i < n; i++) #define FOR(i, x, y) for (int i = x; i < y; i++) #define DEC(i, x, y) for (int i = x; i >= y; i--) #define all(v) v.begin(), v.end() #define min3(a, b, c) min(a, min(b, c)) #define max3(a, b, c) max(a, max(b, c)) #define alla(a, n) a, a + n using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll> ii; typedef vector<ll> vi; typedef vector<ii> vii; ll MOD = 1000000007; vector<ll> v[100055]; void sieve() { for (int n = 1; n <= 100055; n++) { for (int i = 1; i <= sqrt(n); i++) { if (n % i == 0) { // If divisors are equal, print only one if (n / i == i) v[n].push_back(i); else // Otherwise print both { v[n].push_back(i); v[n].push_back(n / i); } } } } } ll pw(ll a, ll b) { if (b == 0) return 1; ll x = pw(a, b / 2); x = (x * x) % MOD; if (b % 2) x = (x * a) % MOD; return x; } ll n, s; ll a[100005][3]; int ok = 0; // ll res=0; ll dp[100005]; ll mpi[100005][3]; ll go(ll i, ll prev) { if (i == n - 1) { // s=0; return a[i][prev]; } if (mpi[i][prev]) return mpi[i][prev]; else if (prev == 0) { s = a[i][prev] + (max(go(i + 1, 2), go(i + 1, 1))); mpi[i][prev] = s; } else if (prev == 1) { s = a[i][prev] + (max(go(i + 1, 2), go(i + 1, 0))); mpi[i][prev] = s; } else if (prev == 2) { s = a[i][prev] + (max(go(i + 1, 0), go(i + 1, 1))); mpi[i][prev] = s; } return s; } void solve() { ll t; t = 1; while (t--) { // ll n; cin >> n; // ll a[n]; FOR(i, 0, n) FOR(j, 0, 3) cin >> a[i][j], mpi[i][j] = 0; ll res = max(go(0, 0), max(go(0, 1), go(0, 2))); cout << res; } } int main() { std::ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int start = clock(); #ifdef ONLINE_JUDGE #else // freopen("input.in" , "r" , stdin); // freopen("output.txt", "w", stdout); #endif solve(); int stop = clock(); #ifdef ONLINE_JUDGE #else // cout << setprecision(6) << fixed << (stop - start) * 1000.00 / // double(CLOCKS_PER_SEC)<< endl; #endif }
replace
72
73
72
73
0
p03162
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define eb emplace_back #define pb push_back #define pii pair<int64_t, int64_t> #define mp make_pair #define whole(v) begin(v), end(v) #define print64_t(c, n) \ for (int64_t i = 0; i < n; i++) \ cout << c[i] << " "; #define MOD 1000000007 #define INF 0x3f3f3f3f int64_t dp[100005][3]; vector<int64_t> a, b, c; int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif std::ios::sync_with_stdio(false); cin.tie(0); int64_t n; cin >> n; a.reserve(n), b.reserve(n), c.reserve(n); for (int64_t i = 0; i < n; i++) { int64_t x, y, z; cin >> x >> y >> z; a.eb(x), b.eb(y), c.eb(z); } dp[0][0] = a[0]; dp[0][1] = b[0]; dp[0][2] = c[0]; for (int64_t i = 1; i < n; i++) { dp[i][0] = max(dp[i - 1][1], dp[i - 1][2]) + a[i]; dp[i][1] = max(dp[i - 1][2], dp[i - 1][0]) + b[i]; dp[i][2] = max(dp[i - 1][1], dp[i - 1][0]) + c[i]; } cout << max(dp[n - 1][0], max(dp[n - 1][1], dp[n - 1][2])); return 0; }
#include <bits/stdc++.h> using namespace std; #define eb emplace_back #define pb push_back #define pii pair<int64_t, int64_t> #define mp make_pair #define whole(v) begin(v), end(v) #define print64_t(c, n) \ for (int64_t i = 0; i < n; i++) \ cout << c[i] << " "; #define MOD 1000000007 #define INF 0x3f3f3f3f int64_t dp[100005][3]; vector<int64_t> a, b, c; int main() { std::ios::sync_with_stdio(false); cin.tie(0); int64_t n; cin >> n; a.reserve(n), b.reserve(n), c.reserve(n); for (int64_t i = 0; i < n; i++) { int64_t x, y, z; cin >> x >> y >> z; a.eb(x), b.eb(y), c.eb(z); } dp[0][0] = a[0]; dp[0][1] = b[0]; dp[0][2] = c[0]; for (int64_t i = 1; i < n; i++) { dp[i][0] = max(dp[i - 1][1], dp[i - 1][2]) + a[i]; dp[i][1] = max(dp[i - 1][2], dp[i - 1][0]) + b[i]; dp[i][2] = max(dp[i - 1][1], dp[i - 1][0]) + c[i]; } cout << max(dp[n - 1][0], max(dp[n - 1][1], dp[n - 1][2])); return 0; }
replace
15
19
15
16
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p03162
C++
Runtime Error
// Author Name - Mohammad Shehar Yaar Tausif #include <bits/stdc++.h> using namespace std; typedef long long int lld; int main() { long arr[1000][3], n, i, j, DP[1000][3]; cin >> n; for (i = 0; i < n; i++) { cin >> arr[i][0] >> arr[i][1] >> arr[i][2]; } DP[0][0] = arr[0][0]; DP[0][1] = arr[0][1]; DP[0][2] = arr[0][2]; for (i = 1; i < n; i++) { DP[i][0] = max(arr[i][0] + DP[i - 1][1], arr[i][0] + DP[i - 1][2]); DP[i][1] = max(arr[i][1] + DP[i - 1][0], arr[i][1] + DP[i - 1][2]); DP[i][2] = max(arr[i][2] + DP[i - 1][1], arr[i][2] + DP[i - 1][0]); } cout << max(DP[n - 1][0], max(DP[n - 1][1], DP[n - 1][2])) << endl; return 0; }
// Author Name - Mohammad Shehar Yaar Tausif #include <bits/stdc++.h> using namespace std; typedef long long int lld; int main() { long arr[100000][3], n, i, j, DP[100000][3]; cin >> n; for (i = 0; i < n; i++) { cin >> arr[i][0] >> arr[i][1] >> arr[i][2]; } DP[0][0] = arr[0][0]; DP[0][1] = arr[0][1]; DP[0][2] = arr[0][2]; for (i = 1; i < n; i++) { DP[i][0] = max(arr[i][0] + DP[i - 1][1], arr[i][0] + DP[i - 1][2]); DP[i][1] = max(arr[i][1] + DP[i - 1][0], arr[i][1] + DP[i - 1][2]); DP[i][2] = max(arr[i][2] + DP[i - 1][1], arr[i][2] + DP[i - 1][0]); } cout << max(DP[n - 1][0], max(DP[n - 1][1], DP[n - 1][2])) << endl; return 0; }
replace
8
9
8
9
0
p03162
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define vi vector<int> #define vii vector<vi> #define pi pair<int, int> #define vpi vector<pi> #define S second #define F first #define pb push_back #define inf 1000000 #define Test \ int t, i = 0; \ cin >> t; \ while (i < t) #define MAXN 100005 #define M 10005 signed main() { int a[M], b[M], c[M]; Test { cin >> a[i] >> b[i] >> c[i]; i += 1; } for (i = 1; i < t; i++) { a[i] = max(a[i] + b[i - 1], a[i] + c[i - 1]); b[i] = max(a[i - 1] + b[i], c[i - 1] + b[i]); c[i] = max(a[i - 1] + c[i], b[i - 1] + c[i]); } cout << max({a[t - 1], b[t - 1], c[t - 1]}) << "\n"; }
#include <bits/stdc++.h> using namespace std; #define vi vector<int> #define vii vector<vi> #define pi pair<int, int> #define vpi vector<pi> #define S second #define F first #define pb push_back #define inf 1000000 #define Test \ int t, i = 0; \ cin >> t; \ while (i < t) #define MAXN 100005 #define M 100005 signed main() { int a[M], b[M], c[M]; Test { cin >> a[i] >> b[i] >> c[i]; i += 1; } for (i = 1; i < t; i++) { a[i] = max(a[i] + b[i - 1], a[i] + c[i - 1]); b[i] = max(a[i - 1] + b[i], c[i - 1] + b[i]); c[i] = max(a[i - 1] + c[i], b[i - 1] + c[i]); } cout << max({a[t - 1], b[t - 1], c[t - 1]}) << "\n"; }
replace
16
17
16
17
0
p03162
C++
Runtime Error
#include <algorithm> #include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define rep(i, a, b) for (ll i = a; i < b; i++) #define repb(i, a, b) for (ll i = a; i >= b; i--) #define err() cout << "==================================" << endl; #define errA(A) \ for (auto i : A) \ cout << i << " "; \ cout << endl; #define err1(a) cout << #a << " " << a << endl #define err2(a, b) cout << #a << " " << a << " " << #b << " " << b << endl #define err3(a, b, c) \ cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c \ << endl #define err4(a, b, c, d) \ cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c \ << " " << #d << " " << d << endl #define pb push_back #define all(A) A.begin(), A.end() #define allr(A) A.rbegin(), A.rend() #define ft first #define sd second #define pll pair<ll, ll> #define V vector<ll> #define S set<ll> #define VV vector<V> #define Vpll vector<pll> #define endl "\n" const ll logN = 20; const ll M = 1000000007; const ll INF = 1e12; #define PI 3.14159265 int main() { int n; cin >> n; vector<int> dp(3); rep(i, 0, n) { vector<int> new_dp(3, 0); vector<int> c(3); rep(j, 0, 3) cin >> c[j]; rep(k, 0, 3) { rep(j, 0, 3) { if (k != j) { new_dp[j] = max(new_dp[j], dp[i] + c[j]); } } } dp = new_dp; } cout << max(dp[0], max(dp[1], dp[2])); }
#include <algorithm> #include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define rep(i, a, b) for (ll i = a; i < b; i++) #define repb(i, a, b) for (ll i = a; i >= b; i--) #define err() cout << "==================================" << endl; #define errA(A) \ for (auto i : A) \ cout << i << " "; \ cout << endl; #define err1(a) cout << #a << " " << a << endl #define err2(a, b) cout << #a << " " << a << " " << #b << " " << b << endl #define err3(a, b, c) \ cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c \ << endl #define err4(a, b, c, d) \ cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c \ << " " << #d << " " << d << endl #define pb push_back #define all(A) A.begin(), A.end() #define allr(A) A.rbegin(), A.rend() #define ft first #define sd second #define pll pair<ll, ll> #define V vector<ll> #define S set<ll> #define VV vector<V> #define Vpll vector<pll> #define endl "\n" const ll logN = 20; const ll M = 1000000007; const ll INF = 1e12; #define PI 3.14159265 int main() { int n; cin >> n; vector<int> dp(3); rep(i, 0, n) { vector<int> new_dp(3, 0); vector<int> c(3); rep(j, 0, 3) cin >> c[j]; rep(k, 0, 3) { rep(j, 0, 3) { if (k != j) { new_dp[j] = max(new_dp[j], dp[k] + c[j]); } } } dp = new_dp; } cout << max(dp[0], max(dp[1], dp[2])); }
replace
54
55
54
55
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int w[101], v[101]; long long f[101][10001]; int main() { int n, w1, i, j; scanf("%d %d", &n, &w1); for (i = 1; i <= n; i++) scanf("%d %d", &w[i], &v[i]); for (i = 1; i <= n; i++) { for (j = 1; j <= w1; j++) { if (j < w[i]) f[i][j] = f[i - 1][j]; else f[i][j] = max(f[i - 1][j], f[i - 1][j - w[i]] + v[i]); } } printf("%lld", f[n][w1]); return 0; }
#include <bits/stdc++.h> using namespace std; int w[101], v[101]; long long f[101][100001]; int main() { int n, w1, i, j; scanf("%d %d", &n, &w1); for (i = 1; i <= n; i++) scanf("%d %d", &w[i], &v[i]); for (i = 1; i <= n; i++) { for (j = 1; j <= w1; j++) { if (j < w[i]) f[i][j] = f[i - 1][j]; else f[i][j] = max(f[i - 1][j], f[i - 1][j - w[i]] + v[i]); } } printf("%lld", f[n][w1]); return 0; }
replace
3
4
3
4
0
p03163
C++
Memory Limit Exceeded
#include <cmath> #include <iostream> #include <vector> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; typedef long long ll; int main() { int n; cin >> n; int W; cin >> W; vector<int> w(n); vector<ll> v(n); rep(i, n) cin >> w[i] >> v[i]; // solve vector<vector<ll>> dp(n + 100, vector<ll>(1e6)); rep(i, n) { for (int j = 0; j <= W; j++) { if (j + w[i] <= W) { dp[i + 1][j + w[i]] = max(dp[i + 1][j + w[i]], dp[i][j] + v[i]); } dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]); } } cout << dp[n][W] << endl; }
#include <cmath> #include <iostream> #include <vector> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; typedef long long ll; int main() { int n; cin >> n; int W; cin >> W; vector<int> w(n); vector<ll> v(n); rep(i, n) cin >> w[i] >> v[i]; // solve vector<vector<ll>> dp(1000, vector<ll>(1e5 + 10)); rep(i, n) { for (int j = 0; j <= W; j++) { if (j + w[i] <= W) { dp[i + 1][j + w[i]] = max(dp[i + 1][j + w[i]], dp[i][j] + v[i]); } dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]); } } cout << dp[n][W] << endl; }
replace
16
17
16
17
MLE
p03163
C++
Runtime Error
#include <cstdio> #include <iostream> using namespace std; const int MAXN = 105; typedef long long ll; ll v[MAXN], w[MAXN]; ll dp[MAXN] = {0}; int main() { int N, M; ll maxx = 0; cin >> N >> M; for (int i = 0; i < N; ++i) { cin >> v[i] >> w[i]; } for (int i = 0; i < N; ++i) { for (int j = M; j >= v[i]; --j) { dp[j] = max(dp[j], dp[j - v[i]] + w[i]); maxx = max(maxx, dp[j]); } // for(int j=0;j<=M;++j) // cout<<dp[j]<<" "; // cout<<endl; } cout << maxx << endl; return 0; }
#include <cstdio> #include <iostream> using namespace std; const int MAXN = 1e5 + 5; typedef long long ll; ll v[MAXN], w[MAXN]; ll dp[MAXN] = {0}; int main() { int N, M; ll maxx = 0; cin >> N >> M; for (int i = 0; i < N; ++i) { cin >> v[i] >> w[i]; } for (int i = 0; i < N; ++i) { for (int j = M; j >= v[i]; --j) { dp[j] = max(dp[j], dp[j - v[i]] + w[i]); maxx = max(maxx, dp[j]); } // for(int j=0;j<=M;++j) // cout<<dp[j]<<" "; // cout<<endl; } cout << maxx << endl; return 0; }
replace
3
4
3
4
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> // #include<ext/pb_ds/assoc_container.hpp> // #include<ext/pb_ds/tree_policy.hpp> #define IOS \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define MAX 2000005 #define MOD 1000000007 #define pb push_back #define mp make_pair #define pii pair<ll, ll> #define vi vector<ll> #define vp vector<pii> #define all(v) v.begin(), v.end() #define lb(v, x) lower_bound(all(v), x) #define ub(v, x) upper_bound(all(v), x) #define sz(x) (ll) x.size() #define F first #define S second #define FOR(i, a, b) for (ll i = a; i < b; ++i) #define ROF(i, a, b) for (ll i = a; i >= b; --i) #define trace(x) cerr << #x << ": " << x << '\n'; typedef long long ll; using namespace std; // using namespace __gnu_pbds; // #define ordered_set tree<int, null_type, less<int>, rb_tree_tag, // tree_order_statistics_node_update> ll mexp(ll a, ll b) { ll ans = 1; a %= MOD; while (b) { if (b & 1) ans = ans * a % MOD; b >>= 1; a = a * a % MOD; } return ans; } ll gcd(ll a, ll b) { if (a == 0) { return b; } return gcd(b % a, a); } const int N = 105; ll dp[N][N]; int main() { IOS int n, W; cin >> n >> W; ll w[n + 1], v[n + 1]; FOR(i, 1, n + 1) { cin >> w[i] >> v[i]; } FOR(i, 1, n + 1) { FOR(j, 1, W + 1) { if (w[i] <= j) { dp[i][j] = max(dp[i - 1][j - w[i]] + v[i], dp[i - 1][j]); } else { dp[i][j] = dp[i - 1][j]; } } } cout << dp[n][W]; cerr << 1.0 / CLOCKS_PER_SEC << endl; return 0; }
#include <bits/stdc++.h> // #include<ext/pb_ds/assoc_container.hpp> // #include<ext/pb_ds/tree_policy.hpp> #define IOS \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define MAX 2000005 #define MOD 1000000007 #define pb push_back #define mp make_pair #define pii pair<ll, ll> #define vi vector<ll> #define vp vector<pii> #define all(v) v.begin(), v.end() #define lb(v, x) lower_bound(all(v), x) #define ub(v, x) upper_bound(all(v), x) #define sz(x) (ll) x.size() #define F first #define S second #define FOR(i, a, b) for (ll i = a; i < b; ++i) #define ROF(i, a, b) for (ll i = a; i >= b; --i) #define trace(x) cerr << #x << ": " << x << '\n'; typedef long long ll; using namespace std; // using namespace __gnu_pbds; // #define ordered_set tree<int, null_type, less<int>, rb_tree_tag, // tree_order_statistics_node_update> ll mexp(ll a, ll b) { ll ans = 1; a %= MOD; while (b) { if (b & 1) ans = ans * a % MOD; b >>= 1; a = a * a % MOD; } return ans; } ll gcd(ll a, ll b) { if (a == 0) { return b; } return gcd(b % a, a); } const int N = 105; ll dp[N][100005]; int main() { IOS int n, W; cin >> n >> W; ll w[n + 1], v[n + 1]; FOR(i, 1, n + 1) { cin >> w[i] >> v[i]; } FOR(i, 1, n + 1) { FOR(j, 1, W + 1) { if (w[i] <= j) { dp[i][j] = max(dp[i - 1][j - w[i]] + v[i], dp[i - 1][j]); } else { dp[i][j] = dp[i - 1][j]; } } } cout << dp[n][W]; cerr << 1.0 / CLOCKS_PER_SEC << endl; return 0; }
replace
50
51
50
51
0
1e-06
p03163
C++
Time Limit Exceeded
#include <cmath> #include <iostream> #include <unistd.h> #include <vector> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; typedef long long ll; int n, W; // DPtable void show(vector<vector<ll>> &dp, int i) { printf("\n"); printf(" |"); rep(i, W + 1) printf("%3d", i); printf("\n"); printf("---+"); rep(i, 10) printf("---"); printf("\n"); rep(i, n + 1) { printf("%3d|", i); rep(j, W + 1) { printf("%3lld", dp[i][j]); } printf("\n"); } sleep(i); } int main() { cin >> n; cin >> W; vector<int> w(n); vector<ll> v(n); rep(i, n) cin >> w[i] >> v[i]; // solve vector<vector<ll>> dp(1000, vector<ll>(1e5 + 10)); rep(i, n) { for (int j = 0; 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]; } show(dp, 1); } } cout << dp[n][W] << endl; }
#include <cmath> #include <iostream> #include <unistd.h> #include <vector> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; typedef long long ll; int n, W; // DPtable void show(vector<vector<ll>> &dp, int i) { printf("\n"); printf(" |"); rep(i, W + 1) printf("%3d", i); printf("\n"); printf("---+"); rep(i, 10) printf("---"); printf("\n"); rep(i, n + 1) { printf("%3d|", i); rep(j, W + 1) { printf("%3lld", dp[i][j]); } printf("\n"); } sleep(i); } int main() { cin >> n; cin >> W; vector<int> w(n); vector<ll> v(n); rep(i, n) cin >> w[i] >> v[i]; // solve vector<vector<ll>> dp(1000, vector<ll>(1e5 + 10)); rep(i, n) { for (int j = 0; 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]; } // show(dp, 1); } } cout << dp[n][W] << endl; }
replace
42
43
42
43
TLE
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; long long n, W, dp[201][200001]; pair<long long, long long> v[201]; int main() { cin >> n >> W; for (int i = 1; i <= n; i++) cin >> v[i].first >> v[i].second; sort(v + 1, v + 1 + n); for (int i = 1; i <= W; i++) dp[i][0] = dp[0][i] = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= W; j++) { if (j < v[i].first) dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); else dp[i][j] = max(max(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - v[i].first] + v[i].second); } } cout << dp[n][W]; }
#include <bits/stdc++.h> using namespace std; long long n, W, dp[201][200001]; pair<long long, long long> v[201]; int main() { cin >> n >> W; for (int i = 1; i <= n; i++) cin >> v[i].first >> v[i].second; sort(v + 1, v + 1 + n); for (int i = 1; i <= n; i++) { for (int j = 1; j <= W; j++) { if (j < v[i].first) dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); else dp[i][j] = max(max(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - v[i].first] + v[i].second); } } cout << dp[n][W]; }
delete
9
11
9
9
-11
p03163
C++
Runtime Error
#include <iostream> using namespace std; long long int N, W; long long int v[108], w[108]; long long int dp[108][10800]; // 大きいほうを返す関数 long long int mx(long long int a, long long int b) { if (a > b) { return a; } else { return b; } } int main() { cin >> N >> W; for (long long int i = 0; i < N; i++) { cin >> w[i] >> v[i]; } for (long long int i = 0; i < N; i++) { for (long long int j = 0; j <= W; j++) { if (i == 0) { if (j >= w[0]) { dp[i][j] = v[i]; } else { dp[i][j] = 0; } } else if (j - w[i] < 0) { dp[i][j] = dp[i - 1][j]; } else { dp[i][j] = mx(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]); } } } cout << dp[N - 1][W] << endl; }
#include <iostream> using namespace std; long long int N, W; long long int v[108], w[108]; long long int dp[108][108000]; // 大きいほうを返す関数 long long int mx(long long int a, long long int b) { if (a > b) { return a; } else { return b; } } int main() { cin >> N >> W; for (long long int i = 0; i < N; i++) { cin >> w[i] >> v[i]; } for (long long int i = 0; i < N; i++) { for (long long int j = 0; j <= W; j++) { if (i == 0) { if (j >= w[0]) { dp[i][j] = v[i]; } else { dp[i][j] = 0; } } else if (j - w[i] < 0) { dp[i][j] = dp[i - 1][j]; } else { dp[i][j] = mx(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]); } } } cout << dp[N - 1][W] << endl; }
replace
6
7
6
7
0
p03163
C++
Time Limit Exceeded
// ABHISHEK AGRAWAL// // Newbie......You have to be odd to be no. ONE :)// #include <bits/stdc++.h> using namespace std; #define ll long long int #define ld long double #define fl float #define lcm(a, b) (a * (b / __gcd(a, b))) #define vs vector<string> #define vc vector<char> #define vll vector<ll> #define sll set<ll> #define pll pair<ll, ll> #define plc pair<ll, char> #define tlll tuple<ll, ll, ll> #define mt make_tuple #define vpll vector<pair<ll, ll>> #define vtll vector<tuple<ll, ll, ll>> #define vvll vector<vector<ll>> #define lb lower_bound #define pb push_back #define pob pop_back #define f first #define s second #define mll map<ll, ll> #define mp make_pair #define sp(n) fixed << setprecision(n) #define mcl map<char, ll> #define mcc map<char, char> #define msl map<string, ll> #define mss map<string, string> #define mod (ll)1000000007 #define flash \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define test \ ll t; \ read(t); \ while (t--) #define sortv(v) sort(v.begin(), v.end()) #define INF (ll)(1e15) #define loop(i, n) for (ll i = 0; i < n; i++) #define loop1(i, n) for (ll i = 1; i <= n; i++) #define rloop(n, i) for (ll i = n - 1; i >= 0; i--) #define loopab(i, a, b, c) for (ll i = a; i <= b; i += c) #define max3(a, b, c) max(max(a, b), c) #define min3(a, b, c) min(min(a, b), c) #define reada(a, n) loop(i, n) cin >> a[i]; #define reada1(a, n) loop1(i, n) cin >> a[i]; #define sorta(a) sort(a, a + n, greater<ll>()) #define countone(n) __builtin_popcount(n) #define numoftrailzero(n) __builtin_ctz(n) #define maxpowoftwo(n) __builtin_clz(n) #define leastindexwithone(n) __builtin_ffs(n) #define what_is(x) cerr << #x << " is " << x << endl; #define pfv(v) \ cout << v.size() << "\n"; \ loop(i, v.size()) cout << v[i] << " "; #define pv(v) loop(i, v.size()) cout << v[i] << " "; // if we need to devide any given number in powers of term the power of max term #define answer(n) ceil(log2(n + 1)) ll n, q; ll gcd(ll a, ll b) { if (b == 0) { return a; } return gcd(b, a % b); } ll powe(ll a, ll b) { ll res = 1; while (b > 0) { if (b % 2 == 1) { res = (res * a); } a = (a * a); b /= 2; } return res; } ll power(ll a, ll b, ll M) { a %= M; ll res = 1; while (b > 0) { if (b % 2 == 1) { res = (res * a) % M; } a = (a * a) % M; b /= 2; } return res; } ll extendedEuclid(ll A, ll B, ll &x, ll &y) { if (B == 0) { x = 1; y = 0; return A; } else { ll x1, y1; ll gcd = extendedEuclid(B, A % B, x1, y1); y = x1 - (A / B) * y1; x = y1; return gcd; } } ll mi(ll A, ll M) { ll x, y; extendedEuclid(A, M, x, y); if (x < 0) { x += mod; } return x; } template <typename T> void read(T &x) { cin >> x; } template <typename T, typename T0> void read(T &x, T0 &y) { cin >> x >> y; } template <typename T, typename T0, typename T1> void read(T &x, T0 &y, T1 &z) { cin >> x >> y >> z; } template <typename T, typename T0, typename T1, typename T2> void read(T &x, T0 &y, T1 &z, T2 &w) { cin >> x >> y >> z >> w; } // pair// // read pair// template <typename T, typename T0> void read(pair<T, T0> &p) { cin >> p.f >> p.s; } // write pair// template <typename T, typename T0> void write(pair<T, T0> &p) { write(p.f); write(p.s); } // vector// // read vector// template <typename T> void read(vector<T> &oneD, ll n) { loop(i, n) { ll x; read(x); oneD.pb(x); } } // array// // read array// template <typename T> void read(T oneD[], ll n) { loop(i, n) { read(oneD[i]); } } // write array// template <typename T> void write(T oneD[], int n) { loop(i, n) { write(oneD[i]); } cout << endl; } vector<bool> sieve(1000000, true); void Sieve() { sieve[0] = false; sieve[1] = false; for (ll i = 2; i * i <= 1000000; i++) { if (sieve[i] == true) { for (ll j = i * i; j < 1000000; j += i) sieve[j] = false; } } } vll sieve_spf; void Sieve_spf() { const ll n = 1e6 + 5; sieve_spf.resize(n); loop(i, n) sieve_spf[i] = i; sieve_spf[0] = -1; sieve_spf[1] = 1; loopab(i, 2, n, 2) sieve_spf[i] = 2; loopab(i, 3, n, 2) if (sieve_spf[i] == i) loopab(j, i * i, n, i) if (sieve_spf[j] == j) sieve_spf[j] = i; } bool oppositeSigns(ll x, ll y) { return ((x ^ y) < 0); } int mpt(ll n) { ll c = 0; ll n1 = pow(2, c); while (n1 < n) { c++; n1 = pow(2, c); } return c; } void the_happiest_place_on_earth() { flash; #ifdef ENABLE_FILE_IO freopen("in", "r", stdin); freopen("out", "w", stdout); #endif } const int N = 400023; ll wt[N], val[N]; ll knapsack(ll wt[], ll val[], ll w, ll n) { if (n == 0 || w == 0) return 0; if (wt[n - 1] <= w) return max(val[n - 1] + knapsack(wt, val, w - wt[n - 1], n - 1), knapsack(wt, val, w, n - 1)); else return knapsack(wt, val, w, n - 1); } void solve() { ll w; read(n, w); ll dp[n + 1][w + 1]; loop(i, n) read(wt[i], val[i]); loop(i, n + 1) loop(j, w + 1) if (i == 0 || j == 0) dp[i][j] = 0; loop1(i, n) { loop1(j, w) { 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] << "\n"; return; } int main() { the_happiest_place_on_earth(); // Today's thought: Push yourself, because no one else is going to do it for // you. Things aren’t always #000000 and #FFFFFF START OF PROGRAM LOGIC ll w; read(n, w); ll dp[n + 1][w + 1]; loop(i, n) read(wt[i], val[i]); cout << knapsack(wt, val, w, n); // solve(); cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; // END OF PROGRAM LOGIC return 0; }
// ABHISHEK AGRAWAL// // Newbie......You have to be odd to be no. ONE :)// #include <bits/stdc++.h> using namespace std; #define ll long long int #define ld long double #define fl float #define lcm(a, b) (a * (b / __gcd(a, b))) #define vs vector<string> #define vc vector<char> #define vll vector<ll> #define sll set<ll> #define pll pair<ll, ll> #define plc pair<ll, char> #define tlll tuple<ll, ll, ll> #define mt make_tuple #define vpll vector<pair<ll, ll>> #define vtll vector<tuple<ll, ll, ll>> #define vvll vector<vector<ll>> #define lb lower_bound #define pb push_back #define pob pop_back #define f first #define s second #define mll map<ll, ll> #define mp make_pair #define sp(n) fixed << setprecision(n) #define mcl map<char, ll> #define mcc map<char, char> #define msl map<string, ll> #define mss map<string, string> #define mod (ll)1000000007 #define flash \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define test \ ll t; \ read(t); \ while (t--) #define sortv(v) sort(v.begin(), v.end()) #define INF (ll)(1e15) #define loop(i, n) for (ll i = 0; i < n; i++) #define loop1(i, n) for (ll i = 1; i <= n; i++) #define rloop(n, i) for (ll i = n - 1; i >= 0; i--) #define loopab(i, a, b, c) for (ll i = a; i <= b; i += c) #define max3(a, b, c) max(max(a, b), c) #define min3(a, b, c) min(min(a, b), c) #define reada(a, n) loop(i, n) cin >> a[i]; #define reada1(a, n) loop1(i, n) cin >> a[i]; #define sorta(a) sort(a, a + n, greater<ll>()) #define countone(n) __builtin_popcount(n) #define numoftrailzero(n) __builtin_ctz(n) #define maxpowoftwo(n) __builtin_clz(n) #define leastindexwithone(n) __builtin_ffs(n) #define what_is(x) cerr << #x << " is " << x << endl; #define pfv(v) \ cout << v.size() << "\n"; \ loop(i, v.size()) cout << v[i] << " "; #define pv(v) loop(i, v.size()) cout << v[i] << " "; // if we need to devide any given number in powers of term the power of max term #define answer(n) ceil(log2(n + 1)) ll n, q; ll gcd(ll a, ll b) { if (b == 0) { return a; } return gcd(b, a % b); } ll powe(ll a, ll b) { ll res = 1; while (b > 0) { if (b % 2 == 1) { res = (res * a); } a = (a * a); b /= 2; } return res; } ll power(ll a, ll b, ll M) { a %= M; ll res = 1; while (b > 0) { if (b % 2 == 1) { res = (res * a) % M; } a = (a * a) % M; b /= 2; } return res; } ll extendedEuclid(ll A, ll B, ll &x, ll &y) { if (B == 0) { x = 1; y = 0; return A; } else { ll x1, y1; ll gcd = extendedEuclid(B, A % B, x1, y1); y = x1 - (A / B) * y1; x = y1; return gcd; } } ll mi(ll A, ll M) { ll x, y; extendedEuclid(A, M, x, y); if (x < 0) { x += mod; } return x; } template <typename T> void read(T &x) { cin >> x; } template <typename T, typename T0> void read(T &x, T0 &y) { cin >> x >> y; } template <typename T, typename T0, typename T1> void read(T &x, T0 &y, T1 &z) { cin >> x >> y >> z; } template <typename T, typename T0, typename T1, typename T2> void read(T &x, T0 &y, T1 &z, T2 &w) { cin >> x >> y >> z >> w; } // pair// // read pair// template <typename T, typename T0> void read(pair<T, T0> &p) { cin >> p.f >> p.s; } // write pair// template <typename T, typename T0> void write(pair<T, T0> &p) { write(p.f); write(p.s); } // vector// // read vector// template <typename T> void read(vector<T> &oneD, ll n) { loop(i, n) { ll x; read(x); oneD.pb(x); } } // array// // read array// template <typename T> void read(T oneD[], ll n) { loop(i, n) { read(oneD[i]); } } // write array// template <typename T> void write(T oneD[], int n) { loop(i, n) { write(oneD[i]); } cout << endl; } vector<bool> sieve(1000000, true); void Sieve() { sieve[0] = false; sieve[1] = false; for (ll i = 2; i * i <= 1000000; i++) { if (sieve[i] == true) { for (ll j = i * i; j < 1000000; j += i) sieve[j] = false; } } } vll sieve_spf; void Sieve_spf() { const ll n = 1e6 + 5; sieve_spf.resize(n); loop(i, n) sieve_spf[i] = i; sieve_spf[0] = -1; sieve_spf[1] = 1; loopab(i, 2, n, 2) sieve_spf[i] = 2; loopab(i, 3, n, 2) if (sieve_spf[i] == i) loopab(j, i * i, n, i) if (sieve_spf[j] == j) sieve_spf[j] = i; } bool oppositeSigns(ll x, ll y) { return ((x ^ y) < 0); } int mpt(ll n) { ll c = 0; ll n1 = pow(2, c); while (n1 < n) { c++; n1 = pow(2, c); } return c; } void the_happiest_place_on_earth() { flash; #ifdef ENABLE_FILE_IO freopen("in", "r", stdin); freopen("out", "w", stdout); #endif } const int N = 400023; ll wt[N], val[N]; ll knapsack(ll wt[], ll val[], ll w, ll n) { if (n == 0 || w == 0) return 0; if (wt[n - 1] <= w) return max(val[n - 1] + knapsack(wt, val, w - wt[n - 1], n - 1), knapsack(wt, val, w, n - 1)); else return knapsack(wt, val, w, n - 1); } void solve() { ll w; read(n, w); ll dp[n + 1][w + 1]; loop(i, n) read(wt[i], val[i]); loop(i, n + 1) loop(j, w + 1) if (i == 0 || j == 0) dp[i][j] = 0; loop1(i, n) { loop1(j, w) { 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] << "\n"; return; } int main() { the_happiest_place_on_earth(); // Today's thought: Push yourself, because no one else is going to do it for // you. Things aren’t always #000000 and #FFFFFF START OF PROGRAM LOGIC // ll w;read(n,w);ll dp[n+1][w+1]; // loop(i,n) read(wt[i],val[i]); // cout<<knapsack(wt,val,w,n); solve(); cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; // END OF PROGRAM LOGIC return 0; }
replace
230
236
230
234
TLE
p03163
C++
Runtime Error
#include <iostream> #include <string> #include <vector> #define REP(i, start, n) for (int i = start; i < n; ++i) #define debug(var) \ do { \ std::cout << #var << " : "; \ view(var); \ } while (0) #define debug2d(var) \ do { \ std::cout << #var << " : \n"; \ view(var); \ } while (0) template <typename T> void view(T e) { std::cout << e << std::endl; } template <typename T> void view(const std::vector<T> &v) { for (const auto &e : v) { std::cout << e << " "; } std::cout << std::endl; } template <typename T> void view(const std::vector<std::vector<T>> &vv) { for (const auto &v : vv) { view(v); } } using namespace std; using ll = long long; using P = pair<int, int>; int main() { int N, W; cin >> N >> W; vector<P> wv(N); vector<vector<ll>> dp(N + 1, vector<ll>(W + 1)); REP(i, 1, N + 1) cin >> wv[i].first >> wv[i].second; REP(j, 0, W + 1) { dp[0][j] = 0; } REP(i, 1, N + 1) REP(j, 1, W + 1) { if (wv[i].first <= j) { dp[i][j] = max(dp[i - 1][j - wv[i].first] + wv[i].second, dp[i - 1][j]); } else { dp[i][j] = dp[i - 1][j]; } } cout << dp[N][W] << endl; return 0; }
#include <iostream> #include <string> #include <vector> #define REP(i, start, n) for (int i = start; i < n; ++i) #define debug(var) \ do { \ std::cout << #var << " : "; \ view(var); \ } while (0) #define debug2d(var) \ do { \ std::cout << #var << " : \n"; \ view(var); \ } while (0) template <typename T> void view(T e) { std::cout << e << std::endl; } template <typename T> void view(const std::vector<T> &v) { for (const auto &e : v) { std::cout << e << " "; } std::cout << std::endl; } template <typename T> void view(const std::vector<std::vector<T>> &vv) { for (const auto &v : vv) { view(v); } } using namespace std; using ll = long long; using P = pair<int, int>; int main() { int N, W; cin >> N >> W; vector<P> wv(N + 1); vector<vector<ll>> dp(N + 1, vector<ll>(W + 1)); REP(i, 1, N + 1) cin >> wv[i].first >> wv[i].second; REP(j, 0, W + 1) { dp[0][j] = 0; } REP(i, 1, N + 1) REP(j, 1, W + 1) { if (wv[i].first <= j) { dp[i][j] = max(dp[i - 1][j - wv[i].first] + wv[i].second, dp[i - 1][j]); } else { dp[i][j] = dp[i - 1][j]; } } cout << dp[N][W] << endl; return 0; }
replace
35
36
35
36
0
p03163
C++
Runtime Error
#include <algorithm> #include <iostream> #include <string.h> using namespace std; // Sol 3A // forward dp // enumerating items from beginning (z = 0..N) // inner loop is looping // "weights >= the current item weight" // in descending order long long dp[10005][105]; long long solve(int N, int W, int *v, int *w) { // items are 0 indexed, 0..N-1'th items // dp[a][b] = solution for a knapsack of weight a, // only can use items until and including b'th item // long long dp[W + 1][N + 1]; // memset(dp, 0, sizeof(long long) * (W + 1) * (N + 1)); for (int z = 0; z < N; ++z) { for (int q = W - w[z]; q >= 0; --q) { dp[q + w[z]][z] = max(dp[q + w[z]][z], v[z] + dp[q][z]); } for (int q = 0; q < W + 1; ++q) { dp[q][z + 1] = dp[q][z]; } } // for (int q = 0; q < W + 1; ++q) { // for (int z = 0; z < N; ++z) { // cout << dp[q][z] << ' '; // } // cout << '\n'; // } return dp[W][N - 1]; } int main() { int N, W; cin >> N >> W; int v[N], w[N]; for (int q = 0; q < N; ++q) cin >> w[q] >> v[q]; long long ans = solve(N, W, v, w); cout << ans << '\n'; }
#include <algorithm> #include <iostream> #include <string.h> using namespace std; // Sol 3A // forward dp // enumerating items from beginning (z = 0..N) // inner loop is looping // "weights >= the current item weight" // in descending order long long dp[100005][105]; long long solve(int N, int W, int *v, int *w) { // items are 0 indexed, 0..N-1'th items // dp[a][b] = solution for a knapsack of weight a, // only can use items until and including b'th item // long long dp[W + 1][N + 1]; // memset(dp, 0, sizeof(long long) * (W + 1) * (N + 1)); for (int z = 0; z < N; ++z) { for (int q = W - w[z]; q >= 0; --q) { dp[q + w[z]][z] = max(dp[q + w[z]][z], v[z] + dp[q][z]); } for (int q = 0; q < W + 1; ++q) { dp[q][z + 1] = dp[q][z]; } } // for (int q = 0; q < W + 1; ++q) { // for (int z = 0; z < N; ++z) { // cout << dp[q][z] << ' '; // } // cout << '\n'; // } return dp[W][N - 1]; } int main() { int N, W; cin >> N >> W; int v[N], w[N]; for (int q = 0; q < N; ++q) cin >> w[q] >> v[q]; long long ans = solve(N, W, v, w); cout << ans << '\n'; }
replace
11
12
11
12
0
p03163
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iostream> #include <string> #include <vector> using namespace std; typedef long long ll; int n, W; vector<ll> w, v; ll dp[100][(int)1e5 + 1]; int main() { int t = 1; // cin >> t ; while (t--) { for (int i = 0; i < 100; i++) for (int j = 0; j <= 1e5; j++) dp[i][j] = 0; cin >> n >> W; w.resize(n); v.resize(n); for (int i = 0; i < n; i++) cin >> w[i] >> v[i]; // cout<<fun(0,W) << endl ; for (int i = n - 1; i >= 0; i--) for (int c = 0; c <= W; c++) // ? best value in pathars i, i+1, ... n with capacity c. { if (i + 1 < n) dp[i][c] = dp[i + 1][c]; if (c >= w[i]) { dp[i][c] = max(dp[i][c], v[i] + dp[i + 1][c - w[i]]); } } cout << dp[0][W] << endl; } return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <string> #include <vector> using namespace std; typedef long long ll; int n, W; vector<ll> w, v; ll dp[100][(int)1e5 + 1]; int main() { int t = 1; // cin >> t ; while (t--) { for (int i = 0; i < 100; i++) for (int j = 0; j <= 1e5; j++) dp[i][j] = 0; cin >> n >> W; w.resize(n); v.resize(n); for (int i = 0; i < n; i++) cin >> w[i] >> v[i]; // cout<<fun(0,W) << endl ; for (int i = n - 1; i >= 0; i--) for (int c = 0; c <= W; c++) // ? best value in pathars i, i+1, ... n with capacity c. { if (i + 1 < n) dp[i][c] = dp[i + 1][c]; if (c >= w[i]) { if (i + 1 < n) dp[i][c] = max(dp[i][c], v[i] + dp[i + 1][c - w[i]]); else dp[i][c] = v[i]; } } cout << dp[0][W] << endl; } return 0; }
replace
38
39
38
42
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; long long dp[100][100005], vi; int main() { int n, w, wi; cin >> n >> w; ; for (int i = 0; i < w; i++) dp[0][i] = 0; for (int i = 1; i <= n; i++) { cin >> wi >> vi; for (int p = 0; p <= w; p++) { if (wi <= p) dp[i][p] = max(dp[i - 1][p], vi + dp[i - 1][p - wi]); else dp[i][p] = dp[i - 1][p]; } } cout << dp[n][w]; return 0; }
#include <bits/stdc++.h> using namespace std; long long dp[105][100005], vi; int main() { int n, w, wi; cin >> n >> w; ; for (int i = 0; i < w; i++) dp[0][i] = 0; for (int i = 1; i <= n; i++) { cin >> wi >> vi; for (int p = 0; p <= w; p++) { if (wi <= p) dp[i][p] = max(dp[i - 1][p], vi + dp[i - 1][p - wi]); else dp[i][p] = dp[i - 1][p]; } } cout << dp[n][w]; return 0; }
replace
3
4
3
4
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int t = 1; // cin >> t; while (t--) { int n; cin >> n; long long w; cin >> w; long long *val = new long long[n]; long long *we = new long long[n]; for (int i = 0; i < n; i++) { cin >> we[i]; cin >> val[i]; } long long dp[10001][10001]; for (int i = 0; i < n; i++) { dp[0][i] = 0; } for (int i = 0; i < n; i++) { dp[i][0] = 0; } int k; for (int i = 1; i <= n; i++) { for (int j = 1; j <= w; j++) { if (we[i - 1] <= j) { dp[i][j] = max(val[i - 1] + dp[i - 1][j - we[i - 1]], dp[i - 1][j]); } else dp[i][j] = dp[i - 1][j]; } } cout << dp[n][w]; } }
#include <bits/stdc++.h> using namespace std; int main() { int t = 1; // cin >> t; while (t--) { int n; cin >> n; long long w; cin >> w; long long *val = new long long[n]; long long *we = new long long[n]; for (int i = 0; i < n; i++) { cin >> we[i]; cin >> val[i]; } long long dp[101][100001]; for (int i = 0; i < n; i++) { dp[0][i] = 0; } for (int i = 0; i < n; i++) { dp[i][0] = 0; } int k; for (int i = 1; i <= n; i++) { for (int j = 1; j <= w; j++) { if (we[i - 1] <= j) { dp[i][j] = max(val[i - 1] + dp[i - 1][j - we[i - 1]], dp[i - 1][j]); } else dp[i][j] = dp[i - 1][j]; } } cout << dp[n][w]; } }
replace
18
19
18
19
-11
p03163
C++
Runtime Error
// atcoder knapsack 1 #include <bits/stdc++.h> #define N_ 101000 using namespace std; long long D[N_]; int main() { int a, b, n, W; cin >> n >> W; for (int i = 1; i <= n; i++) { cin >> a >> b; for (int j = W; j >= 0; j--) D[j + a] = max(D[j + a], D[j] + b); } long long r = 0; for (int i = 0; i <= W; i++) r = max(r, D[i]); cout << r << endl; return 0; }
// atcoder knapsack 1 #include <bits/stdc++.h> #define N_ 101000000 using namespace std; long long D[N_]; int main() { int a, b, n, W; cin >> n >> W; for (int i = 1; i <= n; i++) { cin >> a >> b; for (int j = W; j >= 0; j--) D[j + a] = max(D[j + a], D[j] + b); } long long r = 0; for (int i = 0; i <= W; i++) r = max(r, D[i]); cout << r << endl; return 0; }
replace
2
3
2
3
0
p03163
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_set> #include <vector> using namespace std; #define FOR(i, N) for (int i = 0; i < (int)N; i++) #define FORIN(i, a, b) for (int i = a; i < (int)b; i++) #define ALL(x) (x).begin(), (x).end() #define MOD 1000000007 #define DEBUG(...) debug(__LINE__, ":" __VA_ARGS__) using Pi = pair<int, int>; using ll = long long; const int INF = 1 << 28; string to_string(string s) { return s; } template <class S, class T> string to_string(pair<S, T> p) { return "{" + to_string(p.first) + "," + to_string(p.second) + "}"; } template <class T> string to_string(vector<T> v) { string ret = "{"; for (int i = 0; i < v.size() - 1; ++i) { ret += to_string(v[i]) + ","; } if (v.size() > 0) { ret += to_string(v.back()); } ret += "}"; return ret; } void debug() { cerr << endl; } template <class Head, class... Tail> void debug(Head head, Tail... tail) { cerr << to_string(head) << " "; debug(tail...); } void print() { cout << endl; } template <class Head, class... Tail> void print(Head head, Tail... tail) { cout << to_string(head); print(tail...); } void get() {} template <class Head, class... Tail> void get(Head &head, Tail &...tail) { cin >> head; get(tail...); } template <class T> void getv(vector<T> &vec) { for (int i = 0; i < vec.size(); ++i) cin >> vec[i]; } int main() { ll N, W; cin >> N >> W; vector<ll> w(N), v(N); FOR(i, N) cin >> w[i] >> v[i]; vector<ll> dp(110000); FOR(i, N) { for (ll j = W; j >= 0; --j) { dp[j + w[i]] = max(dp[j + w[i]], dp[j] + v[i]); } } print(*max_element(dp.begin(), dp.begin() + W + 1)); return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_set> #include <vector> using namespace std; #define FOR(i, N) for (int i = 0; i < (int)N; i++) #define FORIN(i, a, b) for (int i = a; i < (int)b; i++) #define ALL(x) (x).begin(), (x).end() #define MOD 1000000007 #define DEBUG(...) debug(__LINE__, ":" __VA_ARGS__) using Pi = pair<int, int>; using ll = long long; const int INF = 1 << 28; string to_string(string s) { return s; } template <class S, class T> string to_string(pair<S, T> p) { return "{" + to_string(p.first) + "," + to_string(p.second) + "}"; } template <class T> string to_string(vector<T> v) { string ret = "{"; for (int i = 0; i < v.size() - 1; ++i) { ret += to_string(v[i]) + ","; } if (v.size() > 0) { ret += to_string(v.back()); } ret += "}"; return ret; } void debug() { cerr << endl; } template <class Head, class... Tail> void debug(Head head, Tail... tail) { cerr << to_string(head) << " "; debug(tail...); } void print() { cout << endl; } template <class Head, class... Tail> void print(Head head, Tail... tail) { cout << to_string(head); print(tail...); } void get() {} template <class Head, class... Tail> void get(Head &head, Tail &...tail) { cin >> head; get(tail...); } template <class T> void getv(vector<T> &vec) { for (int i = 0; i < vec.size(); ++i) cin >> vec[i]; } int main() { ll N, W; cin >> N >> W; vector<ll> w(N), v(N); FOR(i, N) cin >> w[i] >> v[i]; vector<ll> dp(210000); FOR(i, N) { for (ll j = W; j >= 0; --j) { dp[j + w[i]] = max(dp[j + w[i]], dp[j] + v[i]); } } print(*max_element(dp.begin(), dp.begin() + W + 1)); return 0; }
replace
75
76
75
76
0
p03163
C++
Runtime Error
// #Sazaの1日1AC #include <bits/stdc++.h> using namespace std; const long long mod = 1000000000 + 7; const long long INF = 1e16; // マクロ // 型エイリアス using ll = long long; using P = pair<long long, long long>; using vl = vector<long long>; using vvl = vector<vector<long long>>; using vP = vector<pair<long long, long long>>; // ショートカット #define rep(i, n) for (int i = 0; i < n; i++) #define rep2(i, k, n) for (int i = k; i < n; i++) // 半開区間 #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define pb push_back // 入力 #define vin(v, N) \ for (long long i = 0; i < N; i++) \ cin >> v.at(i) #define lin(n) \ long long n; \ cin >> n #define chin(x) \ char x; \ cin >> x; #define sin(s) \ string s; \ cin >> s; #define vlin(v, N) \ vector<long long> v(N); \ for (long long i = 0; i < N; i++) \ cin >> v.at(i) #define max(a, b) max((ll)a, (ll)b) #define min(a, b) min((ll)a, (ll)b) // 関数 // 最大公約数 long long gcd(long long m, long long n) { long long a = max(m, n); long long b = min(m, n); long long r = a % b; while (r != 0) { a = b; b = r; r = a % b; } return b; } // 最小公倍数 long long lcd(long long m, long long n) { return m * n / gcd(m, n); } // xのn乗 long long power(long long x, long long N) { long long ret = 1; for (long long i = 0; i < N; i++) ret *= x; return ret; } // 繰り返し二乗法 long long repeat_squaring(long long x, long long n) { if (n == 0) return 1; else if (n % 2 == 0) { long long t = repeat_squaring(x, n / 2); return t * t % mod; } else return x * repeat_squaring(x, n - 1); } // nCr long long Comb(long long n, long long r); // nCr (mod) long long modC(long long n, long long r) { vector<long long> f(n + 1), inv(n + 1), invf(n + 1); f[0] = f[1] = 1; invf[0] = invf[1] = 1; inv[1] = 1; for (long long i = 2; i < n + 1; i++) { f[i] = (i * f[i - 1]) % mod; inv[i] = mod - ((mod / i) * inv[mod % i]) % mod; invf[i] = (inv[i] * invf[i - 1]) % mod; } long long cmb = f[n] * (invf[r] * invf[n - r] % mod) % mod; if (n >= r) return cmb; else return 0; } // 素因数分解(mapで返す) map<long long, long long> factor(long long p) { ll p1 = p; map<long long, long long> ret; for (long long i = 2; i * i <= p1; i++) { while (p % i == 0) { ret[i]++; p /= i; } } if (p != 1) ret[p]++; return ret; } // 素数判定 bool is_prime(long long N) { for (long long i = 2; i * i <= N; i++) { if (N % i == 0) return false; } return true; } // 最大値更新 void chmax(long long &a, long long b) { a = max(a, b); } // 最小値更新 void chmin(long long &a, long long b) { a = min(a, b); } // 構造体 // Union-Find木 struct UnionFind { // メンバ変数 vector<long long> par; // par[i]:=頂点iの親 vector<long long> s; // s[i]:=頂点iが属する集合の個数(iが根のとき) // コンストラクタ UnionFind(long long N) : par(N), s(N) { for (long long i = 0; i < N; i++) { par[i] = i; // 最初はすべてが根として初期化 s[i] = 1; } } // メンバ関数 // root(i):=頂点iの根 long long root(long long i) { if (par[i] == i) return i; par[i] = root(par[i]); // 経路圧縮 return root(par[i]); // 再帰 } // size[i]:=iが属する集合の個数 long long size(long long i) { return s[root(i)]; } // same(x,y) xとyが同じ根を持つか(同じ集合に含まれるか) bool same(long long x, long long y) { return root(x) == root(y); } // unite(x,y) xの根をyの根に繋げる(集合を合併) void unite(long long x, long long y) { if (!same(x, y)) { long long rx = root(x); long long ry = root(y); par[rx] = par[ry]; s[ry] = s[rx] + s[ry]; } } }; // ここまでライブラリ ll dp[10000 + 10]; // メイン関数 int main() { // doubleの桁数 cout << fixed << setprecision(10); lin(N); lin(W); rep(i, N) { lin(w); lin(v); for (ll j = W; j >= 0; j--) { if (j - w >= 0) chmax(dp[j], dp[j - w] + v); } } cout << dp[W]; }
// #Sazaの1日1AC #include <bits/stdc++.h> using namespace std; const long long mod = 1000000000 + 7; const long long INF = 1e16; // マクロ // 型エイリアス using ll = long long; using P = pair<long long, long long>; using vl = vector<long long>; using vvl = vector<vector<long long>>; using vP = vector<pair<long long, long long>>; // ショートカット #define rep(i, n) for (int i = 0; i < n; i++) #define rep2(i, k, n) for (int i = k; i < n; i++) // 半開区間 #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define pb push_back // 入力 #define vin(v, N) \ for (long long i = 0; i < N; i++) \ cin >> v.at(i) #define lin(n) \ long long n; \ cin >> n #define chin(x) \ char x; \ cin >> x; #define sin(s) \ string s; \ cin >> s; #define vlin(v, N) \ vector<long long> v(N); \ for (long long i = 0; i < N; i++) \ cin >> v.at(i) #define max(a, b) max((ll)a, (ll)b) #define min(a, b) min((ll)a, (ll)b) // 関数 // 最大公約数 long long gcd(long long m, long long n) { long long a = max(m, n); long long b = min(m, n); long long r = a % b; while (r != 0) { a = b; b = r; r = a % b; } return b; } // 最小公倍数 long long lcd(long long m, long long n) { return m * n / gcd(m, n); } // xのn乗 long long power(long long x, long long N) { long long ret = 1; for (long long i = 0; i < N; i++) ret *= x; return ret; } // 繰り返し二乗法 long long repeat_squaring(long long x, long long n) { if (n == 0) return 1; else if (n % 2 == 0) { long long t = repeat_squaring(x, n / 2); return t * t % mod; } else return x * repeat_squaring(x, n - 1); } // nCr long long Comb(long long n, long long r); // nCr (mod) long long modC(long long n, long long r) { vector<long long> f(n + 1), inv(n + 1), invf(n + 1); f[0] = f[1] = 1; invf[0] = invf[1] = 1; inv[1] = 1; for (long long i = 2; i < n + 1; i++) { f[i] = (i * f[i - 1]) % mod; inv[i] = mod - ((mod / i) * inv[mod % i]) % mod; invf[i] = (inv[i] * invf[i - 1]) % mod; } long long cmb = f[n] * (invf[r] * invf[n - r] % mod) % mod; if (n >= r) return cmb; else return 0; } // 素因数分解(mapで返す) map<long long, long long> factor(long long p) { ll p1 = p; map<long long, long long> ret; for (long long i = 2; i * i <= p1; i++) { while (p % i == 0) { ret[i]++; p /= i; } } if (p != 1) ret[p]++; return ret; } // 素数判定 bool is_prime(long long N) { for (long long i = 2; i * i <= N; i++) { if (N % i == 0) return false; } return true; } // 最大値更新 void chmax(long long &a, long long b) { a = max(a, b); } // 最小値更新 void chmin(long long &a, long long b) { a = min(a, b); } // 構造体 // Union-Find木 struct UnionFind { // メンバ変数 vector<long long> par; // par[i]:=頂点iの親 vector<long long> s; // s[i]:=頂点iが属する集合の個数(iが根のとき) // コンストラクタ UnionFind(long long N) : par(N), s(N) { for (long long i = 0; i < N; i++) { par[i] = i; // 最初はすべてが根として初期化 s[i] = 1; } } // メンバ関数 // root(i):=頂点iの根 long long root(long long i) { if (par[i] == i) return i; par[i] = root(par[i]); // 経路圧縮 return root(par[i]); // 再帰 } // size[i]:=iが属する集合の個数 long long size(long long i) { return s[root(i)]; } // same(x,y) xとyが同じ根を持つか(同じ集合に含まれるか) bool same(long long x, long long y) { return root(x) == root(y); } // unite(x,y) xの根をyの根に繋げる(集合を合併) void unite(long long x, long long y) { if (!same(x, y)) { long long rx = root(x); long long ry = root(y); par[rx] = par[ry]; s[ry] = s[rx] + s[ry]; } } }; // ここまでライブラリ ll dp[100000 + 10]; // メイン関数 int main() { // doubleの桁数 cout << fixed << setprecision(10); lin(N); lin(W); rep(i, N) { lin(w); lin(v); for (ll j = W; j >= 0; j--) { if (j - w >= 0) chmax(dp[j], dp[j - w] + v); } } cout << dp[W]; }
replace
156
157
156
157
0
p03163
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define fi first #define se second using namespace std; typedef pair<int, int> ii; int n, w, v[105], we[105]; long long dp[100005][105]; long long recur(int cap, int i) { if (i > n) return 0; if (cap < we[i]) return recur(cap, i + 1); else return max(v[i] + recur(cap - we[i], i + 1), recur(cap, i + 1)); } int main() { cin >> n >> w; for (int i = 1; i <= n; ++i) cin >> we[i] >> v[i]; cout << recur(w, 1); return 0; }
#include <bits/stdc++.h> #define fi first #define se second using namespace std; typedef pair<int, int> ii; int n, w, v[105], we[105]; long long dp[100005][105]; long long recur(int cap, int i) { if (i > n) return 0; if (cap < we[i]) return recur(cap, i + 1); else return max(v[i] + recur(cap - we[i], i + 1), recur(cap, i + 1)); } int main() { cin >> n >> w; for (int i = 1; i <= n; ++i) cin >> we[i] >> v[i]; for (int i = 0; i <= w; ++i) dp[i][0] = 0; for (int i = 0; i <= n; ++i) dp[0][i] = 0; for (int i = 1; i <= w; ++i) { for (int j = 1; j <= n; ++j) { if (we[j] > i) dp[i][j] = dp[i][j - 1]; else dp[i][j] = max(v[j] + dp[i - we[j]][j - 1], dp[i][j - 1]); } } cout << dp[w][n]; return 0; }
replace
24
25
24
37
TLE
p03163
C++
Runtime Error
#include <iostream> using namespace std; pair<long long int, long long int> p[105]; long long int dp[100005]; int main() { long long int n, w; cin >> n >> w; for (long long int i = 0; i < n; i++) { long long int w1, v1; cin >> w1 >> v1; p[i] = make_pair(w1, v1); } for (long long int i = 0; i < n; i++) { for (long long int j = w; j >= 0; j--) { if ((j - p[i].first == 0 || dp[j - p[i].first] != 0) && j - p[i].first >= 0) { dp[j] = max(dp[j], p[i].second + dp[j - p[i].first]); } } /*for(long long int j=0;j<12;j++) { cout <<dp[j]<<" "; } cout <<endl;*/ } /*for(long long int j=w;j>=0;j--) { for(long long int k=0;k<n;k++) { if(j-p[k].first!=p[k].first && dp[j-p[k].first]!=0 && dp[p[k].first]!=0) dp[j]=max(dp[j],dp[p[k].first]+dp[j-p[k].first]); } }*/ long long int maxi = 0; for (long long int i = 0; i <= w; i++) { maxi = max(maxi, dp[i]); } cout << maxi; return 0; }
#include <iostream> using namespace std; pair<long long int, long long int> p[105]; long long int dp[100005]; int main() { long long int n, w; cin >> n >> w; for (long long int i = 0; i < n; i++) { long long int w1, v1; cin >> w1 >> v1; p[i] = make_pair(w1, v1); } for (long long int i = 0; i < n; i++) { for (long long int j = w; j >= 0; j--) { if (j - p[i].first >= 0 && (j - p[i].first == 0 || dp[j - p[i].first] != 0)) { dp[j] = max(dp[j], p[i].second + dp[j - p[i].first]); } } /*for(long long int j=0;j<12;j++) { cout <<dp[j]<<" "; } cout <<endl;*/ } /*for(long long int j=w;j>=0;j--) { for(long long int k=0;k<n;k++) { if(j-p[k].first!=p[k].first && dp[j-p[k].first]!=0 && dp[p[k].first]!=0) dp[j]=max(dp[j],dp[p[k].first]+dp[j-p[k].first]); } }*/ long long int maxi = 0; for (long long int i = 0; i <= w; i++) { maxi = max(maxi, dp[i]); } cout << maxi; return 0; }
replace
14
16
14
16
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; // #define Fast ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define int long long typedef vector<int> Vl; typedef pair<int, int> pii; typedef vector<pii> Vll; #define S second #define F first #define mp make_pair #define ms(x, y) memset(x, y, sizeof(x)) #define pb push_back #define sl(n) scanf("%lld", &n) #define pl(n) printf("%lld", n) #define sz(a) (int)(a).size() const int mod = 2e19; int dp[105][10005]; int recur(int w[], int val[], int i, int n, int W) { if (W < 0) return -mod; if (i == n) return 0; if (dp[i][W] != 0) return dp[i][W]; int take = val[i] + recur(w, val, i + 1, n, W - w[i]); int leav = recur(w, val, i + 1, n, W); return dp[i][W] = max(take, leav); } signed main() { ms(dp, 0); int n, W; sl(n); sl(W); int w[n], val[n]; for (int i = 0; i < n; i++) { sl(w[i]); sl(val[i]); } cout << recur(w, val, 0, n, W) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; // #define Fast ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define int long long typedef vector<int> Vl; typedef pair<int, int> pii; typedef vector<pii> Vll; #define S second #define F first #define mp make_pair #define ms(x, y) memset(x, y, sizeof(x)) #define pb push_back #define sl(n) scanf("%lld", &n) #define pl(n) printf("%lld", n) #define sz(a) (int)(a).size() const int mod = 2e19; int dp[105][100005]; int recur(int w[], int val[], int i, int n, int W) { if (W < 0) return -mod; if (i == n) return 0; if (dp[i][W] != 0) return dp[i][W]; int take = val[i] + recur(w, val, i + 1, n, W - w[i]); int leav = recur(w, val, i + 1, n, W); return dp[i][W] = max(take, leav); } signed main() { ms(dp, 0); int n, W; sl(n); sl(W); int w[n], val[n]; for (int i = 0; i < n; i++) { sl(w[i]); sl(val[i]); } cout << recur(w, val, 0, n, W) << endl; return 0; }
replace
16
17
16
17
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> 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 si set<int> #define usi unordered_set<int> #define mii map<int, int> #define umii unordered_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 endl '\n' #define ps(x, y) fixed << setprecision(y) << x #define mk(arr, n, type) type *arr = new type[n]; #define w(t) \ int t; \ cin >> t; \ while (t--) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); void jaiswal_sahil() { 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 } const int N = 1e2; int dp[N][N]; int n; int maxVal(int i, int w, int *wt, int *v) { if (i == n) return 0; if (dp[i][w] != -1) return dp[i][w]; int q1 = -inf, q2 = -inf; if (w >= wt[i]) { q1 = v[i] + maxVal(i + 1, w - wt[i], wt, v); } q2 = maxVal(i + 1, w, wt, v); return dp[i][w] = max(q1, q2); } int32_t main() { jaiswal_sahil(); int w; cin >> n >> w; int wt[n], v[n]; for (int i = 0; i < n; i++) cin >> wt[i] >> v[i]; memset(dp, -1, sizeof(dp)); int ans = maxVal(0, w, wt, v); cout << ans << endl; return 0; }
#include <bits/stdc++.h> 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 si set<int> #define usi unordered_set<int> #define mii map<int, int> #define umii unordered_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 endl '\n' #define ps(x, y) fixed << setprecision(y) << x #define mk(arr, n, type) type *arr = new type[n]; #define w(t) \ int t; \ cin >> t; \ while (t--) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); void jaiswal_sahil() { 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 } const int N = 1e2 + 2, W = 1e5 + 5; int dp[N][W]; int n; int maxVal(int i, int w, int *wt, int *v) { if (i == n) return 0; if (dp[i][w] != -1) return dp[i][w]; int q1 = -inf, q2 = -inf; if (w >= wt[i]) { q1 = v[i] + maxVal(i + 1, w - wt[i], wt, v); } q2 = maxVal(i + 1, w, wt, v); return dp[i][w] = max(q1, q2); } int32_t main() { jaiswal_sahil(); int w; cin >> n >> w; int wt[n], v[n]; for (int i = 0; i < n; i++) cin >> wt[i] >> v[i]; memset(dp, -1, sizeof(dp)); int ans = maxVal(0, w, wt, v); cout << ans << endl; return 0; }
replace
39
41
39
41
0
p03163
C++
Runtime Error
#include <algorithm> #include <climits> #include <cmath> #include <cstring> #include <iostream> #include <map> #include <queue> #include <string> #include <utility> #include <vector> #define REP(i, n) for (int i = 0; i < n; i++) #define REPR(i, n) for (int i = n; i >= 0; i--) #define FOR(i, m, n) for (int i = m; i < n; i++) #define FORR(i, m, n) for (int i = m; i >= n; i--) #define SORT(v, n) sort(v, v + n); #define VSORT(v) sort(v.begin(), v.end()); #define llong long long #define pb(a) push_back(a) using namespace std; typedef pair<int, int> P; typedef long long int ll; typedef pair<ll, ll> LLP; int dx[4] = {1, 0, 0, -1}; int dy[4] = {0, 1, -1, 0}; #define INF 1000000007 #define MOD 1000000007 #define MAX_N 105 #define MAX_W 100005 ll dp[MAX_N][MAX_W]; // 0:海,1:山,2:宿題,dp[i][j] = iをj日目に行う時の幸福度 ll weight[MAX_N]; ll value[MAX_N]; int main() { ll n, weight_maxi; cin >> n >> weight_maxi; REP(i, n) { cin >> weight[i] >> value[i]; } for (int i = 1; i <= n; i++) { for (int j = 0; j <= weight_maxi; j++) { if (j < weight[i]) { dp[i][j] = dp[i - 1][j]; } else { dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i - 1]] + value[i - 1]); } } } cout << dp[n][weight_maxi] << endl; return 0; }
#include <algorithm> #include <climits> #include <cmath> #include <cstring> #include <iostream> #include <map> #include <queue> #include <string> #include <utility> #include <vector> #define REP(i, n) for (int i = 0; i < n; i++) #define REPR(i, n) for (int i = n; i >= 0; i--) #define FOR(i, m, n) for (int i = m; i < n; i++) #define FORR(i, m, n) for (int i = m; i >= n; i--) #define SORT(v, n) sort(v, v + n); #define VSORT(v) sort(v.begin(), v.end()); #define llong long long #define pb(a) push_back(a) using namespace std; typedef pair<int, int> P; typedef long long int ll; typedef pair<ll, ll> LLP; int dx[4] = {1, 0, 0, -1}; int dy[4] = {0, 1, -1, 0}; #define INF 1000000007 #define MOD 1000000007 #define MAX_N 105 #define MAX_W 100005 ll dp[MAX_N][MAX_W]; // 0:海,1:山,2:宿題,dp[i][j] = iをj日目に行う時の幸福度 ll weight[MAX_N]; ll value[MAX_N]; int main() { ll n, weight_maxi; cin >> n >> weight_maxi; REP(i, n) { cin >> weight[i] >> value[i]; } for (int i = 1; i <= n; i++) { for (int j = 0; j <= weight_maxi; j++) { if (j < weight[i - 1]) { dp[i][j] = dp[i - 1][j]; } else { dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i - 1]] + value[i - 1]); } } } cout << dp[n][weight_maxi] << endl; return 0; }
replace
43
44
43
44
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; // M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A // #define int long long #define endl '\n' #define ff first #define ss second #define mp make_pair #define pb push_back #define vii vector<int> #define pii pair<int, int> #define vpii vector<pii> #define all(v) v.begin(), v.end() #define loop(i, s, e) for (int i = s; i < e; i++) #define rloop(i, e, s) for (int i = e; i >= s; i--) #define mset(a, f) memset(a, f, sizeof(a)) #define M 1000000007 #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #define prvec(v) \ loop(i_u_j, 0, v.size()) cout << v[i_u_j] << " "; \ cout << endl; #define prarr(arr, n) \ loop(i_u_j, 0, n) cout << arr[i_u_j] << " "; \ cout << endl; #define bug(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cout << name << " : " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } // M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A // const int N = 107; int n; int wt[N], val[N], dp[N][100007]; int getval(int it, int mxw) { if (dp[it][mxw] != -1) return dp[it][mxw]; if (mxw == 0 || it == n) return dp[it][mxw] = 0; int a = 0, b = 0; if (wt[it] <= mxw) { a = val[it] + getval(it + 1, mxw - wt[it]); } b = getval(it + 1, mxw); return dp[it][mxw] = max(a, b); } void solve() { int mxw; cin >> n >> mxw; loop(i, 0, n) { cin >> wt[i] >> val[i]; } mset(dp, -1); cout << getval(0, mxw); } int32_t main() { fastio; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int t = 1; // cin>>t; while (t--) solve(); } // M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //
#include <bits/stdc++.h> using namespace std; // M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A // #define int long long #define endl '\n' #define ff first #define ss second #define mp make_pair #define pb push_back #define vii vector<int> #define pii pair<int, int> #define vpii vector<pii> #define all(v) v.begin(), v.end() #define loop(i, s, e) for (int i = s; i < e; i++) #define rloop(i, e, s) for (int i = e; i >= s; i--) #define mset(a, f) memset(a, f, sizeof(a)) #define M 1000000007 #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #define prvec(v) \ loop(i_u_j, 0, v.size()) cout << v[i_u_j] << " "; \ cout << endl; #define prarr(arr, n) \ loop(i_u_j, 0, n) cout << arr[i_u_j] << " "; \ cout << endl; #define bug(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cout << name << " : " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } // M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A // const int N = 107; int n; int wt[N], val[N], dp[N][100007]; int getval(int it, int mxw) { if (dp[it][mxw] != -1) return dp[it][mxw]; if (mxw == 0 || it == n) return dp[it][mxw] = 0; int a = 0, b = 0; if (wt[it] <= mxw) { a = val[it] + getval(it + 1, mxw - wt[it]); } b = getval(it + 1, mxw); return dp[it][mxw] = max(a, b); } void solve() { int mxw; cin >> n >> mxw; loop(i, 0, n) { cin >> wt[i] >> val[i]; } mset(dp, -1); cout << getval(0, mxw); } int32_t main() { fastio; // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif int t = 1; // cin>>t; while (t--) solve(); } // M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //
replace
71
75
71
75
-11
p03163
C++
Time Limit Exceeded
/** * author: Shubham Srivastava * created: 26 January 2020 00:24:22 **/ #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define MOD 1000000007 #define INF 0x7fffffff #define sorti(v) sort(v.begin(), v.end()) #define sortd(v) sort(v.rbegin(), v.rend()) typedef pair<ll, ll> pll; typedef pair<ll, ll> pii; typedef vector<ll> vi; typedef vector<pii> vii; ll parent[2000005]; ll helper(vector<ll> weight, vector<ll> value, ll w, ll i, ll n, vector<vector<ll>> &dp) { if (w <= 0 || i == n) return 0; if (dp[i][w] != -1) return dp[i][w]; if (weight[i] > w) return dp[i][w] = helper(weight, value, w, i + 1, n, dp); return dp[i][w] = max(value[i] + helper(weight, value, w - weight[i], i + 1, n, dp), helper(weight, value, w, i + 1, n, dp)); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n, w; cin >> n >> w; vector<vector<ll>> dp(n + 1, vector<ll>(w + 1, -1)); vector<ll> weight(n), value(n); for (ll i = 0; i < n; i++) { cin >> weight[i] >> value[i]; } cout << helper(weight, value, w, 0, n, dp); return 0; }
/** * author: Shubham Srivastava * created: 26 January 2020 00:24:22 **/ #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define MOD 1000000007 #define INF 0x7fffffff #define sorti(v) sort(v.begin(), v.end()) #define sortd(v) sort(v.rbegin(), v.rend()) typedef pair<ll, ll> pll; typedef pair<ll, ll> pii; typedef vector<ll> vi; typedef vector<pii> vii; ll parent[2000005]; ll helper(vector<ll> &weight, vector<ll> &value, ll w, ll i, ll n, vector<vector<ll>> &dp) { if (w <= 0 || i == n) return 0; if (dp[i][w] != -1) return dp[i][w]; if (weight[i] > w) return dp[i][w] = helper(weight, value, w, i + 1, n, dp); return dp[i][w] = max(value[i] + helper(weight, value, w - weight[i], i + 1, n, dp), helper(weight, value, w, i + 1, n, dp)); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n, w; cin >> n >> w; vector<vector<ll>> dp(n + 1, vector<ll>(w + 1, -1)); vector<ll> weight(n), value(n); for (ll i = 0; i < n; i++) { cin >> weight[i] >> value[i]; } cout << helper(weight, value, w, 0, n, dp); return 0; }
replace
18
19
18
19
TLE
p03163
C++
Runtime Error
#include <algorithm> #include <bitset> #include <ciso646> #include <cmath> #include <complex> #include <cstdio> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef unsigned int ui; const ll mod = 1000000007; typedef long double ld; const ll INF = 1e+14; typedef pair<int, int> P; #define stop \ char nyaa; \ cin >> nyaa; #define rep(i, n) for (int i = 0; i < n; i++) #define per(i, n) for (int i = n - 1; i >= 0; i--) #define Rep(i, sta, n) for (int i = sta; i < n; i++) #define rep1(i, n) for (int i = 1; i <= n; i++) #define per1(i, n) for (int i = n; i >= 1; i--) #define Rep1(i, sta, n) for (int i = sta; i <= n; i++) typedef complex<ld> Point; const ld eps = 1e-8; const ld pi = acos(-1.0); typedef pair<ld, ld> LDP; typedef pair<ll, ll> LP; #define fr first #define sc second #define all(c) c.begin(), c.end() #define pb push_back void Yes() { cout << "Yes" << endl; exit(0); } void No() { cout << "No" << endl; exit(0); } 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; } int main() { ios::sync_with_stdio(false); cin.tie(0); int N, W; cin >> N >> W; ll w[110], v[110]; rep(i, N) cin >> w[i] >> v[i]; ll dp[110][100100]; // i-1番目までの品を選んで重さがj以下の時の価値の最大 rep1(i, N) { rep(j, W + 1) { chmax(dp[i][j], dp[i - 1][j]); chmax(dp[i][j], dp[i - 1][j - w[i - 1]] + v[i - 1]); } } cout << dp[N][W] << endl; return 0; }
#include <algorithm> #include <bitset> #include <ciso646> #include <cmath> #include <complex> #include <cstdio> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef unsigned int ui; const ll mod = 1000000007; typedef long double ld; const ll INF = 1e+14; typedef pair<int, int> P; #define stop \ char nyaa; \ cin >> nyaa; #define rep(i, n) for (int i = 0; i < n; i++) #define per(i, n) for (int i = n - 1; i >= 0; i--) #define Rep(i, sta, n) for (int i = sta; i < n; i++) #define rep1(i, n) for (int i = 1; i <= n; i++) #define per1(i, n) for (int i = n; i >= 1; i--) #define Rep1(i, sta, n) for (int i = sta; i <= n; i++) typedef complex<ld> Point; const ld eps = 1e-8; const ld pi = acos(-1.0); typedef pair<ld, ld> LDP; typedef pair<ll, ll> LP; #define fr first #define sc second #define all(c) c.begin(), c.end() #define pb push_back void Yes() { cout << "Yes" << endl; exit(0); } void No() { cout << "No" << endl; exit(0); } 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; } int main() { ios::sync_with_stdio(false); cin.tie(0); int N, W; cin >> N >> W; ll w[110], v[110]; rep(i, N) cin >> w[i] >> v[i]; ll dp[110][100100]; // i-1番目までの品を選んで重さがj以下の時の価値の最大 rep1(i, N) { rep(j, W + 1) { chmax(dp[i][j], dp[i - 1][j]); if (j - w[i - 1] >= 0) chmax(dp[i][j], dp[i - 1][j - w[i - 1]] + v[i - 1]); } } cout << dp[N][W] << endl; return 0; }
replace
79
80
79
81
-11
p03163
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int n, w; cin >> n >> w; long long dp[101][101]; for (int i = 0; i <= n; i++) { dp[i][0] = 0; } for (int j = 0; j <= w; j++) { dp[0][j] = 0; } for (int i = 1; i <= n; i++) { int wi; long long vi; cin >> wi >> vi; for (int j = 1; j <= w; j++) { if (j < wi) { dp[i][j] = dp[i - 1][j]; continue; } dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - wi] + vi); } } cout << dp[n][w] << endl; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int n, w; cin >> n >> w; long long dp[101][100001]; for (int i = 0; i <= n; i++) { dp[i][0] = 0; } for (int j = 0; j <= w; j++) { dp[0][j] = 0; } for (int i = 1; i <= n; i++) { int wi; long long vi; cin >> wi >> vi; for (int j = 1; j <= w; j++) { if (j < wi) { dp[i][j] = dp[i - 1][j]; continue; } dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - wi] + vi); } } cout << dp[n][w] << endl; }
replace
10
11
10
11
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #pragma GCC target("avx2") #pragma GCC optimization("O3") #pragma GCC optimization("unroll-loops") #define lld long long int //-4%3=-1 lld n, w; lld wt[101], val[101], dp[101][101]; lld fun(lld i, lld w) { if (i <= 0 || w <= 0) return 0; if (dp[i][w] == -1) { if (wt[i] <= w) return dp[i][w] = max(val[i] + fun(i - 1, w - wt[i]), fun(i - 1, w)); else return dp[i][w] = fun(i - 1, w); } else return dp[i][w]; } int main() { // your code goes here ios_base::sync_with_stdio(false); cin.tie(NULL); lld t; // cin>>t; t = 1; while (t--) { cin >> n >> w; for (int i = 1; i <= n; i++) cin >> wt[i] >> val[i]; memset(dp, -1, sizeof(dp)); cout << fun(n, w); } }
#include <bits/stdc++.h> using namespace std; #pragma GCC target("avx2") #pragma GCC optimization("O3") #pragma GCC optimization("unroll-loops") #define lld long long int //-4%3=-1 lld n, w; lld wt[101], val[101], dp[101][100001]; lld fun(lld i, lld w) { if (i <= 0 || w <= 0) return 0; if (dp[i][w] == -1) { if (wt[i] <= w) return dp[i][w] = max(val[i] + fun(i - 1, w - wt[i]), fun(i - 1, w)); else return dp[i][w] = fun(i - 1, w); } else return dp[i][w]; } int main() { // your code goes here ios_base::sync_with_stdio(false); cin.tie(NULL); lld t; // cin>>t; t = 1; while (t--) { cin >> n >> w; for (int i = 1; i <= n; i++) cin >> wt[i] >> val[i]; memset(dp, -1, sizeof(dp)); cout << fun(n, w); } }
replace
9
10
9
10
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> #define maxn 100 #define maxk 10001 using namespace std; typedef long long LL; LL ans; int n, k; int w[maxn]; int v[maxn]; LL dp[maxk]; int main() { cin >> n >> k; for (int i = 0; i < n; i++) cin >> w[i] >> v[i]; memset(dp, -1, sizeof(dp)); dp[0] = 0; for (int i = 0; i < n; i++) for (int j = k - w[i]; j >= 0; j--) if (dp[j] != -1) if (dp[j + w[i]] == -1 || dp[j] + v[i] > dp[j + w[i]]) dp[j + w[i]] = dp[j] + v[i]; for (int i = 0; i <= k; i++) ans = max(ans, dp[i]); cout << ans << endl; }
#include <bits/stdc++.h> #define maxn 100 #define maxk 100001 using namespace std; typedef long long LL; LL ans; int n, k; int w[maxn]; int v[maxn]; LL dp[maxk]; int main() { cin >> n >> k; for (int i = 0; i < n; i++) cin >> w[i] >> v[i]; memset(dp, -1, sizeof(dp)); dp[0] = 0; for (int i = 0; i < n; i++) for (int j = k - w[i]; j >= 0; j--) if (dp[j] != -1) if (dp[j + w[i]] == -1 || dp[j] + v[i] > dp[j + w[i]]) dp[j + w[i]] = dp[j] + v[i]; for (int i = 0; i <= k; i++) ans = max(ans, dp[i]); cout << ans << endl; }
replace
2
3
2
3
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; /* * GUST KEEP GOING */ #define endl '\n' #define ll long long #define all(v) ((v).begin()), ((v).end()) #define sz(v) ((int)((v).size())) #define clr(v, d) memset(v, d, sizeof(v)) #define rep(i, v) for (int i = 0; i < sz(v); ++i) #define lp(i, n) for (int i = 0; i < (int)(n); ++i) #define lpi(i, j, n) for (int i = (j); i < (int)(n); ++i) #define lpd(i, j, n) for (int i = (j); i >= (int)(n); --i) #define vi vector<int> #define vl vector<ll> #define ii pair<ll, ll> #define vii vector<ii> #define PI double PPPPPP = 3.14159265358979323846; #define bit(mask, i) ((mask >> i) & 1) vector<string> vec_splitter(string s) { s += ','; vector<string> res; while (!s.empty()) { res.push_back(s.substr(0, s.find(','))); s = s.substr(s.find(',') + 1); } return res; } void debug_out(vector<string> __attribute__((unused)) args, __attribute__((unused)) int idx, __attribute__((unused)) int LINE_NUM) { cerr << endl; } template <typename Head, typename... Tail> void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) { if (idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") "; stringstream ss; ss << H; cerr << args[idx] << " = " << ss.str(); debug_out(args, idx + 1, LINE_NUM, T...); } #define debug(...) \ debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__) void run() { ios_base::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); } int n, w; ll dp[101][100001]; vector<pair<int, int>> v; ll sol(int i, int w) { if (w < 0) { return -1e12; } if (i == n || w == 0) return 0; ll &ret = dp[i][w]; if (~ret) return ret; return ret = max(sol(i + 1, w), v[i].second + sol(i + 1, w - v[i].first)); } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); /// freopen("output.txt", "w", stdout); #endif run(); cin >> n >> w; v.resize(n); for (auto &i : v) { cin >> i.first >> i.second; } memset(dp, -1, sizeof(dp)); cout << sol(0, w) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; /* * GUST KEEP GOING */ #define endl '\n' #define ll long long #define all(v) ((v).begin()), ((v).end()) #define sz(v) ((int)((v).size())) #define clr(v, d) memset(v, d, sizeof(v)) #define rep(i, v) for (int i = 0; i < sz(v); ++i) #define lp(i, n) for (int i = 0; i < (int)(n); ++i) #define lpi(i, j, n) for (int i = (j); i < (int)(n); ++i) #define lpd(i, j, n) for (int i = (j); i >= (int)(n); --i) #define vi vector<int> #define vl vector<ll> #define ii pair<ll, ll> #define vii vector<ii> #define PI double PPPPPP = 3.14159265358979323846; #define bit(mask, i) ((mask >> i) & 1) vector<string> vec_splitter(string s) { s += ','; vector<string> res; while (!s.empty()) { res.push_back(s.substr(0, s.find(','))); s = s.substr(s.find(',') + 1); } return res; } void debug_out(vector<string> __attribute__((unused)) args, __attribute__((unused)) int idx, __attribute__((unused)) int LINE_NUM) { cerr << endl; } template <typename Head, typename... Tail> void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) { if (idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") "; stringstream ss; ss << H; cerr << args[idx] << " = " << ss.str(); debug_out(args, idx + 1, LINE_NUM, T...); } #define debug(...) \ debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__) void run() { ios_base::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); } int n, w; ll dp[101][100001]; vector<pair<int, int>> v; ll sol(int i, int w) { if (w < 0) { return -1e12; } if (i == n || w == 0) return 0; ll &ret = dp[i][w]; if (~ret) return ret; return ret = max(sol(i + 1, w), v[i].second + sol(i + 1, w - v[i].first)); } int main() { /*#ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); ///freopen("output.txt", "w", stdout); #endif*/ run(); cin >> n >> w; v.resize(n); for (auto &i : v) { cin >> i.first >> i.second; } memset(dp, -1, sizeof(dp)); cout << sol(0, w) << endl; return 0; }
replace
67
71
67
71
0
p03163
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <vector> #define ll long long #define rep(i, a, b) for (int i = a; i <= b; i++) void read(int &x) { x = 0; int flag = 1; char ch = ' '; while (ch < '0' || ch > '9') { if (ch == '-') flag = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); x *= flag; } void read(ll &x) { x = 0; int flag = 1; char ch = ' '; while (ch < '0' || ch > '9') { if (ch == '-') flag = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); x *= flag; } using namespace std; const int maxn = 110; int n, w, weight[maxn], value[maxn]; ll dp[10010]; int main() { read(n), read(w); rep(i, 1, n) read(weight[i]), read(value[i]); rep(i, 1, n) for (int j = w; j >= weight[i]; j--) dp[j] = max(dp[j], dp[j - weight[i]] + value[i]); printf("%lld\n", dp[w]); return 0; }
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <vector> #define ll long long #define rep(i, a, b) for (int i = a; i <= b; i++) void read(int &x) { x = 0; int flag = 1; char ch = ' '; while (ch < '0' || ch > '9') { if (ch == '-') flag = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); x *= flag; } void read(ll &x) { x = 0; int flag = 1; char ch = ' '; while (ch < '0' || ch > '9') { if (ch == '-') flag = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); x *= flag; } using namespace std; const int maxn = 110; int n, w, weight[maxn], value[maxn]; ll dp[100010]; int main() { read(n), read(w); rep(i, 1, n) read(weight[i]), read(value[i]); rep(i, 1, n) for (int j = w; j >= weight[i]; j--) dp[j] = max(dp[j], dp[j - weight[i]] + value[i]); printf("%lld\n", dp[w]); return 0; }
replace
36
37
36
37
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> //{ START using namespace std; #define int int64_t #define rep(i, a, n) for (int i = (a); i < (n); ++i) #define reps(i, a, n) for (int i = (a); i > (n); --i) #define arep(i, x) for (auto &&i : (x)) #define irep(i, x) for (auto i = (x).begin(); i != (x).end(); ++i) #define rirep(i, x) for (auto i = (x).rbegin(); i != (x).rend(); ++i) // 降順はgreater<T>() #define all(x) (x).begin(), (x).end() #define rv(s) reverse((s).begin(), (s).end()) // gcd lcmはそのままok #define gcd(a, b) __gcd(a, b) #define bits(n) (1LL << (n)) #define pcnt(x) __builtin_popcountll(x) // 配列内等要素削除 #define Unique(x) (x).erase(unique((x).begin(), (x).end()), (x).end()) #define Fixed(n) fi > xed << setprecision(n) // 総和 #define sowa(n) (((n) * ((n) + 1)) / 2) #define cauto const auto & using P = pair<int, int>; using Graph = vector<vector<P>>; template <class T> // 昇順 using min_heap = priority_queue<T, vector<T>, greater<T>>; template <class T> // 降順 using max_heap = priority_queue<T>; template <class A, class B> using umap = unordered_map<A, B>; template <class A> using uset = unordered_set<A>; template <typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val) { // 多次元初期化 std::fill((T *)array, (T *)(array + N), val); } template <class A, class B> bool chmax(A &a, const B &b) { // 最大値更新 返り値はbool if (a < b) { a = b; return 1; } return 0; } template <class A, class B> bool chmin(A &a, const B &b) { // 最小値更新 返り値はbool if (b < a) { a = b; return 1; } return 0; } int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}; int dy[] = {0, 1, 0, -1, -1, 1, 1, -1}; constexpr int INF = 0x3f3f3f3f; constexpr int LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr int mod1 = 1e9 + 7; constexpr int mod2 = 998244353; //} END signed main(void) { cin.tie(nullptr); ios_base::sync_with_stdio(false); int n, W; cin >> n >> W; vector<int> v(110), w(110); vector<vector<int>> dp(110, vector<int>(1100, 0)); rep(i, 0, n) cin >> w[i] >> v[i]; rep(i, 0, n) { rep(j, 0, W + 1) { if (j >= w[i]) dp[i + 1][j] = max(dp[i][j - w[i]] + v[i], dp[i][j]); else dp[i + 1][j] = dp[i][j]; // cout << dp[i+1][j] << ' '; } // cout << '\n'; } cout << dp[n][W] << '\n'; return 0; }
#include <bits/stdc++.h> //{ START using namespace std; #define int int64_t #define rep(i, a, n) for (int i = (a); i < (n); ++i) #define reps(i, a, n) for (int i = (a); i > (n); --i) #define arep(i, x) for (auto &&i : (x)) #define irep(i, x) for (auto i = (x).begin(); i != (x).end(); ++i) #define rirep(i, x) for (auto i = (x).rbegin(); i != (x).rend(); ++i) // 降順はgreater<T>() #define all(x) (x).begin(), (x).end() #define rv(s) reverse((s).begin(), (s).end()) // gcd lcmはそのままok #define gcd(a, b) __gcd(a, b) #define bits(n) (1LL << (n)) #define pcnt(x) __builtin_popcountll(x) // 配列内等要素削除 #define Unique(x) (x).erase(unique((x).begin(), (x).end()), (x).end()) #define Fixed(n) fi > xed << setprecision(n) // 総和 #define sowa(n) (((n) * ((n) + 1)) / 2) #define cauto const auto & using P = pair<int, int>; using Graph = vector<vector<P>>; template <class T> // 昇順 using min_heap = priority_queue<T, vector<T>, greater<T>>; template <class T> // 降順 using max_heap = priority_queue<T>; template <class A, class B> using umap = unordered_map<A, B>; template <class A> using uset = unordered_set<A>; template <typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val) { // 多次元初期化 std::fill((T *)array, (T *)(array + N), val); } template <class A, class B> bool chmax(A &a, const B &b) { // 最大値更新 返り値はbool if (a < b) { a = b; return 1; } return 0; } template <class A, class B> bool chmin(A &a, const B &b) { // 最小値更新 返り値はbool if (b < a) { a = b; return 1; } return 0; } int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}; int dy[] = {0, 1, 0, -1, -1, 1, 1, -1}; constexpr int INF = 0x3f3f3f3f; constexpr int LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr int mod1 = 1e9 + 7; constexpr int mod2 = 998244353; //} END signed main(void) { cin.tie(nullptr); ios_base::sync_with_stdio(false); int n, W; cin >> n >> W; vector<int> v(110), w(110); vector<vector<int>> dp(110, vector<int>(100010, 0)); rep(i, 0, n) cin >> w[i] >> v[i]; rep(i, 0, n) { rep(j, 0, W + 1) { if (j >= w[i]) dp[i + 1][j] = max(dp[i][j - w[i]] + v[i], dp[i][j]); else dp[i + 1][j] = dp[i][j]; // cout << dp[i+1][j] << ' '; } // cout << '\n'; } cout << dp[n][W] << '\n'; return 0; }
replace
66
67
66
67
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> long long t[100][100000]; using namespace std; void Knapsack(vector<long long> wt, vector<long long> val, long long w, long long n) { if (w == 0 || n == 0) { return; } for (int i = 1; i < n + 1; i++) { for (int j = 1; j < w + 1; j++) { if (wt[i - 1] <= j) { t[i][j] = max(val[i - 1] + t[i - 1][j - wt[i - 1]], t[i - 1][j]); } else { t[i][j] = t[i - 1][j]; } } } } int main() { int n, w; cin >> n; cin >> w; // memset(t, -1, sizeof(t)); vector<long long> wt(n); vector<long long> val(n); for (int i = 0; i < n; i++) { cin >> wt[i] >> val[i]; } for (int i = 0; i < n + 1; i++) { for (int j = 0; j < w + 1; j++) { if (i == 0 || j == 0) { t[i][j] = 0; } } } Knapsack(wt, val, w, n); cout << t[n][w]; }
#include <bits/stdc++.h> long long t[101][1000001]; using namespace std; void Knapsack(vector<long long> wt, vector<long long> val, long long w, long long n) { if (w == 0 || n == 0) { return; } for (int i = 1; i < n + 1; i++) { for (int j = 1; j < w + 1; j++) { if (wt[i - 1] <= j) { t[i][j] = max(val[i - 1] + t[i - 1][j - wt[i - 1]], t[i - 1][j]); } else { t[i][j] = t[i - 1][j]; } } } } int main() { int n, w; cin >> n; cin >> w; // memset(t, -1, sizeof(t)); vector<long long> wt(n); vector<long long> val(n); for (int i = 0; i < n; i++) { cin >> wt[i] >> val[i]; } for (int i = 0; i < n + 1; i++) { for (int j = 0; j < w + 1; j++) { if (i == 0 || j == 0) { t[i][j] = 0; } } } Knapsack(wt, val, w, n); cout << t[n][w]; }
replace
2
3
2
3
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define sz(x) (int)(x).size() #define pb push_back #define all(x) (x).begin(), (x).end() #define ll long long const int N = 3e5 + 5, MOD = 1e9 + 7; int n, s; int main() { #ifndef ONLINE_JUDGE freopen("tst.txt", "r", stdin); #endif scanf("%d %d", &n, &s); std::vector<vector<ll>> dp(n, vector<ll>(s + 1)); for (int i = 0; i < n; i++) { int w, x; scanf("%d %d", &w, &x); if (i) for (int j = 0; j <= s; j++) { if (j - w >= 0) dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w] + x); else dp[i][j] = dp[i - 1][j]; } else for (int j = w; j <= s; j++) dp[i][j] = x; } printf("%lld\n", dp[n - 1][s]); }
#include <bits/stdc++.h> using namespace std; #define sz(x) (int)(x).size() #define pb push_back #define all(x) (x).begin(), (x).end() #define ll long long const int N = 3e5 + 5, MOD = 1e9 + 7; int n, s; int main() { scanf("%d %d", &n, &s); std::vector<vector<ll>> dp(n, vector<ll>(s + 1)); for (int i = 0; i < n; i++) { int w, x; scanf("%d %d", &w, &x); if (i) for (int j = 0; j <= s; j++) { if (j - w >= 0) dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w] + x); else dp[i][j] = dp[i - 1][j]; } else for (int j = w; j <= s; j++) dp[i][j] = x; } printf("%lld\n", dp[n - 1][s]); }
delete
11
14
11
11
-11
p03163
C++
Time Limit Exceeded
#define _CRT_SECURE_NO_WARNINGS #include <bits/stdc++.h> #define rep(i, a, n) for (int i = a; i < n; i++) #define per(i, a, n) for (int i = n - 1; i >= a; i--) #define all(s) s.begin(), s.end() #define sz(s) (int)s.size() #define clr(v, d) memset(v, d, sizeof(v)) #define fi first #define se second #define pp push_back #define pb pop_back #define pi 3.141592654 #define MOD 1000000007 #define cons1 1e12 #define cons2 1e6 using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int, ll> pa; typedef unsigned long long int ull; typedef double long dl; const int N = 1e5 + 9, oo = -1e9; int n, v[N][2]; ll ans[N]; ll sol(int i, int w) { if (i == n) return 0; ll x = sol(i + 1, w); if (w >= v[i][0]) x = max(x, sol(i + 1, w - v[i][0]) + v[i][1]); return x; } void solve() { int w; cin >> n >> w; rep(i, 0, n) cin >> v[i][0] >> v[i][1]; cout << sol(0, w) << endl; } int main() { ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int t = 1; // cin >> t; while (t--) solve(); #ifndef ONLINE_JUDGE -system("pause"); #endif }
#define _CRT_SECURE_NO_WARNINGS #include <bits/stdc++.h> #define rep(i, a, n) for (int i = a; i < n; i++) #define per(i, a, n) for (int i = n - 1; i >= a; i--) #define all(s) s.begin(), s.end() #define sz(s) (int)s.size() #define clr(v, d) memset(v, d, sizeof(v)) #define fi first #define se second #define pp push_back #define pb pop_back #define pi 3.141592654 #define MOD 1000000007 #define cons1 1e12 #define cons2 1e6 using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int, ll> pa; typedef unsigned long long int ull; typedef double long dl; const int N = 1e5 + 9, oo = -1e9; ll dp[109][N], n, v[109][109]; ll sol(int i, int k) { if (n - i && !dp[i][k]) { if (k >= v[i][0]) dp[i][k] = v[i][1] + sol(i + 1, k - v[i][0]); dp[i][k] = max(dp[i][k], sol(i + 1, k)); } return dp[i][k]; } void solve() { int w; cin >> n >> w; rep(i, 0, n) cin >> v[i][0] >> v[i][1]; cout << sol(0, w) << endl; } int main() { ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int t = 1; // cin >> t; while (t--) solve(); #ifndef ONLINE_JUDGE -system("pause"); #endif }
replace
22
31
22
31
TLE
p03163
C++
Time Limit Exceeded
// You either die a hero, or you live long enough to see yourself become the // villain. /** * author: Blind_is_love * created: 12.10.2019, 15:49 **/ #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; typedef long long ll; template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { for (T i : v) os << i << ' '; return os; } template <class T> ostream &operator<<(ostream &os, const set<T> &v) { for (T i : v) os << i << ' '; return os; } template <class T, class S> ostream &operator<<(ostream &os, const pair<T, S> &v) { os << v.first << ' ' << v.second; return os; } template <class T, class S> ostream &operator<<(ostream &os, const map<T, S> &v) { for (auto i : v) os << '(' << i.first << "=>" << i.second << ')' << ' '; return os; } template <class T, class S> ostream &operator<<(ostream &os, const unordered_map<T, S> &v) { for (auto i : v) os << '(' << i.first << "=>" << i.second << ')' << ' '; return os; } #ifndef ONLINE_JUDGE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <class Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << endl; } template <class Arg1, class... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *sep = strchr(names + 1, ','); cerr.write(names, sep - names) << " : " << arg1 << " "; __f(sep + 1, args...); } #else #define trace(...) 0 #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #pragma GCC target( \ "avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define _CRT_SECURE_NO_WARNINGS #endif // ifndef ONLINE_JUDGE #define endl "\n" #define vll vector<int> #define max_heap priority_queue<int> #define min_heap priority_queue<int, vector<int>, greater<int>> #define pb push_back #define all(container) container.begin(), container.end() #define pll pair<ll, ll> #define pii pair<int, int> #define printArr(name, from, to) \ for (int x = from; x < to; x++) \ cout << name[x] << " "; #define fi first #define se second #define mp make_pair #define prime1 304933 #define prime2 15486277 #define mod2 179424691 #define int ll typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; const ll MOD = 1000000007; const ll MAX = 1e5 + 100; const double PI = 3.1415926; const ll INF = 1e18; const double EPS = 1e-5; int n, W; pii arr[101]; int dp[101][MAX]; bool seen[101][MAX]; int solve(int i, int rem) { if (rem < 0) return -1e18; if (i == -1) return 0; if (seen[i][rem]) return dp[i][rem]; int f1 = solve(i - 1, rem); int f2 = solve(i - 1, rem - arr[i].fi); if (f2 >= 0) f2 += arr[i].se; return dp[i][rem] = max(f1, f2); } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); clock_t t1, t2; t1 = clock(); cin >> n >> W; for (int i = 0; i < n; ++i) { cin >> arr[i].fi >> arr[i].se; } cout << solve(n - 1, W) << endl; t2 = clock(); cerr << "time taken: " << (t2 - t1) / (long double)CLOCKS_PER_SEC << endl; return 0; }
// You either die a hero, or you live long enough to see yourself become the // villain. /** * author: Blind_is_love * created: 12.10.2019, 15:49 **/ #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; typedef long long ll; template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { for (T i : v) os << i << ' '; return os; } template <class T> ostream &operator<<(ostream &os, const set<T> &v) { for (T i : v) os << i << ' '; return os; } template <class T, class S> ostream &operator<<(ostream &os, const pair<T, S> &v) { os << v.first << ' ' << v.second; return os; } template <class T, class S> ostream &operator<<(ostream &os, const map<T, S> &v) { for (auto i : v) os << '(' << i.first << "=>" << i.second << ')' << ' '; return os; } template <class T, class S> ostream &operator<<(ostream &os, const unordered_map<T, S> &v) { for (auto i : v) os << '(' << i.first << "=>" << i.second << ')' << ' '; return os; } #ifndef ONLINE_JUDGE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <class Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << endl; } template <class Arg1, class... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *sep = strchr(names + 1, ','); cerr.write(names, sep - names) << " : " << arg1 << " "; __f(sep + 1, args...); } #else #define trace(...) 0 #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #pragma GCC target( \ "avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define _CRT_SECURE_NO_WARNINGS #endif // ifndef ONLINE_JUDGE #define endl "\n" #define vll vector<int> #define max_heap priority_queue<int> #define min_heap priority_queue<int, vector<int>, greater<int>> #define pb push_back #define all(container) container.begin(), container.end() #define pll pair<ll, ll> #define pii pair<int, int> #define printArr(name, from, to) \ for (int x = from; x < to; x++) \ cout << name[x] << " "; #define fi first #define se second #define mp make_pair #define prime1 304933 #define prime2 15486277 #define mod2 179424691 #define int ll typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; const ll MOD = 1000000007; const ll MAX = 1e5 + 100; const double PI = 3.1415926; const ll INF = 1e18; const double EPS = 1e-5; int n, W; pii arr[101]; int dp[101][MAX]; bool seen[101][MAX]; int solve(int i, int rem) { if (rem < 0) return -1e18; if (i == -1) return 0; if (seen[i][rem]) return dp[i][rem]; int f1 = solve(i - 1, rem); int f2 = solve(i - 1, rem - arr[i].fi); if (f2 >= 0) f2 += arr[i].se; seen[i][rem] = true; return dp[i][rem] = max(f1, f2); } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); clock_t t1, t2; t1 = clock(); cin >> n >> W; for (int i = 0; i < n; ++i) { cin >> arr[i].fi >> arr[i].se; } cout << solve(n - 1, W) << endl; t2 = clock(); cerr << "time taken: " << (t2 - t1) / (long double)CLOCKS_PER_SEC << endl; return 0; }
insert
111
111
111
112
TLE
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; ifstream fin("jocul.in"); ofstream fout("jocul.out"); long long n, g; long long v[105], w[105]; long long dp[100005]; int main() { long long i, sum = 0, answer = -1; cin >> n >> g; for (i = 1; i <= n; i++) cin >> w[i] >> v[i]; dp[0] = 0; for (i = 1; i <= n; i++) { sum += w[i]; for (int j = sum; j >= w[i]; j--) if (dp[j - w[i]] != -1) dp[j] = max(dp[j], v[i] + dp[j - w[i]]); } for (i = 0; i <= sum; i++) { if (dp[i] > answer && i <= g) answer = dp[i]; } cout << answer; return 0; }
#include <bits/stdc++.h> using namespace std; ifstream fin("jocul.in"); ofstream fout("jocul.out"); long long n, g; long long v[105], w[105]; long long dp[10000005]; int main() { long long i, sum = 0, answer = -1; cin >> n >> g; for (i = 1; i <= n; i++) cin >> w[i] >> v[i]; dp[0] = 0; for (i = 1; i <= n; i++) { sum += w[i]; for (int j = sum; j >= w[i]; j--) if (dp[j - w[i]] != -1) dp[j] = max(dp[j], v[i] + dp[j - w[i]]); } for (i = 0; i <= sum; i++) { if (dp[i] > answer && i <= g) answer = dp[i]; } cout << answer; return 0; }
replace
9
10
9
10
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 100; const int CAP = 1e5 + 10; int n, cap; int v[N], w[N]; ll dp[N][CAP]; ll run(int idx, int cur) { if (dp[idx][cur] != -1) return dp[idx][cur]; ll res = 0; if (idx == n or cur == 0) res = 0; else if (w[idx] > cur) res = run(idx + 1, cur); else res = max(run(idx + 1, cur), run(idx + 1, cur - w[idx]) + v[idx]); return dp[idx][cur] = res; } ll run() { for (int i = 1; i <= n; ++i) { for (int k = 1; k <= cap; ++k) { if (w[i - 1] > k) dp[i][k] = dp[i - 1][k]; else dp[i][k] = max(dp[i - 1][k], dp[i - 1][k - w[i - 1]] + v[i - 1]); } } return dp[n][cap]; } int main() { scanf("%d %d", &n, &cap); for (int i = 0; i < n; ++i) scanf("%d %d", &w[i], &v[i]); // memset(dp, -1, sizeof dp); ll ans = run(); printf("%lld\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 100 + 10; const int CAP = 1e5 + 10; int n, cap; int v[N], w[N]; ll dp[N][CAP]; ll run(int idx, int cur) { if (dp[idx][cur] != -1) return dp[idx][cur]; ll res = 0; if (idx == n or cur == 0) res = 0; else if (w[idx] > cur) res = run(idx + 1, cur); else res = max(run(idx + 1, cur), run(idx + 1, cur - w[idx]) + v[idx]); return dp[idx][cur] = res; } ll run() { for (int i = 1; i <= n; ++i) { for (int k = 1; k <= cap; ++k) { if (w[i - 1] > k) dp[i][k] = dp[i - 1][k]; else dp[i][k] = max(dp[i - 1][k], dp[i - 1][k - w[i - 1]] + v[i - 1]); } } return dp[n][cap]; } int main() { scanf("%d %d", &n, &cap); for (int i = 0; i < n; ++i) scanf("%d %d", &w[i], &v[i]); // memset(dp, -1, sizeof dp); ll ans = run(); printf("%lld\n", ans); return 0; }
replace
3
4
3
4
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { long long wt[110], val[110], v[110][110]; int n; long long w; cin >> n >> w; for (int i = 1; i <= n; i++) cin >> wt[i] >> val[i]; for (int i = 0; i <= n; i++) for (int j = 0; j <= w; j++) v[i][j] = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= w; j++) { if (wt[i] > j) v[i][j] = v[i - 1][j]; else v[i][j] = max(v[i - 1][j], val[i] + v[i - 1][j - wt[i]]); } } cout << v[n][w]; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long wt[110], val[110], v[110][110000]; int n; long long w; cin >> n >> w; for (int i = 1; i <= n; i++) cin >> wt[i] >> val[i]; for (int i = 0; i <= n; i++) for (int j = 0; j <= w; j++) v[i][j] = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= w; j++) { if (wt[i] > j) v[i][j] = v[i - 1][j]; else v[i][j] = max(v[i - 1][j], val[i] + v[i - 1][j - wt[i]]); } } cout << v[n][w]; return 0; }
replace
3
4
3
4
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define pb push_back #define eps 1e-9 #define PI acos(-1.0) typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> ii; typedef pair<int, ii> iii; typedef vector<ii> vii; const int INF = 1e9 + 5; bool prime(ll x) { if (x == 2) return true; for (int i = 2; i * i <= x; i++) if (x % i == 0) return false; return true; } void self_max(ll &a, ll b) { a = max(a, b); } bool cmp(int a, int b) { return a > b; } int a[int(1e5 + 5)]; int main() { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); ios_base::sync_with_stdio(0); cin.tie(0); int n, w; cin >> n >> w; std::vector<ll> dp(w + 5); for (int item = 0; item < n; ++item) { int wt, val; cin >> wt >> val; for (int wt_alrdy = w - wt; wt_alrdy >= 0; --wt_alrdy) { self_max(dp[wt_alrdy + wt], dp[wt_alrdy] + val); } } ll ans = 0; for (int i = 0; i <= w; ++i) { self_max(ans, dp[i]); } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; #define pb push_back #define eps 1e-9 #define PI acos(-1.0) typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> ii; typedef pair<int, ii> iii; typedef vector<ii> vii; const int INF = 1e9 + 5; bool prime(ll x) { if (x == 2) return true; for (int i = 2; i * i <= x; i++) if (x % i == 0) return false; return true; } void self_max(ll &a, ll b) { a = max(a, b); } bool cmp(int a, int b) { return a > b; } int a[int(1e5 + 5)]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, w; cin >> n >> w; std::vector<ll> dp(w + 5); for (int item = 0; item < n; ++item) { int wt, val; cin >> wt >> val; for (int wt_alrdy = w - wt; wt_alrdy >= 0; --wt_alrdy) { self_max(dp[wt_alrdy + wt], dp[wt_alrdy] + val); } } ll ans = 0; for (int i = 0; i <= w; ++i) { self_max(ans, dp[i]); } cout << ans; return 0; }
delete
27
29
27
27
0
p03163
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> using namespace std; long long a[10 + 100][2], b[10 + 100][110] = {0}; long long sol(int n, int i, int j) { if (i == n - 1) { long long m = -8; for (int t = 0; t < 3; t++) { if (t != j) m = max(m, a[i][t]); } return m; } long long m = -8, s = 0; for (int t = 0; t < 3; t++) { if (t != j) { if (m < a[i][t]) s = t; m = max(m, a[i][t]); } } if (b[i][s] == -1) b[i][s] = m + sol(n, i + 1, s); return b[i][s]; } int main() { int n, w; cin >> n >> w; for (int i = 1; i <= n; i++) for (int j = 0; j < 2; j++) { cin >> a[i][j]; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= w; j++) { if (j - a[i][0] > -1) { b[i][j] = max(a[i][1] + b[i - 1][j - a[i][0]], b[i - 1][j]); } else b[i][j] = b[i - 1][j]; } } cout << b[n][w] << endl; return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; long long a[1000 + 100][2], b[10 + 100][100000 + 100] = {0}; long long sol(int n, int i, int j) { if (i == n - 1) { long long m = -8; for (int t = 0; t < 3; t++) { if (t != j) m = max(m, a[i][t]); } return m; } long long m = -8, s = 0; for (int t = 0; t < 3; t++) { if (t != j) { if (m < a[i][t]) s = t; m = max(m, a[i][t]); } } if (b[i][s] == -1) b[i][s] = m + sol(n, i + 1, s); return b[i][s]; } int main() { int n, w; cin >> n >> w; for (int i = 1; i <= n; i++) for (int j = 0; j < 2; j++) { cin >> a[i][j]; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= w; j++) { if (j - a[i][0] > -1) { b[i][j] = max(a[i][1] + b[i - 1][j - a[i][0]], b[i - 1][j]); } else b[i][j] = b[i - 1][j]; } } cout << b[n][w] << endl; return 0; }
replace
4
5
4
5
0
p03163
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define int long long #define ld long double #define rep0(i, n) for (int i = 0; i < n; i++) #define rep1(i, n) for (int i = 1; i <= n; i++) #define rep(i, x, y) for (int i = x; i <= y; i++) #define reprev1(i, n) for (int i = n; i > 0; i--) #define reprev0(i, n) for (int i = n - 1; i >= 0; i--) #define reprev(i, x, y) for (int i = x; i >= y; i--) #define all(c) c.begin(), c.end() #define rall(c) c.rbegin(), c.rend() #define si set<int> #define vi vector<int> #define mii map<int, int> #define pii pair<int, int> #define usi unordered_set<int> #define umii unordered_map<int, int> #define pqmx priority_queue<int> #define pqmn priority_queue<int, vi, greater<int>> #define sp(x, y) fixed << setprecision(y) << x #define setbits(x) __builtin_popcount(x) #define zerobits(x) __builtin_ctz(x) #define in(a, n) \ vi a(n); \ rep0(i, n) cin >> a[i]; #define w(t) \ int t; \ cin >> t; \ while (t--) #define endl '\n' #define mod 1000000007 #define M (int)1e6 + 6 #define inf 1e18 using namespace std; int n, m, k, x, y, z, a1, b, c, d, i, j; int dp[M][105] = {}; int profit(int weight, int w[], int v[], int i) { if (i < 0) return 0; // if(dp[weight][i]!=0)return dp[weight][i]; if (w[i] > weight) return dp[weight][i] = profit(weight, w, v, i - 1); int op1 = profit(weight, w, v, i - 1); int op2 = v[i] + profit(weight - w[i], w, v, i - 1); return dp[weight][i] = max(op1, op2); } void sol() { int weight; cin >> n >> weight; int w[n], v[n]; rep0(i, n) cin >> w[i] >> v[i]; cout << profit(weight, w, v, n - 1) << endl; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // w(t) sol(); return 0; }
#include <bits/stdc++.h> #define int long long #define ld long double #define rep0(i, n) for (int i = 0; i < n; i++) #define rep1(i, n) for (int i = 1; i <= n; i++) #define rep(i, x, y) for (int i = x; i <= y; i++) #define reprev1(i, n) for (int i = n; i > 0; i--) #define reprev0(i, n) for (int i = n - 1; i >= 0; i--) #define reprev(i, x, y) for (int i = x; i >= y; i--) #define all(c) c.begin(), c.end() #define rall(c) c.rbegin(), c.rend() #define si set<int> #define vi vector<int> #define mii map<int, int> #define pii pair<int, int> #define usi unordered_set<int> #define umii unordered_map<int, int> #define pqmx priority_queue<int> #define pqmn priority_queue<int, vi, greater<int>> #define sp(x, y) fixed << setprecision(y) << x #define setbits(x) __builtin_popcount(x) #define zerobits(x) __builtin_ctz(x) #define in(a, n) \ vi a(n); \ rep0(i, n) cin >> a[i]; #define w(t) \ int t; \ cin >> t; \ while (t--) #define endl '\n' #define mod 1000000007 #define M (int)1e6 + 6 #define inf 1e18 using namespace std; int n, m, k, x, y, z, a1, b, c, d, i, j; int dp[M][105] = {}; int profit(int weight, int w[], int v[], int i) { if (i < 0) return 0; if (dp[weight][i] != 0) return dp[weight][i]; if (w[i] > weight) return dp[weight][i] = profit(weight, w, v, i - 1); int op1 = profit(weight, w, v, i - 1); int op2 = v[i] + profit(weight - w[i], w, v, i - 1); return dp[weight][i] = max(op1, op2); } void sol() { int weight; cin >> n >> weight; int w[n], v[n]; rep0(i, n) cin >> w[i] >> v[i]; cout << profit(weight, w, v, n - 1) << endl; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // w(t) sol(); return 0; }
replace
43
44
43
45
TLE
p03163
C++
Runtime Error
#include <algorithm> #include <cstring> #include <iostream> #define ll long long using namespace std; int n, w; ll dp[105][105]; ll V[105], W[105]; ll solve(int i, ll p) { if (i == n || p == 0) return 0; if (dp[i][p] != -1) return dp[i][p]; if (W[i] > p) return dp[i][p] = solve(i + 1, p); return dp[i][p] = max(V[i] + solve(i + 1, p - W[i]), solve(i + 1, p)); } int main() { cin >> n >> w; memset(dp, -1, sizeof dp); for (int i = 0; i < n; i++) cin >> W[i] >> V[i]; cout << solve(0, w) << endl; return 0; }
#include <algorithm> #include <cstring> #include <iostream> #define ll long long using namespace std; int n, w; ll dp[105][1000000]; ll V[105], W[105]; ll solve(int i, ll p) { if (i == n || p == 0) return 0; if (dp[i][p] != -1) return dp[i][p]; if (W[i] > p) return dp[i][p] = solve(i + 1, p); return dp[i][p] = max(V[i] + solve(i + 1, p - W[i]), solve(i + 1, p)); } int main() { cin >> n >> w; memset(dp, -1, sizeof dp); for (int i = 0; i < n; i++) cin >> W[i] >> V[i]; cout << solve(0, w) << endl; return 0; }
replace
7
8
7
8
0
p03163
C++
Runtime Error
/****************************************** * AUTHOR : SHUBHAM KUMAR * * NICK : { "CODECHEF" : "SCHELEON", "CODEFORCES" : "SCH3130N" } * * INSTITUTION : BIT MESRA * ******************************************/ #include <bits/stdc++.h> #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define sd(x) scanf("%lld", &x) #define ll long long int #define large 100005 #define MOD 1000000007 #define dd double #define pb push_back #define mk make_pair #define rep0(i, b) for (int i = 0; i < b; i++) #define rep1(i, b) for (int i = 1; i <= b; i++) #define rep2(i, a, b) for (int i = a; i <= b; i++) #define pll pair<ll, ll> #define ld long double #define vll vector<ll> #define vpll vector<pll> #define sortv(v) sort(v.begin(), v.end()) ll ModE(ll x, unsigned ll y, ll p = MOD) { ll res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } using namespace std; ll dp[100][large], a[large][3]; ll n; ll v[100], w[100]; ll solve(ll i, ll weight) { if (i < 0) return 0; if (dp[i][weight] != -1) return dp[i][weight]; if (weight - w[i] >= 0) dp[i][weight] = max(v[i] + solve(i - 1, weight - w[i]), solve(i - 1, weight)); else dp[i][weight] = solve(i - 1, weight); return dp[i][weight]; // if(weight-w[i]>=0) // return max(v[i]+solve(i-1, weight-w[i]), solve(i-1, weight)); // else // return solve(i-1, weight); // return dp[i][weight]; } int main() { // #ifndef _DEBUG #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 fastio // ios_base& scientific (ios_base& str); // ll T; cin>>T; while(T--){} ll W; cin >> n >> W; rep0(i, n) { cin >> w[i] >> v[i]; } rep0(i, large) rep0(j, n) dp[j][i] = -1; // rep0(i,10){ // rep0(j,10) // cout<<dp[i][j]<<" "; // cout<<endl;} // cout<<"hello"<<endl; cout << solve(n - 1, W); return 0; }
/****************************************** * AUTHOR : SHUBHAM KUMAR * * NICK : { "CODECHEF" : "SCHELEON", "CODEFORCES" : "SCH3130N" } * * INSTITUTION : BIT MESRA * ******************************************/ #include <bits/stdc++.h> #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define sd(x) scanf("%lld", &x) #define ll long long int #define large 100005 #define MOD 1000000007 #define dd double #define pb push_back #define mk make_pair #define rep0(i, b) for (int i = 0; i < b; i++) #define rep1(i, b) for (int i = 1; i <= b; i++) #define rep2(i, a, b) for (int i = a; i <= b; i++) #define pll pair<ll, ll> #define ld long double #define vll vector<ll> #define vpll vector<pll> #define sortv(v) sort(v.begin(), v.end()) ll ModE(ll x, unsigned ll y, ll p = MOD) { ll res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } using namespace std; ll dp[100][large], a[large][3]; ll n; ll v[100], w[100]; ll solve(ll i, ll weight) { if (i < 0) return 0; if (dp[i][weight] != -1) return dp[i][weight]; if (weight - w[i] >= 0) dp[i][weight] = max(v[i] + solve(i - 1, weight - w[i]), solve(i - 1, weight)); else dp[i][weight] = solve(i - 1, weight); return dp[i][weight]; // if(weight-w[i]>=0) // return max(v[i]+solve(i-1, weight-w[i]), solve(i-1, weight)); // else // return solve(i-1, weight); // return dp[i][weight]; } int main() { // #ifndef _DEBUG #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 fastio // ios_base& scientific (ios_base& str); // ll T; cin>>T; while(T--){} ll W; cin >> n >> W; rep0(i, n) { cin >> w[i] >> v[i]; } rep0(i, large) rep0(j, n) dp[j][i] = -1; // rep0(i,10){ // rep0(j,10) // cout<<dp[i][j]<<" "; // cout<<endl;} // cout<<"hello"<<endl; cout << solve(n - 1, W); return 0; }
replace
68
69
68
69
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; #define ll long long #define vv vector #define pp pair #define ff first #define ss second #define all(v) v.begin(), v.end() #define fastio \ ios_base::sync_with_stdio(0); \ cin.tie(0); typedef pair<ll, ll> pll; typedef vv<ll> vll; typedef vv<pll> vpll; #define ordered_set \ tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> // adds two more functions to set //(1)*(set.find_by_order(k))[kth element in the sorted set] //(2)set.order_of_key(k)[count of elements strictly less than k] #define LG(args...) \ { \ string _s = #args; \ replace(_s.begin(), _s.end(), ',', ' '); \ stringstream _ss(_s); \ err(istream_iterator<string>(_ss), args); \ cerr << endl; \ } void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << "(" << *it << ":" << a << ") "; err(++it, args...); } //::::::::::::::::::::::::::::::::::::::::::::: const ll INF = 9e18; const ll MOD = 1e9 + 7; // #define dbg #define val ff #define wt ss // #define MXN 109 // #define MXW 100009 ll n, W; vpll v; int main() { fastio; // for(auto& i: dp){ // for(auto& j: i){ // j= INF; // } // } cin >> n >> W; v.resize(n); ll wmax = 0; for (auto &i : v) { cin >> i.wt >> i.val; wmax += i.wt; } // so need for dp with state elements allowed and weight // hence to save space make only 1D dp // dp[i] at any time is the maximum value u can acheive with // exactly i as weight // So firstly only 1 item is allowed and then allowed lost grows to // n size. // dp state will be like dp[wt_already + w[i]] = max(self, dp[wt_already] + // val[i]) makes sense but the problem is order of execution so notice that // though dp is 1D but there id a hidden state of allowed elements hence the // dp[wt_already] should be the one when allowed items were till i-1 so when // we add another item and inc it by val[i] we will be eligible to call it a // state for i allowed items Since allowed items increase hence loop goes // for(i :0-> n-1) now notice to update a dp of higher weight we need dp state // of previuos i in lower dp states hence update larger wights first hence rev // for loop for weights see possible updates in weight can be wmax - w[i] to // zero , becuase its the range of wt_already vll dp(wmax + 1, 0); auto maxself = [](ll &a, ll b) { a = max(a, b); }; for (ll i = 0; i < n; i++) { for (ll j = wmax - v[i].wt; j >= 0; j--) { maxself(dp[j + v[i].wt], dp[j] + v[i].val); } } ll ans = 0; for (ll i = 0; i <= W; i++) { maxself(ans, dp[i]); // LG(i,dp[i]) } cout << ans; }
#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; #define ll long long #define vv vector #define pp pair #define ff first #define ss second #define all(v) v.begin(), v.end() #define fastio \ ios_base::sync_with_stdio(0); \ cin.tie(0); typedef pair<ll, ll> pll; typedef vv<ll> vll; typedef vv<pll> vpll; #define ordered_set \ tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> // adds two more functions to set //(1)*(set.find_by_order(k))[kth element in the sorted set] //(2)set.order_of_key(k)[count of elements strictly less than k] #define LG(args...) \ { \ string _s = #args; \ replace(_s.begin(), _s.end(), ',', ' '); \ stringstream _ss(_s); \ err(istream_iterator<string>(_ss), args); \ cerr << endl; \ } void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << "(" << *it << ":" << a << ") "; err(++it, args...); } //::::::::::::::::::::::::::::::::::::::::::::: const ll INF = 9e18; const ll MOD = 1e9 + 7; // #define dbg #define val ff #define wt ss // #define MXN 109 // #define MXW 100009 ll n, W; vpll v; int main() { fastio; // for(auto& i: dp){ // for(auto& j: i){ // j= INF; // } // } cin >> n >> W; v.resize(n); ll wmax = 0; for (auto &i : v) { cin >> i.wt >> i.val; wmax += i.wt; } // so need for dp with state elements allowed and weight // hence to save space make only 1D dp // dp[i] at any time is the maximum value u can acheive with // exactly i as weight // So firstly only 1 item is allowed and then allowed lost grows to // n size. // dp state will be like dp[wt_already + w[i]] = max(self, dp[wt_already] + // val[i]) makes sense but the problem is order of execution so notice that // though dp is 1D but there id a hidden state of allowed elements hence the // dp[wt_already] should be the one when allowed items were till i-1 so when // we add another item and inc it by val[i] we will be eligible to call it a // state for i allowed items Since allowed items increase hence loop goes // for(i :0-> n-1) now notice to update a dp of higher weight we need dp state // of previuos i in lower dp states hence update larger wights first hence rev // for loop for weights see possible updates in weight can be wmax - w[i] to // zero , becuase its the range of wt_already vll dp(wmax + 1, 0); auto maxself = [](ll &a, ll b) { a = max(a, b); }; for (ll i = 0; i < n; i++) { for (ll j = wmax - v[i].wt; j >= 0; j--) { maxself(dp[j + v[i].wt], dp[j] + v[i].val); } } ll ans = 0; for (ll i = 0; i <= min(W, wmax); i++) { maxself(ans, dp[i]); // LG(i,dp[i]) } cout << ans; }
replace
91
92
91
92
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define pii pair<int, int> #define ll long long #define s second #define f first ll n, W, val[105], wt[105], dp[105][105]; int main() { cin >> n >> W; for (int i = 0; i < n; ++i) { cin >> wt[i] >> val[i]; } for (int i = 0; i <= n; ++i) { for (int j = 0; j <= W; ++j) { if (i == 0 || j == 0) { dp[i][j] = 0; } else if (wt[i - 1] > j) { dp[i][j] = dp[i - 1][j]; } else { dp[i][j] = max(val[i - 1] + dp[i - 1][j - wt[i - 1]], dp[i - 1][j]); } } } cout << dp[n][W]; return 0; }
#include <bits/stdc++.h> using namespace std; #define pii pair<int, int> #define ll long long #define s second #define f first ll n, W, val[105], wt[105], dp[105][100005]; int main() { cin >> n >> W; for (int i = 0; i < n; ++i) { cin >> wt[i] >> val[i]; } for (int i = 0; i <= n; ++i) { for (int j = 0; j <= W; ++j) { if (i == 0 || j == 0) { dp[i][j] = 0; } else if (wt[i - 1] > j) { dp[i][j] = dp[i - 1][j]; } else { dp[i][j] = max(val[i - 1] + dp[i - 1][j - wt[i - 1]], dp[i - 1][j]); } } } cout << dp[n][W]; return 0; }
replace
9
10
9
10
0
p03163
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdlib> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; #define rep(i, s, e) for (int(i) = (s); (i) <= (e); ++(i)) #define all(x) x.begin(), x.end() int main() { int N, W; cin >> N >> W; vector<int> w(N + 1); vector<ll> v(N + 1); rep(i, 1, N) cin >> w[i] >> v[i]; vector<vector<ll>> dp(N + 1, vector<ll>(5001001, 0)); rep(i, 1, N) rep(j, 0, W) { 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]); } cout << dp[N][W] << endl; }
#include <algorithm> #include <cmath> #include <cstdlib> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; #define rep(i, s, e) for (int(i) = (s); (i) <= (e); ++(i)) #define all(x) x.begin(), x.end() int main() { int N, W; cin >> N >> W; vector<int> w(N + 1); vector<ll> v(N + 1); rep(i, 1, N) cin >> w[i] >> v[i]; vector<vector<ll>> dp(N + 1, vector<ll>(500000, 0)); rep(i, 1, N) rep(j, 0, W) { 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]); } cout << dp[N][W] << endl; }
replace
23
24
23
24
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p03163
C++
Runtime Error
#define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <bitset> #include <cmath> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_set> #include <vector> using namespace std; #define ll long long #define endl "\n" const int dx[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1}; const int dy[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1}; const ll integer = pow(2, 32) - 1; // 4294967295 #define N 8010000 #define TC \ int t; \ cin >> t; \ for (int T = 1; T <= t; T++) #define mod 1000000007 #define PI acos(-1) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define clr(v, d) memset(v, d, sizeof(v)) void AIA() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); } bool isprime(ll n) { for (ll i = 2; i * i <= n; i++) { if (n % i == 0) return 0; } return (n != 1); } ll gcd(ll x, ll y) { return (y == 0 ? x : gcd(y, x % y)); } ll lcm(ll x, ll y) { return (x * y) / gcd(x, y); } /* _____________ __ ___ ___ ___________________________ ____ ____ | | /\ \ | | | | | | \ \ / / | __________| / /\ \ | | | | | _____ _____ | \ \ / / | | / / \ \ | | | | | | | | | | \ \ / / | | / / \ \ | | | | | | | | | | \ \/ / | |______ / / \ \ | |______| | | | | | | | \ / | | / / \ \ | | | | | | | | \ / | ______| / / \ \ | ______ | | | | | | | | | | | / /============\ \ | | | | | | | | | | | | | | / /==============\ \ | | | | | | | | | | | | | | / / \ \ | | | | | | | | | | | | | | / / \ \ | | | | | | | | | | | | |__| /_/ \_\ |___| |___| |_____| |____| |____| |__| <<<<<<<<<<< NEVER GIVE UP !! >>>>>>>>>>> <<<<<<<< YOU CAN DO IT IF YOU WANT !! >>>>>>>> <<<<<<<<< BELIEVE IN YOURSELF !! >>>>>>>>> */ ll n, x, k, m, cnt = 0; vector<ll> v3, v, w; ll dp[107][100007]; ll solve(ll i, ll W) { if (i == 0 || W == 0) { return 0; } ll &ans = dp[i][W]; if (ans != -1) { return ans; } else { if (W >= w[i - 1]) { return dp[i][W] = max(v[i - 1] + solve(i - 1, W - w[i - 1]), solve(i - 1, W)); } else { dp[i][W] = solve(i - 1, W); } } } int main() { // cout<<fixed<<setprecision(20); // freopen("Input.txt.txt", "r", stdin); // freopen("Output.txt.txt", "w", stdout); AIA(); cin >> n >> k; v.resize(n); w.resize(n); for (int i = 0; i < n; i++) { cin >> w[i] >> v[i]; } memset(dp, -1, sizeof dp); cout << solve(n, k); return 0; }
#define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <bitset> #include <cmath> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_set> #include <vector> using namespace std; #define ll long long #define endl "\n" const int dx[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1}; const int dy[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1}; const ll integer = pow(2, 32) - 1; // 4294967295 #define N 8010000 #define TC \ int t; \ cin >> t; \ for (int T = 1; T <= t; T++) #define mod 1000000007 #define PI acos(-1) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define clr(v, d) memset(v, d, sizeof(v)) void AIA() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); } bool isprime(ll n) { for (ll i = 2; i * i <= n; i++) { if (n % i == 0) return 0; } return (n != 1); } ll gcd(ll x, ll y) { return (y == 0 ? x : gcd(y, x % y)); } ll lcm(ll x, ll y) { return (x * y) / gcd(x, y); } /* _____________ __ ___ ___ ___________________________ ____ ____ | | /\ \ | | | | | | \ \ / / | __________| / /\ \ | | | | | _____ _____ | \ \ / / | | / / \ \ | | | | | | | | | | \ \ / / | | / / \ \ | | | | | | | | | | \ \/ / | |______ / / \ \ | |______| | | | | | | | \ / | | / / \ \ | | | | | | | | \ / | ______| / / \ \ | ______ | | | | | | | | | | | / /============\ \ | | | | | | | | | | | | | | / /==============\ \ | | | | | | | | | | | | | | / / \ \ | | | | | | | | | | | | | | / / \ \ | | | | | | | | | | | | |__| /_/ \_\ |___| |___| |_____| |____| |____| |__| <<<<<<<<<<< NEVER GIVE UP !! >>>>>>>>>>> <<<<<<<< YOU CAN DO IT IF YOU WANT !! >>>>>>>> <<<<<<<<< BELIEVE IN YOURSELF !! >>>>>>>>> */ ll n, x, k, m, cnt = 0; vector<ll> v3, v, w; ll dp[107][100007]; ll solve(ll i, ll W) { if (i == 0 || W == 0) { return 0; } ll &ans = dp[i][W]; if (ans != -1) { return ans; } else { if (W >= w[i - 1]) { return dp[i][W] = max(v[i - 1] + solve(i - 1, W - w[i - 1]), solve(i - 1, W)); } else { return dp[i][W] = solve(i - 1, W); } } } int main() { // cout<<fixed<<setprecision(20); // freopen("Input.txt.txt", "r", stdin); // freopen("Output.txt.txt", "w", stdout); AIA(); cin >> n >> k; v.resize(n); w.resize(n); for (int i = 0; i < n; i++) { cin >> w[i] >> v[i]; } memset(dp, -1, sizeof dp); cout << solve(n, k); return 0; }
replace
88
89
88
89
0
p03163
C++
Runtime Error
#include <algorithm> #include <cstdio> using namespace std; using LL = long long; int N, W, w, v; LL dp[105]; int main() { scanf("%d%d", &N, &W); for (int i = 1; i <= N; i++) { scanf("%d%d", &w, &v); for (int j = W; j >= w; j--) dp[j] = max(dp[j], dp[j - w] + v); } printf("%lld", dp[W]); return 0; }
#include <algorithm> #include <cstdio> using namespace std; using LL = long long; int N, W, w, v; LL dp[100005]; int main() { scanf("%d%d", &N, &W); for (int i = 1; i <= N; i++) { scanf("%d%d", &w, &v); for (int j = W; j >= w; j--) dp[j] = max(dp[j], dp[j - w] + v); } printf("%lld", dp[W]); return 0; }
replace
6
7
6
7
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int maxx = 1e6 + 10; struct node { int w; int v; } in[105]; long long dp[105] = {0}; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d%d", &in[i].w, &in[i].v); } for (int j = 0; j < n; j++) { for (int i = m; i >= 0; i--) { if (in[j].w <= i) { dp[i] = max(dp[i], dp[i - in[j].w] + in[j].v); } } } printf("%lld", dp[m]); }
#include <bits/stdc++.h> using namespace std; const int maxx = 1e6 + 10; struct node { int w; int v; } in[105]; long long dp[maxx] = {0}; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d%d", &in[i].w, &in[i].v); } for (int j = 0; j < n; j++) { for (int i = m; i >= 0; i--) { if (in[j].w <= i) { dp[i] = max(dp[i], dp[i - in[j].w] + in[j].v); } } } printf("%lld", dp[m]); }
replace
7
8
7
8
0
p03163
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define IO \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define int long long using namespace std; const int MX = 1e5 + 5; int dp[MX][105]; vector<int> v, w; int n, W; int solve(int weight, int ind) { if (weight > W) return -1e17; if (ind == n + 1) return 0; if (dp[weight][ind] != -1) return dp[weight][ind]; long long ans = -1e17; for (int i = 1; i <= n; i++) { ans = max(ans, solve(weight + w[ind], ind + 1) + v[ind]); ans = max(ans, solve(weight, ind + 1)); } return dp[weight][ind] = ans; } int32_t main() { memset(dp, -1, sizeof dp); cin >> n >> W; v.resize(n + 1); w.resize(n + 1); for (int i = 1; i <= n; i++) cin >> w[i] >> v[i]; cout << solve(0, 1); return 0; }
#include <bits/stdc++.h> #define IO \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define int long long using namespace std; const int MX = 1e5 + 5; int dp[MX][105]; vector<int> v, w; int n, W; int solve(int weight, int ind) { if (weight > W) return -1e17; if (ind == n + 1) return 0; if (dp[weight][ind] != -1) return dp[weight][ind]; long long ans = -1e17; ans = max(ans, solve(weight + w[ind], ind + 1) + v[ind]); ans = max(ans, solve(weight, ind + 1)); return dp[weight][ind] = ans; } int32_t main() { memset(dp, -1, sizeof dp); cin >> n >> W; v.resize(n + 1); w.resize(n + 1); for (int i = 1; i <= n; i++) cin >> w[i] >> v[i]; cout << solve(0, 1); return 0; }
replace
20
24
20
22
TLE
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int n, W; int w[110], v[110]; long long dp[10100]; int main() { 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 = W; j >= w[i]; j--) { dp[j] = max(dp[j], dp[j - w[i]] + 1ll * v[i]); } } long long ans = 0; for (int i = 0; i <= W; i++) ans = max(ans, dp[i]); cout << ans << '\n'; }
#include <bits/stdc++.h> using namespace std; int n, W; int w[110], v[110]; long long dp[110000]; int main() { 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 = W; j >= w[i]; j--) { dp[j] = max(dp[j], dp[j - w[i]] + 1ll * v[i]); } } long long ans = 0; for (int i = 0; i <= W; i++) ans = max(ans, dp[i]); cout << ans << '\n'; }
replace
6
7
6
7
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define f(i, a, b) for (int i = (a); i < (b); i++) #define frr(i, a, b) for (ll i = (a - 1); i >= (b); i--) #define vi vector<int> #define de deque<int> #define del deque<ll> #define vl vector<ll> #define pb push_back #define ppb pop_back() #define pf push_front #define ppf pop_front() #define mpp map<ll, ll> #define rev(a) reverse(a.begin(), a.end()) #define srt(a) sort(a.begin(), a.end()) #define ln(a) a.length() #define sz(a) a.size() #define endl "\n" int M = 1000000007; ll n; ll dp[105][105]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; ll mw; cin >> mw; ll w[n], v[n]; f(i, 0, n) { cin >> w[i] >> v[i]; } f(i, 0, n) { dp[i][0] = 0; } f(j, 0, mw + 1) { if (j >= w[0]) { dp[0][j] = v[0]; } else { dp[0][j] = 0; } } f(i, 1, n) { f(j, 1, mw + 1) { if (j >= w[i]) { dp[i][j] = max(dp[i - 1][j], v[i] + dp[i - 1][j - w[i]]); } else { dp[i][j] = dp[i - 1][j]; } } } cout << dp[n - 1][mw] << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define f(i, a, b) for (int i = (a); i < (b); i++) #define frr(i, a, b) for (ll i = (a - 1); i >= (b); i--) #define vi vector<int> #define de deque<int> #define del deque<ll> #define vl vector<ll> #define pb push_back #define ppb pop_back() #define pf push_front #define ppf pop_front() #define mpp map<ll, ll> #define rev(a) reverse(a.begin(), a.end()) #define srt(a) sort(a.begin(), a.end()) #define ln(a) a.length() #define sz(a) a.size() #define endl "\n" int M = 1000000007; ll n; ll dp[105][100005]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; ll mw; cin >> mw; ll w[n], v[n]; f(i, 0, n) { cin >> w[i] >> v[i]; } f(i, 0, n) { dp[i][0] = 0; } f(j, 0, mw + 1) { if (j >= w[0]) { dp[0][j] = v[0]; } else { dp[0][j] = 0; } } f(i, 1, n) { f(j, 1, mw + 1) { if (j >= w[i]) { dp[i][j] = max(dp[i - 1][j], v[i] + dp[i - 1][j - w[i]]); } else { dp[i][j] = dp[i - 1][j]; } } } cout << dp[n - 1][mw] << endl; }
replace
21
22
21
22
0
p03163
C++
Runtime Error
#include <cstdlib> #include <iostream> #include <vector> using namespace std; typedef long long ll; 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; } const ll INF = 1LL << 60; const int MAX = 100100; // input int N; ll W, weight[110], value[110]; // dp table ll dp[110][MAX] = {0}; int main() { // input cin >> N >> W; for (int i = 0; i < N; i++) cin >> weight[i] >> value[i]; // loop for (int i = 0; i < N; i++) { for (int sum_w = 0; sum_w <= W; sum_w++) { // choose i-th item chmax(dp[i + 1][sum_w], dp[i][sum_w - weight[i]] + value[i]); // don't choose i-th item chmax(dp[i + 1][sum_w], dp[i][sum_w]); } } // output cout << dp[N][W] << '\n'; }
#include <cstdlib> #include <iostream> #include <vector> using namespace std; typedef long long ll; 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; } const ll INF = 1LL << 60; const int MAX = 100100; // input int N; ll W, weight[110], value[110]; // dp table ll dp[110][MAX] = {0}; int main() { // input cin >> N >> W; for (int i = 0; i < N; i++) cin >> weight[i] >> value[i]; // loop for (int i = 0; i < N; i++) { for (int sum_w = 0; sum_w <= W; sum_w++) { // choose i-th item if (sum_w - weight[i] >= 0) { chmax(dp[i + 1][sum_w], dp[i][sum_w - weight[i]] + value[i]); } // don't choose i-th item chmax(dp[i + 1][sum_w], dp[i][sum_w]); } } // output cout << dp[N][W] << '\n'; }
replace
42
43
42
45
0
p03163
C++
Runtime Error
#include <algorithm> #include <bits/stdc++.h> #include <deque> #include <iterator> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define rep2(i, s, n) for (long long i = (s); i(n); i++) #define ll long long int main() { int N, W; cin >> N >> W; ll dp[101][100001] = {0}; rep(i, N + 1) { ll w, v; cin >> w >> v; rep(j, W + 1) { int dw; if (j - w >= 0) { dw = j - w; dp[i + 1][j] = max(dp[i][j], dp[i][dw] + v); } else dp[i + 1][j] = dp[i][j]; } } // rep(i,N){ // rep(j,W){ // cout << dp[i][j] <<" "; // } // cout<<endl; // } ll ans = 0; rep(i, W + 1) ans = max(ans, dp[N][i]); cout << ans << endl; }
#include <algorithm> #include <bits/stdc++.h> #include <deque> #include <iterator> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define rep2(i, s, n) for (long long i = (s); i(n); i++) #define ll long long int main() { int N, W; cin >> N >> W; ll dp[101][100001] = {0}; rep(i, N) { ll w, v; cin >> w >> v; rep(j, W + 1) { int dw; if (j - w >= 0) { dw = j - w; dp[i + 1][j] = max(dp[i][j], dp[i][dw] + v); } else dp[i + 1][j] = dp[i][j]; } } // rep(i,N){ // rep(j,W){ // cout << dp[i][j] <<" "; // } // cout<<endl; // } ll ans = 0; rep(i, W + 1) ans = max(ans, dp[N][i]); cout << ans << endl; }
replace
16
17
16
17
-11
p03163
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <vector> using namespace std; long long n, val[100005], w, v, k, mxw, ans; bool dp[100005]; int main() { dp[0] = true; scanf("%lld%lld", &n, &mxw); for (int i = 1; i <= n; i++) { scanf("%lld%lld", &w, &v); for (int j = mxw; j >= 0; j--) { if (dp[j]) { dp[j + w] = true; val[j + w] = max(val[j + w], val[j] + v); } } } ans = 0; for (int i = mxw; i >= 1; i--) { ans = max(ans, val[i]); } printf("%lld\n", ans); return 0; }
#include <algorithm> #include <cstdio> #include <vector> using namespace std; long long n, val[100005], w, v, k, mxw, ans; bool dp[100005]; int main() { dp[0] = true; scanf("%lld%lld", &n, &mxw); for (int i = 1; i <= n; i++) { scanf("%lld%lld", &w, &v); for (int j = mxw; j >= 0; j--) { if (dp[j] == true && j + w <= mxw) { dp[j + w] = true; val[j + w] = max(val[j + w], val[j] + v); } } } ans = 0; for (int i = mxw; i >= 1; i--) { ans = max(ans, val[i]); } printf("%lld\n", ans); return 0; }
replace
13
14
13
14
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> #define FOR(i, a, b) for (int i = (a); i < ((int)b); ++i) #define RFOR(i, a) for (int i = (a); i >= 0; --i) #define FOE(i, a) for (auto i : a) #define ALL(c) (c).begin(), (c).end() #define RALL(c) (c).rbegin(), (c).rend() #define DUMP(x) cerr << #x << " = " << (x) << endl; #define SUM(x) std::accumulate(ALL(x), 0LL) #define MIN(v) *std::min_element(v.begin(), v.end()) #define MAX(v) *std::max_element(v.begin(), v.end()) #define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end()) #define BIT_ON(bit, i) (((bit >> i) & 1) > 0) typedef long long LL; template <typename T> std::vector<T> make_v(size_t a) { return std::vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return std::vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } // C++14 template <typename T, typename V> typename std::enable_if<std::is_class<T>::value == 0>::type fill_v(T &t, const V &v) { t = v; } template <typename T, typename V> typename std::enable_if<std::is_class<T>::value != 0>::type fill_v(T &t, const V &v) { for (auto &e : t) fill_v(e, v); } template <class T> inline T ceil(T a, T b) { return (a + b - 1) / b; } template <class T> inline void print(T x) { std::cout << x << std::endl; } template <class T> inline void print_vec(const std::vector<T> &v) { for (int i = 0; i < v.size(); ++i) { if (i != 0) { std::cout << " "; } std::cout << v[i]; } std::cout << "\n"; } template <class T> inline bool inside(T y, T x, T H, T W) { return 0 <= y and y < H and 0 <= x and x < W; } template <class T> inline double euclidean_distance(T y1, T x1, T y2, T x2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); } template <class T> inline double manhattan_distance(T y1, T x1, T y2, T x2) { return abs(x1 - x2) + abs(y1 - y2); } template <typename T> T &chmin(T &a, const T &b) { return a = std::min(a, b); } template <typename T> T &chmax(T &a, const T &b) { return a = std::max(a, b); } template <class T> inline std::vector<T> unique(std::vector<T> v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); return v; } // 初項s, 交差dのn個の数列の和 long long sum_of_arithmetic_progression(long long s, long long d, long long n) { return n * (2 * s + (n - 1) * d) / 2; } // 2のべき乗数かどうか bool is_power_of_two(long long x) { return !(x & (x - 1)); } // aとbの最大公約数 O(log min(a, b)) long long gcd(long long a, long long b) { if (b == 0) { return a; } return gcd(b, a % b); } // 数列vの最大公約数 O(N log d) long long gcd(std::vector<long long> &v) { long long ans = v[0]; for (int i = 1; i < (int)v.size(); ++i) { ans = gcd(ans, v[i]); } return ans; } // aとbの最小公倍数 O(log min(a, b)) long long lcm(long long a, long long b) { long long g = gcd(a, b); return a / g * b; } const int INF = 1 << 30; const double EPS = 1e-9; const long double PI = acos(-1.0); const std::vector<int> dy2 = {0, 1}, dx2 = {1, 0}; // 右,下 const std::vector<int> dy4 = {0, -1, 0, 1}, dx4 = {1, 0, -1, 0}; // 右,上,左,下 const std::vector<int> dy8 = {0, -1, 0, 1, 1, -1, -1, 1}, dx8 = {1, 0, -1, 0, 1, 1, -1, -1}; using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); LL N, W; cin >> N >> W; auto weight = make_v<LL>(N); auto value = make_v<LL>(N); FOR(i, 0, N) { cin >> weight[i] >> value[i]; } auto dp = make_v<LL>(N + 100, W + 100); FOR(i, 0, N) { FOR(j, 0, W + 1) { // use chmax(dp[i + 1][j + weight[i]], dp[i][j] + value[i]); // not use chmax(dp[i + 1][j], dp[i][j]); } } LL ans = -INF; FOR(j, 0, W + 1) { chmax(ans, dp[N][j]); } print(ans); return 0; }
#include <bits/stdc++.h> #define FOR(i, a, b) for (int i = (a); i < ((int)b); ++i) #define RFOR(i, a) for (int i = (a); i >= 0; --i) #define FOE(i, a) for (auto i : a) #define ALL(c) (c).begin(), (c).end() #define RALL(c) (c).rbegin(), (c).rend() #define DUMP(x) cerr << #x << " = " << (x) << endl; #define SUM(x) std::accumulate(ALL(x), 0LL) #define MIN(v) *std::min_element(v.begin(), v.end()) #define MAX(v) *std::max_element(v.begin(), v.end()) #define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end()) #define BIT_ON(bit, i) (((bit >> i) & 1) > 0) typedef long long LL; template <typename T> std::vector<T> make_v(size_t a) { return std::vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return std::vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } // C++14 template <typename T, typename V> typename std::enable_if<std::is_class<T>::value == 0>::type fill_v(T &t, const V &v) { t = v; } template <typename T, typename V> typename std::enable_if<std::is_class<T>::value != 0>::type fill_v(T &t, const V &v) { for (auto &e : t) fill_v(e, v); } template <class T> inline T ceil(T a, T b) { return (a + b - 1) / b; } template <class T> inline void print(T x) { std::cout << x << std::endl; } template <class T> inline void print_vec(const std::vector<T> &v) { for (int i = 0; i < v.size(); ++i) { if (i != 0) { std::cout << " "; } std::cout << v[i]; } std::cout << "\n"; } template <class T> inline bool inside(T y, T x, T H, T W) { return 0 <= y and y < H and 0 <= x and x < W; } template <class T> inline double euclidean_distance(T y1, T x1, T y2, T x2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); } template <class T> inline double manhattan_distance(T y1, T x1, T y2, T x2) { return abs(x1 - x2) + abs(y1 - y2); } template <typename T> T &chmin(T &a, const T &b) { return a = std::min(a, b); } template <typename T> T &chmax(T &a, const T &b) { return a = std::max(a, b); } template <class T> inline std::vector<T> unique(std::vector<T> v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); return v; } // 初項s, 交差dのn個の数列の和 long long sum_of_arithmetic_progression(long long s, long long d, long long n) { return n * (2 * s + (n - 1) * d) / 2; } // 2のべき乗数かどうか bool is_power_of_two(long long x) { return !(x & (x - 1)); } // aとbの最大公約数 O(log min(a, b)) long long gcd(long long a, long long b) { if (b == 0) { return a; } return gcd(b, a % b); } // 数列vの最大公約数 O(N log d) long long gcd(std::vector<long long> &v) { long long ans = v[0]; for (int i = 1; i < (int)v.size(); ++i) { ans = gcd(ans, v[i]); } return ans; } // aとbの最小公倍数 O(log min(a, b)) long long lcm(long long a, long long b) { long long g = gcd(a, b); return a / g * b; } const int INF = 1 << 30; const double EPS = 1e-9; const long double PI = acos(-1.0); const std::vector<int> dy2 = {0, 1}, dx2 = {1, 0}; // 右,下 const std::vector<int> dy4 = {0, -1, 0, 1}, dx4 = {1, 0, -1, 0}; // 右,上,左,下 const std::vector<int> dy8 = {0, -1, 0, 1, 1, -1, -1, 1}, dx8 = {1, 0, -1, 0, 1, 1, -1, -1}; using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); LL N, W; cin >> N >> W; auto weight = make_v<LL>(N); auto value = make_v<LL>(N); FOR(i, 0, N) { cin >> weight[i] >> value[i]; } auto dp = make_v<LL>(N + 100, W + 100); FOR(i, 0, N) { FOR(j, 0, W + 1) { // use if (j + weight[i] <= W) { chmax(dp[i + 1][j + weight[i]], dp[i][j] + value[i]); } // not use chmax(dp[i + 1][j], dp[i][j]); } } LL ans = -INF; FOR(j, 0, W + 1) { chmax(ans, dp[N][j]); } print(ans); return 0; }
replace
116
117
116
119
0
p03163
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define ll long long ll i, j, n, m, w, c, ans, p[109], wt[109], dp[100009][109]; map<ll, ll> mp; ll func(ll w, ll i) { if (mp[w] == 0) mp[w] = ++c; ll x = mp[w]; if (dp[x][i] != -1) return dp[x][i]; if (i == n) return 0; if (w >= wt[i]) dp[x][i] = max(p[i] + func(w - wt[i], i + 1), func(w, i + 1)); else dp[x][i] = func(w, i + 1); return dp[x][i]; } int main() { ios_base::sync_with_stdio(false), cin.tie(0); cin >> n >> w; memset(dp, -1, sizeof(dp)); for (i = 0; i < n; i++) cin >> wt[i] >> p[i]; ans = func(w, 0); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define ll long long ll i, j, n, m, w, c, ans, p[109], wt[109], dp[100009][109], mp[100009]; ll func(ll w, ll i) { if (mp[w] == 0) mp[w] = ++c; ll x = mp[w]; if (dp[x][i] != -1) return dp[x][i]; if (i == n) return 0; if (w >= wt[i]) dp[x][i] = max(p[i] + func(w - wt[i], i + 1), func(w, i + 1)); else dp[x][i] = func(w, i + 1); return dp[x][i]; } int main() { ios_base::sync_with_stdio(false), cin.tie(0); cin >> n >> w; memset(dp, -1, sizeof(dp)); for (i = 0; i < n; i++) cin >> wt[i] >> p[i]; ans = func(w, 0); cout << ans << endl; }
replace
3
5
3
4
TLE
p03163
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define ios \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define rep(i, a, b) for (int i = a; i < b; i++) #define pb push_back int main() { ios int N, W; cin >> N >> W; vector<int> w(110, 0); vector<int> v(110, 0); vector<vector<ll>> dp(110, vector<ll>(110000, 0)); rep(i, 0, N) cin >> w[i] >> v[i]; int k = 0; rep(i, 0, N) dp[i][k] = 0; rep(i, 0, N) rep(j, 1, W + 1) { if (i == 0) { rep(j, 0, W + 1) { if (j < w[i]) dp[i][j] = 0; else dp[i][j] = v[i]; } continue; } if (j < w[i]) dp[i][j] = dp[i - 1][j]; else { dp[i][j] = max(v[i] + dp[i - 1][j - w[i]], dp[i - 1][j]); } } cout << dp[N - 1][W]; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define ios \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define rep(i, a, b) for (int i = a; i < b; i++) #define pb push_back int main() { ios int N, W; cin >> N >> W; vector<int> w(110, 0); vector<int> v(110, 0); vector<vector<ll>> dp(110, vector<ll>(110000, 0)); rep(i, 0, N) cin >> w[i] >> v[i]; int k = 0; rep(i, 0, N) dp[i][k] = 0; rep(i, 0, N) rep(j, 1, W + 1) { if (i == 0) { if (j < w[i]) dp[i][j] = 0; else dp[i][j] = v[i]; continue; } if (j < w[i]) dp[i][j] = dp[i - 1][j]; else { dp[i][j] = max(v[i] + dp[i - 1][j - w[i]], dp[i - 1][j]); } } cout << dp[N - 1][W]; return 0; }
replace
21
27
21
25
TLE
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; #define ll long long #define ld long double typedef vector<ll> vi; // Vector of long long typedef vector<vi> vvi; // Vector of vi typedef vector<pair<ll, ll>> ii; // Vector of pairs #define ff first // For pairs #define ss second // For pairs #define sz(a) int((a).size()) // Function to determine size of any container #define pb push_back // Pushback to vector #define mp make_pair // Makes pairs to be stored as pair #define all(c) (c).begin(), (c).end() // Mainly used by me in sorting // ordered_set adds two new functions to set - (set).find_by_order([kth element // based on zero indexing]) and order_of_key() order_of_key returns number of // elements less that parameter. If element exists, that order is its index #define ordered_set \ tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> #define MAXW (ll)(1e5 + 5) #define MAXN (ll)(1e2 + 5) #define INF (ll)(1e12) ll n, w; ll dp[MAXN][MAXN]; ll mg[MAXN]; ll val[MAXN]; ll solve(ll N, ll W) { if (W < 0) return -INF; if (N < 0) return 0; if (dp[N][W] == -1) dp[N][W] = max(solve(N - 1, W - mg[N]) + val[N], solve(N - 1, W)); return dp[N][W]; } int main(void) { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> w; for (int i = 0; i < n; i++) cin >> mg[i] >> val[i]; memset(dp, -1, sizeof(dp)); cout << solve(n - 1, w) << "\n"; }
#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; #define ll long long #define ld long double typedef vector<ll> vi; // Vector of long long typedef vector<vi> vvi; // Vector of vi typedef vector<pair<ll, ll>> ii; // Vector of pairs #define ff first // For pairs #define ss second // For pairs #define sz(a) int((a).size()) // Function to determine size of any container #define pb push_back // Pushback to vector #define mp make_pair // Makes pairs to be stored as pair #define all(c) (c).begin(), (c).end() // Mainly used by me in sorting // ordered_set adds two new functions to set - (set).find_by_order([kth element // based on zero indexing]) and order_of_key() order_of_key returns number of // elements less that parameter. If element exists, that order is its index #define ordered_set \ tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> #define MAXW (ll)(1e5 + 5) #define MAXN (ll)(1e2 + 5) #define INF (ll)(1e12) ll n, w; ll dp[MAXN][MAXW]; ll mg[MAXN]; ll val[MAXN]; ll solve(ll N, ll W) { if (W < 0) return -INF; if (N < 0) return 0; if (dp[N][W] == -1) dp[N][W] = max(solve(N - 1, W - mg[N]) + val[N], solve(N - 1, W)); return dp[N][W]; } int main(void) { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> w; for (int i = 0; i < n; i++) cin >> mg[i] >> val[i]; memset(dp, -1, sizeof(dp)); cout << solve(n - 1, w) << "\n"; }
replace
27
28
27
28
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) \ ; \ for (long long i = 0; i < (n); i++) typedef long long ll; typedef vector<long long> vl; const ll INF = 100000000009; ll dp[105][100005]; template <class T> inline bool chmax(T &x, T y) { if (x < y) { x = y; return true; } else return false; } int main() { ll N, W; cin >> N >> W; vl n(N); vl w(N); rep(i, N) cin >> w[i] >> n[i]; rep(i, N + 10) { rep(j, W + 10) { dp[i][j] = 0; } } rep(i, N) { rep(j, W + 1) { if (j >= w[i]) dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + n[i]); else dp[i + 1][j] = dp[i][j]; } } cout << dp[N][W] << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) \ ; \ for (long long i = 0; i < (n); i++) typedef long long ll; typedef vector<long long> vl; const ll INF = 100000000009; ll dp[200][200000]; template <class T> inline bool chmax(T &x, T y) { if (x < y) { x = y; return true; } else return false; } int main() { ll N, W; cin >> N >> W; vl n(N); vl w(N); rep(i, N) cin >> w[i] >> n[i]; rep(i, N + 10) { rep(j, W + 10) { dp[i][j] = 0; } } rep(i, N) { rep(j, W + 1) { if (j >= w[i]) dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + n[i]); else dp[i + 1][j] = dp[i][j]; } } cout << dp[N][W] << endl; }
replace
9
10
9
10
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long int dp[101][10001]; int n; int weight[200001]; int value[200001]; int rec(int idx, int knap) { if (dp[idx][knap] != -1) return dp[idx][knap]; if (idx == n) return 0; if (knap < 0) return 0; // if(knap==0) return 0; // cout<<max(rec(idx+1,knap),(value[idx]+rec(idx+1,knap-weight[idx]))); // cout<<" "<<"\n"; if (weight[idx] > knap) return dp[idx][knap] = rec(idx + 1, knap); return dp[idx][knap] = max(rec(idx + 1, knap), (value[idx] + rec(idx + 1, knap - weight[idx]))); } signed main() { for (int i = 0; i < 101; i++) { for (int j = 0; j < 100001; j++) dp[i][j] = -1; } int w; cin >> n >> w; for (int i = 0; i < n; i++) { cin >> weight[i] >> value[i]; } // cout<<w; cout << rec(0, w); }
#include <bits/stdc++.h> using namespace std; #define int long long int dp[101][100001]; int n; int weight[200001]; int value[200001]; int rec(int idx, int knap) { if (dp[idx][knap] != -1) return dp[idx][knap]; if (idx == n) return 0; if (knap < 0) return 0; // if(knap==0) return 0; // cout<<max(rec(idx+1,knap),(value[idx]+rec(idx+1,knap-weight[idx]))); // cout<<" "<<"\n"; if (weight[idx] > knap) return dp[idx][knap] = rec(idx + 1, knap); return dp[idx][knap] = max(rec(idx + 1, knap), (value[idx] + rec(idx + 1, knap - weight[idx]))); } signed main() { for (int i = 0; i < 101; i++) { for (int j = 0; j < 100001; j++) dp[i][j] = -1; } int w; cin >> n >> w; for (int i = 0; i < n; i++) { cin >> weight[i] >> value[i]; } // cout<<w; cout << rec(0, w); }
replace
3
4
3
4
0
p03163
C++
Runtime Error
#include <bits/stdc++.h> #define int long long using namespace std; int wt[105]; int val[105]; int ans[100][100005]; int N, w; int ks(int n, int c) { if (ans[n][c] != -1) return ans[n][c]; if (n == 0 or c == 0) return 0; if (wt[n - 1] > c) return ks(n - 1, c); int a = ks(n - 1, c); int b = val[n - 1] + ks(n - 1, c - wt[n - 1]); ans[n][c] = max(a, b); return ans[n][c]; } void solve() { cin >> N >> w; for (int i = 0; i < N; ++i) { cin >> wt[i] >> val[i]; } for (int i = 0; i < N + 1; ++i) for (int j = 0; j < w + 1; ++j) ans[i][j] = -1; cout << ks(N, w); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int t = 1; // cin >> t; while (t--) { solve(); } }
#include <bits/stdc++.h> #define int long long using namespace std; int wt[105]; int val[105]; int ans[105][100005]; int N, w; int ks(int n, int c) { if (ans[n][c] != -1) return ans[n][c]; if (n == 0 or c == 0) return 0; if (wt[n - 1] > c) return ks(n - 1, c); int a = ks(n - 1, c); int b = val[n - 1] + ks(n - 1, c - wt[n - 1]); ans[n][c] = max(a, b); return ans[n][c]; } void solve() { cin >> N >> w; for (int i = 0; i < N; ++i) { cin >> wt[i] >> val[i]; } for (int i = 0; i < N + 1; ++i) for (int j = 0; j < w + 1; ++j) ans[i][j] = -1; cout << ks(N, w); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int t = 1; // cin >> t; while (t--) { solve(); } }
replace
5
6
5
6
0
p03163
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long int lli; vector<int> wei; vector<int> val; lli dp[101][100001]; lli knapsack(lli n, lli w) { if (n == 0 || w == 0) dp[n][w] = 0; if (dp[n][w] != -1) return dp[n][w]; if (wei[n - 1] > w) return knapsack(n - 1, w); lli x1 = val[n - 1] + knapsack(n - 1, w - wei[n - 1]); lli x2 = knapsack(n - 1, w); return max(x1, x2); } int main() { lli n, w, wi, vi; cin >> n >> w; for (lli i = 0; i < n; i++) { cin >> wi >> vi; wei.push_back(wi); val.push_back(vi); } for (int i = 0; i < 101; i++) for (int j = 0; j < 100001; j++) dp[i][j] = -1; cout << knapsack(n, w) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int lli; vector<int> wei; vector<int> val; lli dp[101][100001]; lli knapsack(lli n, lli w) { if (n == 0 || w == 0) dp[n][w] = 0; if (dp[n][w] != -1) return dp[n][w]; if (wei[n - 1] > w) return knapsack(n - 1, w); lli x1 = val[n - 1] + knapsack(n - 1, w - wei[n - 1]); lli x2 = knapsack(n - 1, w); dp[n][w] = max(x1, x2); return dp[n][w]; } int main() { lli n, w, wi, vi; cin >> n >> w; for (lli i = 0; i < n; i++) { cin >> wi >> vi; wei.push_back(wi); val.push_back(vi); } for (int i = 0; i < 101; i++) for (int j = 0; j < 100001; j++) dp[i][j] = -1; cout << knapsack(n, w) << endl; return 0; }
replace
23
24
23
25
TLE
p03163
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; long long dp[105][1000005]; int N, W, w[105], v[105]; int main() { cin >> N >> W; for (int i = 1; i <= N; i++) cin >> w[i] >> v[i]; for (int i = 1; i <= W; i++) { for (int j = 1; j <= N; j++) { dp[j][i] = max(dp[j - 1][i], dp[j][i]); if (i - w[j] >= 0) dp[j][i] = max(dp[j - 1][i], max(dp[j][i], dp[j - 1][i - w[j]] + v[j])); } } int ans = 0; cout << dp[W][N] << endl; }
#include <bits/stdc++.h> using namespace std; long long dp[105][1000005]; int N, W, w[105], v[105]; int main() { cin >> N >> W; for (int i = 1; i <= N; i++) cin >> w[i] >> v[i]; for (int i = 1; i <= W; i++) { for (int j = 1; j <= N; j++) { dp[j][i] = max(dp[j - 1][i], dp[j][i]); if (i - w[j] >= 0) dp[j][i] = max(dp[j - 1][i], max(dp[j][i], dp[j - 1][i - w[j]] + v[j])); } } int ans = 0; cout << dp[N][W] << endl; }
replace
17
18
17
18
-11