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
p02862
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, j, n) for (int i = (int)(j); i < (int)(n); i++) #define REP(i, j, n) for (int i = (int)(j); i <= (int)(n); i++) #define MOD 1000000007 #define int long long #define ALL(a) (a).begin(), (a).end() #define vi vector<int> #define vii vector<vi> #define pii pair<int, int> #define priq priority_queue<int> #define disup(A, key) distance(A.begin(), upper_bound(ALL(A), (int)(key))) #define dislow(A, key) distance(A.begin(), lower_bound(ALL(A), (int)(key))) #define tii tuple<int, int, int> const int MAX = 510000; long long fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } signed main() { int X, Y; cin >> X >> Y; int ans = 0; COMinit(); if ((X + Y) % 3 == 0 && 2 * Y >= X && 2 * X >= Y) { int A = (2 * Y - X) / 3; int B = (X - A) / 2; ans = COM(A + B, A); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, j, n) for (int i = (int)(j); i < (int)(n); i++) #define REP(i, j, n) for (int i = (int)(j); i <= (int)(n); i++) #define MOD 1000000007 #define int long long #define ALL(a) (a).begin(), (a).end() #define vi vector<int> #define vii vector<vi> #define pii pair<int, int> #define priq priority_queue<int> #define disup(A, key) distance(A.begin(), upper_bound(ALL(A), (int)(key))) #define dislow(A, key) distance(A.begin(), lower_bound(ALL(A), (int)(key))) #define tii tuple<int, int, int> const int MAX = 1000001; long long fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } signed main() { int X, Y; cin >> X >> Y; int ans = 0; COMinit(); if ((X + Y) % 3 == 0 && 2 * Y >= X && 2 * X >= Y) { int A = (2 * Y - X) / 3; int B = (X - A) / 2; ans = COM(A + B, A); } cout << ans << endl; }
replace
14
15
14
15
0
p02862
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; #define SZ(a) ((int)a.size()) #define ALL(a) a.begin(), a.end() #define MP make_pair #define PB push_back #define EB emplace_back #define fst first #define snd second void dout() { cerr << endl; } template <typename Head, typename... Tail> void dout(Head H, Tail... T) { cerr << H << ' '; dout(T...); } const int maxN = 1e6 + 47; const int mod = 1e9 + 7; int fact[maxN]; int mul(int a, int b) { return ((ll)a * b) % mod; } int binpow(int a, int n) { if (n == 0) return 1; else if (n % 2 == 0) return binpow(mul(a, a), n / 2); else return mul(a, binpow(a, n - 1)); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); fact[0] = 1; for (int i = 1; i < maxN; i++) { fact[i] = mul(fact[i - 1], i); } int x, y; cin >> x >> y; if ((x + y) % 3 != 0) { cout << 0; return 0; } int n = (x + y) / 3; int k = x - n; cout << mul(fact[n], binpow(mul(fact[k], fact[n - k]), mod - 2)); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; #define SZ(a) ((int)a.size()) #define ALL(a) a.begin(), a.end() #define MP make_pair #define PB push_back #define EB emplace_back #define fst first #define snd second void dout() { cerr << endl; } template <typename Head, typename... Tail> void dout(Head H, Tail... T) { cerr << H << ' '; dout(T...); } const int maxN = 1e6 + 47; const int mod = 1e9 + 7; int fact[maxN]; int mul(int a, int b) { return ((ll)a * b) % mod; } int binpow(int a, int n) { if (n == 0) return 1; else if (n % 2 == 0) return binpow(mul(a, a), n / 2); else return mul(a, binpow(a, n - 1)); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); fact[0] = 1; for (int i = 1; i < maxN; i++) { fact[i] = mul(fact[i - 1], i); } int x, y; cin >> x >> y; if ((x + y) % 3 != 0) { cout << 0; return 0; } int n = (x + y) / 3; int k = x - n; if (k >= 0 && k <= n) cout << mul(fact[n], binpow(mul(fact[k], fact[n - k]), mod - 2)); else cout << 0; return 0; }
replace
50
51
50
54
0
p02862
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, s, n) for (ll i = (s); i < (n); i++) #define rep0(i, n) rep(i, 0, n) #define rep1(i, n) rep(i, 1, n + 1) #define repR(i, s, n) for (ll i = (n - 1); i >= (s); i--) #define repR0(i, n) repR(i, 0, n) #define repR1(i, n) repR(i, 1, n + 1) #define BR "\n" #define SP " " #define SHOW(x) \ for (int i = 0; i < x.size(); i++) { \ cout << x[i] << SP; \ } \ cout << BR; #define SHOW2(x) \ for (int j = 0; j < x.size(); j++) { \ SHOW(x[j]); \ } \ cout << BR; #define fcout cout << fixed << setprecision(18) const ll MAX = 2e6 + 1; ll MOD = 1e9 + 7; ll INV[MAX] = {}, FACTORIAL[MAX] = {}, FACTORIAL_INV[MAX] = {}; void calc_init() { INV[1] = 1; FACTORIAL[0] = FACTORIAL[1] = 1; FACTORIAL_INV[0] = FACTORIAL_INV[1] = 1; rep(i, 2, MAX + 1) { ll q = MOD / i, r = MOD % i; INV[i] = (-INV[r] * q) % MOD; INV[i] = (INV[i] + MOD) % MOD; FACTORIAL[i] = (i * FACTORIAL[i - 1]) % MOD; FACTORIAL_INV[i] = (INV[i] * FACTORIAL_INV[i - 1]) % MOD; } } ll nCk(ll n, ll k) { ll ans = FACTORIAL[n]; ans = (ans * FACTORIAL_INV[k]) % MOD; ans = (ans * FACTORIAL_INV[n - k]) % MOD; return ans; } int main() { ll X, Y; cin >> X >> Y; if ((X + Y) % 3 != 0) { cout << 0 << BR; return 0; } calc_init(); ll n = (X + Y) / 3; ll k = X - n; if (k < 0) { cout << 0 << BR; return 0; } cout << nCk(n, k) << BR; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, s, n) for (ll i = (s); i < (n); i++) #define rep0(i, n) rep(i, 0, n) #define rep1(i, n) rep(i, 1, n + 1) #define repR(i, s, n) for (ll i = (n - 1); i >= (s); i--) #define repR0(i, n) repR(i, 0, n) #define repR1(i, n) repR(i, 1, n + 1) #define BR "\n" #define SP " " #define SHOW(x) \ for (int i = 0; i < x.size(); i++) { \ cout << x[i] << SP; \ } \ cout << BR; #define SHOW2(x) \ for (int j = 0; j < x.size(); j++) { \ SHOW(x[j]); \ } \ cout << BR; #define fcout cout << fixed << setprecision(18) const ll MAX = 2e6 + 1; ll MOD = 1e9 + 7; ll INV[MAX] = {}, FACTORIAL[MAX] = {}, FACTORIAL_INV[MAX] = {}; void calc_init() { INV[1] = 1; FACTORIAL[0] = FACTORIAL[1] = 1; FACTORIAL_INV[0] = FACTORIAL_INV[1] = 1; rep(i, 2, MAX + 1) { ll q = MOD / i, r = MOD % i; INV[i] = (-INV[r] * q) % MOD; INV[i] = (INV[i] + MOD) % MOD; FACTORIAL[i] = (i * FACTORIAL[i - 1]) % MOD; FACTORIAL_INV[i] = (INV[i] * FACTORIAL_INV[i - 1]) % MOD; } } ll nCk(ll n, ll k) { ll ans = FACTORIAL[n]; ans = (ans * FACTORIAL_INV[k]) % MOD; ans = (ans * FACTORIAL_INV[n - k]) % MOD; return ans; } int main() { ll X, Y; cin >> X >> Y; if ((X + Y) % 3 != 0) { cout << 0 << BR; return 0; } calc_init(); ll n = (X + Y) / 3; ll k = X - n; if (X < n || Y < n) { cout << 0 << BR; return 0; } cout << nCk(n, k) << BR; return 0; }
replace
62
63
62
63
0
p02862
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = unsigned long long; using P = pair<long double, long double>; const ll INF64 = 1LL << 60; const int INF = 1 << 29; const ll MOD = 1000000007; const int CASES = 3; // 負の数にも対応した mod (a = -11 とかでも OK) inline long long mod(long long a, long long m) { long long res = a % m; if (res < 0) res += m; return res; } // 拡張 Euclid の互除法 long long extGCD(long long a, long long b, long long &p, long long &q) { if (b == 0) { p = 1; q = 0; return a; } long long d = extGCD(b, a % b, q, p); q -= a / b * p; return d; } // 逆元計算 (ここでは a と m が互いに素であることが必要) long long modinv(long long a, long long m) { long long x, y; extGCD(a, m, x, y); return mod(x, m); // 気持ち的には x % m だが、x が負かもしれないので } ll com(const ll n, const ll k) { ll ue = 1, sita = 1; for (ll i = 0; i < k; ++i) { ue *= (n - i); ue %= MOD; sita *= (i + 1); sita %= MOD; } return ue * modinv(sita, MOD) % MOD; } void solve() { ll x, y; cin >> x >> y; ll sum = x + y; if (sum % 3 != 0) { cout << 0 << endl; return; } ll n = sum / 3; cout << com(n, x - n) << endl; return; } int main() { #ifdef MYLOCAL vector<string> filenames{"input1.txt", "input2.txt", "input3.txt", "input4.txt"}; cout << "------------\n"; for (int i = 0; i < CASES; ++i) { ifstream in(filenames[i]); cin.rdbuf(in.rdbuf()); solve(); cout << "------------\n"; } #else solve(); #endif return 0; }
#include <bits/stdc++.h> using namespace std; using ll = unsigned long long; using P = pair<long double, long double>; const ll INF64 = 1LL << 60; const int INF = 1 << 29; const ll MOD = 1000000007; const int CASES = 3; // 負の数にも対応した mod (a = -11 とかでも OK) inline long long mod(long long a, long long m) { long long res = a % m; if (res < 0) res += m; return res; } // 拡張 Euclid の互除法 long long extGCD(long long a, long long b, long long &p, long long &q) { if (b == 0) { p = 1; q = 0; return a; } long long d = extGCD(b, a % b, q, p); q -= a / b * p; return d; } // 逆元計算 (ここでは a と m が互いに素であることが必要) long long modinv(long long a, long long m) { long long x, y; extGCD(a, m, x, y); return mod(x, m); // 気持ち的には x % m だが、x が負かもしれないので } ll com(const ll n, const ll k) { ll ue = 1, sita = 1; for (ll i = 0; i < k; ++i) { ue *= (n - i); ue %= MOD; sita *= (i + 1); sita %= MOD; } return ue * modinv(sita, MOD) % MOD; } void solve() { ll x, y; cin >> x >> y; ll sum = x + y; if (sum % 3 != 0) { cout << 0 << endl; return; } ll n = sum / 3; ll k = min(x - n, n - (x - n)); cout << com(n, k) << endl; return; } int main() { #ifdef MYLOCAL vector<string> filenames{"input1.txt", "input2.txt", "input3.txt", "input4.txt"}; cout << "------------\n"; for (int i = 0; i < CASES; ++i) { ifstream in(filenames[i]); cin.rdbuf(in.rdbuf()); solve(); cout << "------------\n"; } #else solve(); #endif return 0; }
replace
60
62
60
62
TLE
p02862
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; #define rep(i, n) for (ll i = 0; i < n; i++) #define REP(i, n) for (ll i = 1; i < n + 1; i++) #define MAX 510000 #define MOD 1000000007 // fac: 階乗、finv: 階乗の逆元(invの積)、inv: // ある数の逆元(フェルマーの小定理より) ll fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void combModInit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (ll i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 ll comb(ll n, ll k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } int main() { ll X, Y; cin >> X >> Y; combModInit(); if (Y > 2 * X || 2 * Y < X || (X + Y) % 3 != 0) { cout << 0 << endl; return 0; } ll moves = (X + Y) / 3; ll up = X - moves; ll ans = comb(moves, up); cout << ans << endl; return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; #define rep(i, n) for (ll i = 0; i < n; i++) #define REP(i, n) for (ll i = 1; i < n + 1; i++) #define MAX 5100000 #define MOD 1000000007 // fac: 階乗、finv: 階乗の逆元(invの積)、inv: // ある数の逆元(フェルマーの小定理より) ll fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void combModInit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (ll i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 ll comb(ll n, ll k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } int main() { ll X, Y; cin >> X >> Y; combModInit(); if (Y > 2 * X || 2 * Y < X || (X + Y) % 3 != 0) { cout << 0 << endl; return 0; } ll moves = (X + Y) / 3; ll up = X - moves; ll ans = comb(moves, up); cout << ans << endl; return 0; }
replace
17
18
17
18
0
p02862
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define mp make_pair #define pii pair<int, int> #define pb push_back #define r1 rt << 1 #define r2 rt << 1 | 1 #define fi first #define se second #define ri register int #define rep(i, a, b) for (ri i = (a); i <= (b); ++i) #define rep2(i, a, b, c) for (ri i = (a); i <= (b); i += (c)) #define REP(i, a, b) for (ri i = (a); i >= (b); --i) #define REP2(i, a, b, c) for (ri i = (a); i >= (b); i -= (c)) using namespace std; inline int read() { ri x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ '0'), c = getchar(); return x * f; } inline ll readll() { ll x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ '0'), c = getchar(); return x * f; } inline void write(ll x) { if (x < 0) putchar('-'), x = -x; if (x > 9) write(x / 10); putchar(x % 10 + '0'); } const int p = 1e9 + 7, N = 1e6 + 5; int n, m, fac[N], inv[N]; inline int C(ri x, ri y) { if (x < y) return 0; return (ll)fac[x] * inv[y] % p * inv[x - y] % p; } inline int ksm(ri x, ri y) { ri z = 1; while (y) { if (y & 1) z = (ll)z * x % p; x = (ll)x * x % p; y >>= 1; } return z; } int main() { n = read(); m = read(); if ((n + m) % 3 != 0) { cout << 0; return 0; } ri xy = (n + m) / 3; ri x = m - xy; fac[0] = 1; rep(i, 1, xy) fac[i] = (ll)fac[i - 1] * i % p; inv[xy] = ksm(fac[xy], p - 2); REP(i, xy, 1) inv[i - 1] = (ll)inv[i] * i % p; printf("%d\n", C(xy, x)); return 0; }
#include <bits/stdc++.h> #define ll long long #define mp make_pair #define pii pair<int, int> #define pb push_back #define r1 rt << 1 #define r2 rt << 1 | 1 #define fi first #define se second #define ri register int #define rep(i, a, b) for (ri i = (a); i <= (b); ++i) #define rep2(i, a, b, c) for (ri i = (a); i <= (b); i += (c)) #define REP(i, a, b) for (ri i = (a); i >= (b); --i) #define REP2(i, a, b, c) for (ri i = (a); i >= (b); i -= (c)) using namespace std; inline int read() { ri x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ '0'), c = getchar(); return x * f; } inline ll readll() { ll x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ '0'), c = getchar(); return x * f; } inline void write(ll x) { if (x < 0) putchar('-'), x = -x; if (x > 9) write(x / 10); putchar(x % 10 + '0'); } const int p = 1e9 + 7, N = 1e6 + 5; int n, m, fac[N], inv[N]; inline int C(ri x, ri y) { if (x < y) return 0; return (ll)fac[x] * inv[y] % p * inv[x - y] % p; } inline int ksm(ri x, ri y) { ri z = 1; while (y) { if (y & 1) z = (ll)z * x % p; x = (ll)x * x % p; y >>= 1; } return z; } int main() { n = read(); m = read(); if ((n + m) % 3 != 0) { cout << 0; return 0; } ri xy = (n + m) / 3; ri x = m - xy, y = n - xy; if (x < 0 || y < 0) { cout << 0; return 0; } fac[0] = 1; rep(i, 1, xy) fac[i] = (ll)fac[i - 1] * i % p; inv[xy] = ksm(fac[xy], p - 2); REP(i, xy, 1) inv[i - 1] = (ll)inv[i] * i % p; printf("%d\n", C(xy, x)); return 0; }
replace
72
73
72
77
0
p02862
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define _GLIBCXX_DEBUG // これつけるとA[N]でもいいらしい // for文のマクロ #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define big 1000000007 const int MOD = 1000000007; const int MAX = 510000; long long fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } int main() { COMinit(); int x, y; cin >> x >> y; int n, m; n = 2 * y - x; m = 2 * x - y; // cout<<n<<" "<<m<<endl; int64_t ans; if (n % 3 != 0 || n < 0 || m % 3 != 0 || m < 0) { ans = 0; } else { n /= 3, m /= 3; ans = COM(m + n, n); // m+nCnを求めればいい } // cout<<"Power(m+n,n)="<<power(m+n,m,big)<<endl; cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define _GLIBCXX_DEBUG // これつけるとA[N]でもいいらしい // for文のマクロ #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define big 1000000007 const int MOD = 1000000007; const int MAX = 1000000; long long fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } int main() { COMinit(); int x, y; cin >> x >> y; int n, m; n = 2 * y - x; m = 2 * x - y; // cout<<n<<" "<<m<<endl; int64_t ans; if (n % 3 != 0 || n < 0 || m % 3 != 0 || m < 0) { ans = 0; } else { n /= 3, m /= 3; ans = COM(m + n, n); // m+nCnを求めればいい } // cout<<"Power(m+n,n)="<<power(m+n,m,big)<<endl; cout << ans << endl; }
replace
8
9
8
9
0
p02862
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define rep(i, a, b) for (ll i = a; i < b; i++) #define rrep(i, a, b) for (ll i = a; i >= b; i--) #define fore(i, a) for (auto &i : a) #define all(x) (x).begin(), (x).end() // 2項定理(MOD使用) const ll MAX = 510000; const ll MOD = 1000000007; long long fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (ll i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(ll n, ll k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } int main(void) { vector<ll> len; pair<ll, ll> pre; pre.first = 0; pre.second = 0; ll x, y; cin >> x >> y; len.push_back(1); COMinit(); ll a = 0, b = 0; ll n = 0; rep(i, 0, 1000000) { a += 2; b += 1; n++; if (a - x == y - b) { cout << COM(n, a - x) << endl; return 0; } } cout << 0 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define rep(i, a, b) for (ll i = a; i < b; i++) #define rrep(i, a, b) for (ll i = a; i >= b; i--) #define fore(i, a) for (auto &i : a) #define all(x) (x).begin(), (x).end() // 2項定理(MOD使用) const ll MAX = 1000000; const ll MOD = 1000000007; long long fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (ll i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(ll n, ll k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } int main(void) { vector<ll> len; pair<ll, ll> pre; pre.first = 0; pre.second = 0; ll x, y; cin >> x >> y; len.push_back(1); COMinit(); ll a = 0, b = 0; ll n = 0; rep(i, 0, 1000000) { a += 2; b += 1; n++; if (a - x == y - b) { cout << COM(n, a - x) << endl; return 0; } } cout << 0 << endl; return 0; }
replace
10
11
10
11
0
p02862
C++
Runtime Error
// Author: getharshkumar #include <bits/stdc++.h> using namespace std; #define ll int64_t const ll M = 1e9 + 7, N = 1e6 + 5; ll fac[N]; ll power(ll x, ll y) { ll res = 1; x %= M; while (y) { if (y % 2) res = (res * x) % M; x = (x * x) % M; y /= 2; } return res; } ll ncr(ll n, ll r) { if (r == 0) return 1; return ((((fac[n] * power(fac[r], M - 2)) % M) * power(fac[n - r], M - 2)) % M) % M; } int main() { fac[0] = 1; for (ll i = 1; i < N; i++) fac[i] = (fac[i - 1] * i) % M; ll x, y; cin >> x >> y; ll a = (x + y) / 3; if ((x + y) % 3 || y < a) cout << 0; else cout << ncr(a, y - a); return 0; }
// Author: getharshkumar #include <bits/stdc++.h> using namespace std; #define ll int64_t const ll M = 1e9 + 7, N = 1e6 + 5; ll fac[N]; ll power(ll x, ll y) { ll res = 1; x %= M; while (y) { if (y % 2) res = (res * x) % M; x = (x * x) % M; y /= 2; } return res; } ll ncr(ll n, ll r) { if (r == 0) return 1; return ((((fac[n] * power(fac[r], M - 2)) % M) * power(fac[n - r], M - 2)) % M) % M; } int main() { fac[0] = 1; for (ll i = 1; i < N; i++) fac[i] = (fac[i - 1] * i) % M; ll x, y; cin >> x >> y; ll a = (x + y) / 3; if ((x + y) % 3 || y > 2 * a || y < a) cout << 0; else cout << ncr(a, y - a); return 0; }
replace
32
33
32
33
0
p02862
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define mod 1000000007ll using namespace std; ll fact[2000006]; ll bigmod(ll b, ll p) { if (!p) return 1ll; ll ret = bigmod(b, p / 2); ret = (ret * ret) % mod; if (p & 1ll) return ret = (ret * b) % mod; return ret; } ll ncr(ll n, ll r) { ll nume = fact[n]; ll deno = (fact[n - r] * fact[r]) % mod; return (nume * bigmod(deno, mod - 2)) % mod; } int main() { fact[0] = 1ll; for (ll i = 1; i <= 2000000; i++) fact[i] = (fact[i - 1] * i) % mod; int n, m, cnt = 0; scanf("%d %d", &n, &m); if (n < m) swap(n, m); int tot = (n + m) / 3; while (2 * n > m) n--, n--, m--, cnt++; if (2 * n == m) printf("%lld\n", ncr(tot, cnt)); else puts("0"); return 0; }
#include <bits/stdc++.h> #define ll long long #define mod 1000000007ll using namespace std; ll fact[2000006]; ll bigmod(ll b, ll p) { if (!p) return 1ll; ll ret = bigmod(b, p / 2); ret = (ret * ret) % mod; if (p & 1ll) return ret = (ret * b) % mod; return ret; } ll ncr(ll n, ll r) { ll nume = fact[n]; ll deno = (fact[n - r] * fact[r]) % mod; return (nume * bigmod(deno, mod - 2)) % mod; } int main() { fact[0] = 1ll; for (ll i = 1; i <= 2000000; i++) fact[i] = (fact[i - 1] * i) % mod; int n, m, cnt = 0; scanf("%d %d", &n, &m); if (n < m) swap(n, m); int tot = (n + m) / 3; while (n > 0 && m > 0 && 2 * n > m) n--, n--, m--, cnt++; if (2 * n == m) printf("%lld\n", ncr(tot, cnt)); else puts("0"); return 0; }
replace
28
29
28
29
0
p02862
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; vector<long long> fact, fact_inv, inv; /* init_nCk :二項係数のための前処理 計算量:O(n) */ void init_nCk(int SIZE) { fact.resize(SIZE + 5); fact_inv.resize(SIZE + 5); inv.resize(SIZE + 5); fact[0] = fact[1] = 1; fact_inv[0] = fact_inv[1] = 1; inv[1] = 1; for (int i = 2; i < SIZE + 5; i++) { fact[i] = fact[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; fact_inv[i] = fact_inv[i - 1] * inv[i] % MOD; } } /* nCk :MODでの二項係数を求める(前処理 int_nCk が必要) 計算量:O(1) */ long long nCk(int n, int k) { assert(!(n < k)); assert(!(n < 0 || k < 0)); return fact[n] * (fact_inv[k] * fact_inv[n - k] % MOD) % MOD; } int main() { int x, y; cin >> x >> y; if ((x + y) % 3 != 0) cout << "0" << endl; else { int a, b; a = (2 * y - x) / 3; b = (2 * x - y) / 3; init_nCk(10010010); cout << nCk(a + b, a) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; vector<long long> fact, fact_inv, inv; /* init_nCk :二項係数のための前処理 計算量:O(n) */ void init_nCk(int SIZE) { fact.resize(SIZE + 5); fact_inv.resize(SIZE + 5); inv.resize(SIZE + 5); fact[0] = fact[1] = 1; fact_inv[0] = fact_inv[1] = 1; inv[1] = 1; for (int i = 2; i < SIZE + 5; i++) { fact[i] = fact[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; fact_inv[i] = fact_inv[i - 1] * inv[i] % MOD; } } /* nCk :MODでの二項係数を求める(前処理 int_nCk が必要) 計算量:O(1) */ long long nCk(int n, int k) { assert(!(n < k)); assert(!(n < 0 || k < 0)); return fact[n] * (fact_inv[k] * fact_inv[n - k] % MOD) % MOD; } int main() { int x, y; cin >> x >> y; if ((x + y) % 3 != 0) cout << "0" << endl; else { int a, b; a = (2 * y - x) / 3; b = (2 * x - y) / 3; if (a < 0 || b < 0) cout << "0" << endl; else { init_nCk(1001001); cout << nCk(a + b, a) << endl; } } return 0; }
replace
38
40
38
44
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p02862
C++
Runtime Error
#include <algorithm> #include <iostream> #include <iterator> #include <vector> using namespace std; namespace mod { template <class T, int M> class MInt; template <class T, int M> MInt<T, M> Pow(const MInt<T, M> &base, T exp); template <class T, int M> MInt<T, M> Inv(const MInt<T, M> &n); template <class T, int M> class MInt { T n_; public: MInt() : n_(0) {} MInt(T n) : n_((n % M + M) % M) {} MInt &operator+=(const MInt &rhs) { n_ += rhs.n_; if (n_ >= M) { n_ -= M; } return *this; } MInt &operator-=(const MInt &rhs) { n_ += M - rhs.n_; if (n_ >= M) { n_ -= M; } return *this; } MInt &operator*=(const MInt &rhs) { n_ = n_ * rhs.n_ % M; return *this; } MInt &operator/=(const MInt &rhs) { return (*this) *= Inv(rhs); } T Get() { return n_; } }; template <class T, int M> MInt<T, M> operator-(const MInt<T, M> &lhs, const MInt<T, M> &rhs) { MInt<T, M> res(lhs); return res -= rhs; } template <class T, int M> MInt<T, M> operator+(const MInt<T, M> &lhs, const MInt<T, M> &rhs) { MInt<T, M> res(lhs); return res += rhs; } template <class T, int M> MInt<T, M> operator*(const MInt<T, M> &lhs, const MInt<T, M> &rhs) { MInt<T, M> res(lhs); return res *= rhs; } template <class T, int M> MInt<T, M> operator/(const MInt<T, M> &lhs, const MInt<T, M> &rhs) { MInt<T, M> res(lhs); return res /= rhs; } template <class T, int M> MInt<T, M> Pow(const MInt<T, M> &base, T exp) { if (exp == 0) { return 1; } auto a = Pow(base, exp / 2); a *= a; if (exp % 2 == 1) { a *= base; } return a; } template <class T, int M> MInt<T, M> Inv(const MInt<T, M> &n) { return Pow(n, static_cast<T>(M - 2)); } } // namespace mod const int md = 1e9 + 7; using MInt = mod::MInt<long long, md>; class BinCoef { vector<MInt> f; public: BinCoef(int n) : f(n + 1) { f[0] = f[1] = 1; for (int i = 2; i <= n; i++) { f[i] = MInt(i) * f[i - 1]; } } MInt NCM(int n, int m) { return f[n] * mod::Inv(f[m] * f[n - m]); } }; int main(int argc, const char *argv[]) { int x, y; cin >> x >> y; if ((x + y) % 3 != 0) { cout << "0\n"; return 0; } // (i + 1, j + 2), (i + 2, j + 1) に移動する回数 int m = (2 * x - y) / 3; int n = x - 2 * m; BinCoef bc((m + n) * 2); cout << bc.NCM(m + n, n).Get() << "\n"; return 0; }
#include <algorithm> #include <iostream> #include <iterator> #include <vector> using namespace std; namespace mod { template <class T, int M> class MInt; template <class T, int M> MInt<T, M> Pow(const MInt<T, M> &base, T exp); template <class T, int M> MInt<T, M> Inv(const MInt<T, M> &n); template <class T, int M> class MInt { T n_; public: MInt() : n_(0) {} MInt(T n) : n_((n % M + M) % M) {} MInt &operator+=(const MInt &rhs) { n_ += rhs.n_; if (n_ >= M) { n_ -= M; } return *this; } MInt &operator-=(const MInt &rhs) { n_ += M - rhs.n_; if (n_ >= M) { n_ -= M; } return *this; } MInt &operator*=(const MInt &rhs) { n_ = n_ * rhs.n_ % M; return *this; } MInt &operator/=(const MInt &rhs) { return (*this) *= Inv(rhs); } T Get() { return n_; } }; template <class T, int M> MInt<T, M> operator-(const MInt<T, M> &lhs, const MInt<T, M> &rhs) { MInt<T, M> res(lhs); return res -= rhs; } template <class T, int M> MInt<T, M> operator+(const MInt<T, M> &lhs, const MInt<T, M> &rhs) { MInt<T, M> res(lhs); return res += rhs; } template <class T, int M> MInt<T, M> operator*(const MInt<T, M> &lhs, const MInt<T, M> &rhs) { MInt<T, M> res(lhs); return res *= rhs; } template <class T, int M> MInt<T, M> operator/(const MInt<T, M> &lhs, const MInt<T, M> &rhs) { MInt<T, M> res(lhs); return res /= rhs; } template <class T, int M> MInt<T, M> Pow(const MInt<T, M> &base, T exp) { if (exp == 0) { return 1; } auto a = Pow(base, exp / 2); a *= a; if (exp % 2 == 1) { a *= base; } return a; } template <class T, int M> MInt<T, M> Inv(const MInt<T, M> &n) { return Pow(n, static_cast<T>(M - 2)); } } // namespace mod const int md = 1e9 + 7; using MInt = mod::MInt<long long, md>; class BinCoef { vector<MInt> f; public: BinCoef(int n) : f(n + 1) { f[0] = f[1] = 1; for (int i = 2; i <= n; i++) { f[i] = MInt(i) * f[i - 1]; } } MInt NCM(int n, int m) { return f[n] * mod::Inv(f[m] * f[n - m]); } }; int main(int argc, const char *argv[]) { int x, y; cin >> x >> y; if ((x + y) % 3 != 0) { cout << "0\n"; return 0; } // (i + 1, j + 2), (i + 2, j + 1) に移動する回数 int m = (2 * x - y) / 3; int n = x - 2 * m; if (m < 0 || n < 0) { cout << "0\n"; return 0; } BinCoef bc(m + n); cout << bc.NCM(m + n, n).Get() << "\n"; return 0; }
replace
121
122
121
126
0
p02862
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define MOD 1000000007 #define MAX_P 510000 using namespace std; vector<ll> factn(MAX_P + 1); // n! = factn[i] void fact(ll n) { factn[1] = 1; for (ll i = 2; i <= n; i++) { factn[i] = factn[i - 1] * i % MOD; } return; } ll extgcd(ll a, ll b, ll &x, ll &y) { ll d = a; if (b != 0) { d = extgcd(b, a % b, y, x); y -= (a / b) * x; } else { x = 1; y = 0; } return d; } ll mod_inverse(ll a, ll m) { ll x, y; x = 1; y = 0; extgcd(a, m, x, y); return (m + x % m) % m; } // n! mod p ll mod_fact(ll n, ll p, ll &e) { e = 0; if (n == 0) return 1; ll res = mod_fact(n / p, p, e); e += n / p; if (n / p % 2 != 0) return res * (p - factn[n % p]) % p; return res * factn[n % p] % p; } // nCk %mod p ll mod_comb(ll n, ll k, ll p) { if (n < 0 || k < 0 || n < k) return 0; ll e1, e2, e3; e1 = e2 = e3 = 0; ll a1 = mod_fact(n, p, e1); ll a2 = mod_fact(k, p, e2); ll a3 = mod_fact((n - k), p, e3); if (e1 > e2 + e3) return 0; return a1 * mod_inverse(a2 * a3 % p, p) % p; } // X = 2a + 1b // Y = a +2b int main() { ios_base::sync_with_stdio(0); cin.tie(0); ll xx, yy; cin >> xx >> yy; ll mm = 0; ll nn = 0; bool flag = true; for (ll b = 0; 2 * b <= xx; b++) { ll a = xx - (2 * b); if (yy == (2 * a + b)) { nn = a; mm = b; flag = false; break; } } if (flag) { cout << 0 << endl; return 0; } fact(MAX_P); ll ans = 0; ans = mod_comb(nn + mm, nn, MOD); cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define ll long long #define MOD 1000000007 #define MAX_P 1100000 using namespace std; vector<ll> factn(MAX_P + 1); // n! = factn[i] void fact(ll n) { factn[1] = 1; for (ll i = 2; i <= n; i++) { factn[i] = factn[i - 1] * i % MOD; } return; } ll extgcd(ll a, ll b, ll &x, ll &y) { ll d = a; if (b != 0) { d = extgcd(b, a % b, y, x); y -= (a / b) * x; } else { x = 1; y = 0; } return d; } ll mod_inverse(ll a, ll m) { ll x, y; x = 1; y = 0; extgcd(a, m, x, y); return (m + x % m) % m; } // n! mod p ll mod_fact(ll n, ll p, ll &e) { e = 0; if (n == 0) return 1; ll res = mod_fact(n / p, p, e); e += n / p; if (n / p % 2 != 0) return res * (p - factn[n % p]) % p; return res * factn[n % p] % p; } // nCk %mod p ll mod_comb(ll n, ll k, ll p) { if (n < 0 || k < 0 || n < k) return 0; ll e1, e2, e3; e1 = e2 = e3 = 0; ll a1 = mod_fact(n, p, e1); ll a2 = mod_fact(k, p, e2); ll a3 = mod_fact((n - k), p, e3); if (e1 > e2 + e3) return 0; return a1 * mod_inverse(a2 * a3 % p, p) % p; } // X = 2a + 1b // Y = a +2b int main() { ios_base::sync_with_stdio(0); cin.tie(0); ll xx, yy; cin >> xx >> yy; ll mm = 0; ll nn = 0; bool flag = true; for (ll b = 0; 2 * b <= xx; b++) { ll a = xx - (2 * b); if (yy == (2 * a + b)) { nn = a; mm = b; flag = false; break; } } if (flag) { cout << 0 << endl; return 0; } fact(MAX_P); ll ans = 0; ans = mod_comb(nn + mm, nn, MOD); cout << ans << endl; return 0; }
replace
3
4
3
4
0
p02862
C++
Runtime Error
#include <algorithm> #include <cmath> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define All(v) (v).begin(), (v).end() int dy[4] = {-1, 0, 1, 0}; int dx[4] = {0, 1, 0, -1}; int Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}; const int mod = 1000000007; const int inf = mod * mod; const int d5 = 1000000; template <int mod> struct ModInt { int x; ModInt() : x(0) {} ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} ModInt &operator+=(const ModInt &p) { if ((x += p.x) >= mod) x -= mod; return *this; } ModInt &operator-=(const ModInt &p) { if ((x += mod - p.x) >= mod) x -= mod; return *this; } ModInt &operator*=(const ModInt &p) { x = (int)(1LL * x * p.x % mod); return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt operator-() const { return ModInt(-x); } ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } bool operator==(const ModInt &p) const { return x == p.x; } bool operator!=(const ModInt &p) const { return x != p.x; } ModInt inverse() const { int a = x, b = mod, u = 1, v = 0, t; while (b > 0) { t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } return ModInt(u); } ModInt pow(int e) { long long a = 1, p = x; while (e > 0) { if (e % 2 == 0) { p = (p * p) % mod; e /= 2; } else { a = (a * p) % mod; e--; } } return ModInt(a); } friend ostream &operator<<(ostream &os, const ModInt<mod> &p) { return os << p.x; } friend istream &operator>>(istream &is, ModInt<mod> &a) { long long x; is >> x; a = ModInt<mod>(x); return (is); } }; using modint = ModInt<mod>; modint f[d5]; void makef() { f[0] = 1; for (int i = 1; i < d5; i++) f[i] = f[i - 1] * i; } modint nCr(int n, int r) { if (n < r) return 0; return f[n] / (f[r] * f[n - r]); } signed main() { ios::sync_with_stdio(false); cin.tie(0); int x, y; cin >> x >> y; if ((x + y) % 3 != 0) { cout << 0 << endl; return 0; } int a = (2 * x - y) / 3; int b = (2 * y - x) / 3; makef(); cout << nCr(a + b, b) << endl; }
#include <algorithm> #include <cmath> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define All(v) (v).begin(), (v).end() int dy[4] = {-1, 0, 1, 0}; int dx[4] = {0, 1, 0, -1}; int Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}; const int mod = 1000000007; const int inf = mod * mod; const int d5 = 1000000; template <int mod> struct ModInt { int x; ModInt() : x(0) {} ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} ModInt &operator+=(const ModInt &p) { if ((x += p.x) >= mod) x -= mod; return *this; } ModInt &operator-=(const ModInt &p) { if ((x += mod - p.x) >= mod) x -= mod; return *this; } ModInt &operator*=(const ModInt &p) { x = (int)(1LL * x * p.x % mod); return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt operator-() const { return ModInt(-x); } ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } bool operator==(const ModInt &p) const { return x == p.x; } bool operator!=(const ModInt &p) const { return x != p.x; } ModInt inverse() const { int a = x, b = mod, u = 1, v = 0, t; while (b > 0) { t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } return ModInt(u); } ModInt pow(int e) { long long a = 1, p = x; while (e > 0) { if (e % 2 == 0) { p = (p * p) % mod; e /= 2; } else { a = (a * p) % mod; e--; } } return ModInt(a); } friend ostream &operator<<(ostream &os, const ModInt<mod> &p) { return os << p.x; } friend istream &operator>>(istream &is, ModInt<mod> &a) { long long x; is >> x; a = ModInt<mod>(x); return (is); } }; using modint = ModInt<mod>; modint f[d5]; void makef() { f[0] = 1; for (int i = 1; i < d5; i++) f[i] = f[i - 1] * i; } modint nCr(int n, int r) { if (n < r) return 0; else if (r < 0) return 0; return f[n] / (f[r] * f[n - r]); } signed main() { ios::sync_with_stdio(false); cin.tie(0); int x, y; cin >> x >> y; if ((x + y) % 3 != 0) { cout << 0 << endl; return 0; } int a = (2 * x - y) / 3; int b = (2 * y - x) / 3; makef(); cout << nCr(a + b, b) << endl; }
insert
109
109
109
111
0
p02862
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; #define int long long #define REP(i, n) for (int i = 0; i < (n); ++i) #define REPS(i, r, n) for (int i = (r); i < (n); ++i) #define REPR(i, n) for (int i = (n); i >= 0; --i) // from n to 0 #define REPRS(i, r, n) for (int i = (n); i >= (r); --i) // from n to r #define REPOBJ(itr, obj) \ for (auto itr = (obj).begin(); itr != (obj).end(); ++itr) #define REPROBJ(itr, obj) \ for (auto itr = (obj).rbegin(), e = (obj).rend(); itr != e; ++itr) #define COUTB(x) cout << (x) << "\n" #define COUTS(x) cout << (x) << " " #define PB push_back #define SORT(obj) sort((obj).begin(), (obj).end()) #define SORTR(obj) sort((obj).begin(), (obj).end(), greater<>()) #define ALL(obj) (obj).begin(), (obj).end() #define MOD 1000000007 #define PI (acos(-1)) int lcm(int, int); map<int, int> GetPrimeFactors(int); bool IsPrime(int); int Factorial(int); int npr(int, int); int ncr(int, int); const int MAX = 510000; ll fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } ll COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } /***** MAIN *****/ void Main() { int x, y; cin >> x >> y; if ((x + y) % 3 != 0) { cout << 0 << endl; return; } if (2 * x < y || 2 * y < x) { cout << 0 << endl; return; } int n = (2 * y - x) / 3; int m = (2 * x - y) / 3; COMinit(); cout << COM(m + n, n); cout << "\n"; return; } /***** MAIN *****/ signed main() { cin.tie(0); ios_base::sync_with_stdio(false); cout << fixed << setprecision(15); Main(); return 0; } int lcm(int a, int b) { return a / __gcd(a, b) * b; } map<int, int> GetPrimeFactors(int n) { map<int, int> mp; // dived by 2, 3, 5, ... for (int i = 2; i * i <= n; i += 2) { if (i == 4) i = 3; while (n % i == 0) { ++mp[i]; n /= i; } } if (n != 1) ++mp[n]; return mp; } bool IsPrime(int a) { if (a <= 1) return false; if (a == 2) return true; if (a % 2 == 0) return false; for (int i = 3; i * i <= a; i += 2) { if (a % i == 0) return false; } return true; } int Factorial(int n) { int ans = 1; while (n > 1) { ans *= n; --n; } return ans; } int npr(int n, int r) { return Factorial(n) / Factorial(n - r); } int ncr(int n, int r) { return npr(n, r) / Factorial(r); }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define int long long #define REP(i, n) for (int i = 0; i < (n); ++i) #define REPS(i, r, n) for (int i = (r); i < (n); ++i) #define REPR(i, n) for (int i = (n); i >= 0; --i) // from n to 0 #define REPRS(i, r, n) for (int i = (n); i >= (r); --i) // from n to r #define REPOBJ(itr, obj) \ for (auto itr = (obj).begin(); itr != (obj).end(); ++itr) #define REPROBJ(itr, obj) \ for (auto itr = (obj).rbegin(), e = (obj).rend(); itr != e; ++itr) #define COUTB(x) cout << (x) << "\n" #define COUTS(x) cout << (x) << " " #define PB push_back #define SORT(obj) sort((obj).begin(), (obj).end()) #define SORTR(obj) sort((obj).begin(), (obj).end(), greater<>()) #define ALL(obj) (obj).begin(), (obj).end() #define MOD 1000000007 #define PI (acos(-1)) int lcm(int, int); map<int, int> GetPrimeFactors(int); bool IsPrime(int); int Factorial(int); int npr(int, int); int ncr(int, int); const int MAX = 5100000; ll fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } ll COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } /***** MAIN *****/ void Main() { int x, y; cin >> x >> y; if ((x + y) % 3 != 0) { cout << 0 << endl; return; } if (2 * x < y || 2 * y < x) { cout << 0 << endl; return; } int n = (2 * y - x) / 3; int m = (2 * x - y) / 3; COMinit(); cout << COM(m + n, n); cout << "\n"; return; } /***** MAIN *****/ signed main() { cin.tie(0); ios_base::sync_with_stdio(false); cout << fixed << setprecision(15); Main(); return 0; } int lcm(int a, int b) { return a / __gcd(a, b) * b; } map<int, int> GetPrimeFactors(int n) { map<int, int> mp; // dived by 2, 3, 5, ... for (int i = 2; i * i <= n; i += 2) { if (i == 4) i = 3; while (n % i == 0) { ++mp[i]; n /= i; } } if (n != 1) ++mp[n]; return mp; } bool IsPrime(int a) { if (a <= 1) return false; if (a == 2) return true; if (a % 2 == 0) return false; for (int i = 3; i * i <= a; i += 2) { if (a % i == 0) return false; } return true; } int Factorial(int n) { int ans = 1; while (n > 1) { ans *= n; --n; } return ans; } int npr(int n, int r) { return Factorial(n) / Factorial(n - r); } int ncr(int n, int r) { return npr(n, r) / Factorial(r); }
replace
28
29
28
29
0
p02862
C++
Runtime Error
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace std; using namespace __gnu_pbds; const long long MOD = 1000000007LL; const long long INF = 1e18; int GCD(int a, int b) { return b == 0 ? a : GCD(b, a % b); } long long fast_exp(long long base, long long exp, long long mod = MOD) { long long tot = 1; for (; exp > 0; exp >>= 1) { if ((exp & 1) == 1) tot = tot * base % mod; base = base * base % mod; } return tot; } long long slow_mult(long long base, long long exp, long long mod = MOD) { long long tot = 0; for (; exp > 0; exp >>= 1) { if ((exp & 1) == 1) tot = (tot + base) % mod; base = base * 2 % mod; } return tot; } struct chash { static uint64_t splitmix64(uint64_t x) { x += 0x8e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf54876d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } size_t operator()(pair<uint64_t, uint64_t> x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x.first + FIXED_RANDOM) ^ (splitmix64(x.second + FIXED_RANDOM) >> 1); } }; long long fac[3000006]; int main() { cin.sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); int x, y; cin >> x >> y; fac[1] = fac[0] = 1; for (int i = 2; i <= 3000003; ++i) { fac[i] = fac[i - 1] * i % MOD; } long long tot = 0; for (int j, i = 0; i <= x; ++i) { j = x - 2 * i; if (i + j * 2 == y) { // choose(n + k, k) tot += fac[i + j] * fast_exp(fac[j], MOD - 2) % MOD * fast_exp(fac[i], MOD - 2) % MOD; } } cout << tot; return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace std; using namespace __gnu_pbds; const long long MOD = 1000000007LL; const long long INF = 1e18; int GCD(int a, int b) { return b == 0 ? a : GCD(b, a % b); } long long fast_exp(long long base, long long exp, long long mod = MOD) { long long tot = 1; for (; exp > 0; exp >>= 1) { if ((exp & 1) == 1) tot = tot * base % mod; base = base * base % mod; } return tot; } long long slow_mult(long long base, long long exp, long long mod = MOD) { long long tot = 0; for (; exp > 0; exp >>= 1) { if ((exp & 1) == 1) tot = (tot + base) % mod; base = base * 2 % mod; } return tot; } struct chash { static uint64_t splitmix64(uint64_t x) { x += 0x8e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf54876d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } size_t operator()(pair<uint64_t, uint64_t> x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x.first + FIXED_RANDOM) ^ (splitmix64(x.second + FIXED_RANDOM) >> 1); } }; long long fac[3000006]; int main() { cin.sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); int x, y; cin >> x >> y; fac[1] = fac[0] = 1; for (int i = 2; i <= 3000003; ++i) { fac[i] = fac[i - 1] * i % MOD; } long long tot = 0; for (int j, i = 0; i <= x; ++i) { j = x - 2 * i; if (i + j * 2 == y && j >= 0 && i >= 0) { // choose(n + k, k) tot += fac[i + j] * fast_exp(fac[j], MOD - 2) % MOD * fast_exp(fac[i], MOD - 2) % MOD; } } cout << tot; return 0; }
replace
69
70
69
70
0
p02862
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; constexpr ll MOD = 1e9 + 7; constexpr ll INF = 1ll << 60; #define FOR(i, a, b) for (ll i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(v) (v).begin(), (v).end() constexpr ll COMB_MAX = 1e6 + 10; vector<ll> fac(COMB_MAX); vector<ll> ifac(COMB_MAX); ll mpow(ll x, ll n) { ll ans = 1; while (n != 0) { if (n & 1) ans = ans * x % MOD; x = x * x % MOD; n = n >> 1; } return ans; } ll comb(ll a, ll b) { if (a == 0 && b == 0) return 1; if (a < b || a < 0) return 0; ll tmp = ifac[a - b] * ifac[b] % MOD; return tmp * fac[a] % MOD; } void init() { fac[0] = 1; ifac[0] = 1; for (ll i = 0; i < COMB_MAX; i++) { fac[i + 1] = fac[i] * (i + 1) % MOD; ifac[i + 1] = ifac[i] * mpow(i + 1, MOD - 2) % MOD; } } int main(int argc, char **argv) { ll H, W; cin >> H >> W; init(); ll res; if ((H + W) % 3 != 0) res = 0; else res = comb((W + H) / 3, H - (W + H) / 3); std::cout << res << std::endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; constexpr ll MOD = 1e9 + 7; constexpr ll INF = 1ll << 60; #define FOR(i, a, b) for (ll i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(v) (v).begin(), (v).end() constexpr ll COMB_MAX = 1e6 + 10; vector<ll> fac(COMB_MAX); vector<ll> ifac(COMB_MAX); ll mpow(ll x, ll n) { ll ans = 1; while (n != 0) { if (n & 1) ans = ans * x % MOD; x = x * x % MOD; n = n >> 1; } return ans; } ll comb(ll a, ll b) { if (a == 0 && b == 0) return 1; if (a < b || a < 0) return 0; ll tmp = ifac[a - b] * ifac[b] % MOD; return tmp * fac[a] % MOD; } void init() { fac[0] = 1; ifac[0] = 1; for (ll i = 0; i < COMB_MAX; i++) { fac[i + 1] = fac[i] * (i + 1) % MOD; ifac[i + 1] = ifac[i] * mpow(i + 1, MOD - 2) % MOD; } } int main(int argc, char **argv) { ll H, W; cin >> H >> W; init(); ll res; if ((H + W) % 3 != 0) res = 0; else if (H > W * 2 || W > H * 2) res = 0; else res = comb((W + H) / 3, H - (W + H) / 3); std::cout << res << std::endl; }
insert
52
52
52
54
0
p02862
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define ll int64_t #define rep(i, n) for (ll i = 0; i < n; i++) #define rrep(i, n) for (ll i = 1; i <= n; i++) #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define pb push_back #define mod 1000000007 #define vec vector #define dump(x) cerr << #x << "=" << x << endl #define Array vec<ll> using namespace std; bool compare_by_b(pair<string, ll> a, pair<string, ll> b) { if (a.second != b.second) return a.second < b.second; else return a.first < b.first; } bool my_compare(pair<string, ll> a, pair<string, ll> b) { if (a.first != b.first) return a.first < b.first; if (a.second != b.second) return a.second > b.second; else return true; } ll factorial(ll n) { ll x = 1; rrep(i, n)(x *= i) %= mod; return x; } ll gcd(ll a, ll b) { if (a % b == 0) return (b); else return (gcd(b, a % b)); } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } // 累乗だってさ ll modpow(ll a, ll n, ll mod1) { ll res = 1; while (n > 0) { if (n & 1) res = res * a % mod1; a = a * a % mod1; n >>= 1; } return res; } ll combup(ll n, ll r) { ll sum = 1; rrep(i, min(r, n - r))(sum *= (n - i + 1)) %= mod; return sum; } // 逆元だってさ ll modinv(ll a, ll mod1) { return modpow(a, mod1 - 2, mod1); } const ll MAX = 51000000; const ll MOD = 1000000007; ll fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (ll i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 ll COM(ll n, ll k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } void solve() {} int main() { cin.tie(0); ios::sync_with_stdio(false); ll x, y; ll left1 = 0, right1 = 0; cin >> x >> y; ll a = min(x, y), b = max(x, y); while (a > 0 && b > 0) { if (a > b) { b -= 1; a -= 2; right1++; } else if (a <= b) { b -= 2; a -= 1; left1++; } if ((a == 0 && b != 0) || (a != 0 && b == 0)) { cout << 0 << endl; return 0; } } ll n = (right1 + left1); COMinit(); cout << COM(n, right1) << endl; return 0; }
#include <bits/stdc++.h> #define ll int64_t #define rep(i, n) for (ll i = 0; i < n; i++) #define rrep(i, n) for (ll i = 1; i <= n; i++) #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define pb push_back #define mod 1000000007 #define vec vector #define dump(x) cerr << #x << "=" << x << endl #define Array vec<ll> using namespace std; bool compare_by_b(pair<string, ll> a, pair<string, ll> b) { if (a.second != b.second) return a.second < b.second; else return a.first < b.first; } bool my_compare(pair<string, ll> a, pair<string, ll> b) { if (a.first != b.first) return a.first < b.first; if (a.second != b.second) return a.second > b.second; else return true; } ll factorial(ll n) { ll x = 1; rrep(i, n)(x *= i) %= mod; return x; } ll gcd(ll a, ll b) { if (a % b == 0) return (b); else return (gcd(b, a % b)); } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } // 累乗だってさ ll modpow(ll a, ll n, ll mod1) { ll res = 1; while (n > 0) { if (n & 1) res = res * a % mod1; a = a * a % mod1; n >>= 1; } return res; } ll combup(ll n, ll r) { ll sum = 1; rrep(i, min(r, n - r))(sum *= (n - i + 1)) %= mod; return sum; } // 逆元だってさ ll modinv(ll a, ll mod1) { return modpow(a, mod1 - 2, mod1); } const ll MAX = 1000000; const ll MOD = 1000000007; ll fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (ll i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 ll COM(ll n, ll k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } void solve() {} int main() { cin.tie(0); ios::sync_with_stdio(false); ll x, y; ll left1 = 0, right1 = 0; cin >> x >> y; ll a = min(x, y), b = max(x, y); while (a > 0 && b > 0) { if (a > b) { b -= 1; a -= 2; right1++; } else if (a <= b) { b -= 2; a -= 1; left1++; } if ((a == 0 && b != 0) || (a != 0 && b == 0)) { cout << 0 << endl; return 0; } } ll n = (right1 + left1); COMinit(); cout << COM(n, right1) << endl; return 0; }
replace
65
66
65
66
TLE
p02862
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) typedef long long ll; ll mod = 1e9 + 7; ll inv(ll a) { ll n = mod - 2, ans = 1; while (n > 0) { if (n % 2) ans = ans * a % mod; n = n / 2; a = a * a % mod; } return ans; } ll A[2000001]; ll nCr(ll n, ll r) { return A[n] * inv(A[r]) % mod * inv(A[n - r]) % mod; } int main() { ll x, y; cin >> x >> y; if ((x + y) % 3) { cout << 0; return 0; } ll k = (2 * y - x) / 3, l = (2 * x - y) / 3; A[0] = 1; rep(i, 2000000) A[i + 1] = A[i] * (i + 1) % mod; cout << nCr(k + l, l); }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) typedef long long ll; ll mod = 1e9 + 7; ll inv(ll a) { ll n = mod - 2, ans = 1; while (n > 0) { if (n % 2) ans = ans * a % mod; n = n / 2; a = a * a % mod; } return ans; } ll A[2000001]; ll nCr(ll n, ll r) { return A[n] * inv(A[r]) % mod * inv(A[n - r]) % mod; } int main() { ll x, y; cin >> x >> y; if ((x + y) % 3) { cout << 0; return 0; } ll k = (2 * y - x) / 3, l = (2 * x - y) / 3; if (k < 0 || l < 0) { cout << 0; return 0; } A[0] = 1; rep(i, 2000000) A[i + 1] = A[i] * (i + 1) % mod; cout << nCr(k + l, l); }
insert
25
25
25
29
0
p02862
C++
Runtime Error
#include <algorithm> #include <bitset> #include <iostream> #include <queue> #include <set> #include <string> #include <unordered_map> #include <utility> #include <vector> using namespace std; using ll = long long; using Pii = pair<int, int>; using Pll = pair<ll, ll>; template <class T> using Vvec = vector<vector<T>>; constexpr long long default_mod = 1e9 + 7; // element of Z/pZ class Modulo { public: long long p; long long n; Modulo(long long n0, long long p0); bool operator==(const Modulo rhs) const { return n == rhs.n; } Modulo &operator+=(const long long rhs); Modulo &operator+=(const Modulo &rhs); Modulo &operator*=(const long long rhs); Modulo &operator*=(const Modulo &rhs); Modulo &operator-=(const long long rhs); Modulo &operator-=(const Modulo &rhs); Modulo pow(const long long k) const; Modulo inverse() const; Modulo &operator/=(const long long rhs); Modulo &operator/=(const Modulo &rhs); long long get() const { return n; }; }; const Modulo operator+(const Modulo &lhs, const Modulo &rhs); const Modulo operator+(const Modulo &lhs, const long long rhs); const Modulo operator+(const long long lhs, const Modulo &rhs); const Modulo operator*(const Modulo &lhs, const Modulo &rhs); const Modulo operator*(const Modulo &lhs, const long long rhs); const Modulo operator*(const long long lhs, const Modulo &rhs); template <class T> T ext_gcd(T a, T b, T &x, T &y); // constructor Modulo::Modulo(long long n0, long long p0 = default_mod) : p(p0) { n = n0 >= 0 ? n0 % p : (p - (-n0 % p)) % p; } // operator += inline Modulo &Modulo::operator+=(const long long rhs) { long long rhs1 = rhs >= 0 ? rhs % p : (p - (-rhs % p)) % p; n = (n + rhs1) % p; return *this; } inline Modulo &Modulo::operator+=(const Modulo &rhs) { (*this) += rhs.n; return *this; } // operator *= inline Modulo &Modulo::operator*=(const long long rhs) { long long rhs1 = rhs >= 0 ? rhs % p : (p - (-rhs % p)) % p; n = (n * rhs1) % p; return *this; } inline Modulo &Modulo::operator*=(const Modulo &rhs) { (*this) *= rhs.n; return *this; } // operator -= inline Modulo &Modulo::operator-=(const long long rhs) { return *this += -rhs; } inline Modulo &Modulo::operator-=(const Modulo &rhs) { (*this) -= rhs.get(); return *this; } //(*this)**k Modulo Modulo::pow(const long long k) const { if (k == 0) return Modulo(1, p); if (k == 1) return Modulo(n, p); long long k1 = k >= 0 ? k % (p - 1) : ((p - 1) - (-k % (p - 1))) % (p - 1); Modulo r = pow(k1 / 2); if (k1 % 2 == 0) return r * r; else return r * r * n; } //(*this)**(-1) Modulo Modulo::inverse() const { long long x, y; ext_gcd(n, p, x, y); return Modulo(x, p).n; } // operator /= inline Modulo &Modulo::operator/=(const long long rhs) { Modulo inv(rhs, p); inv = inv.inverse(); return *this *= inv; } inline Modulo &Modulo::operator/=(const Modulo &rhs) { (*this) /= rhs.get(); return *this; } // operator + const Modulo operator+(const Modulo &lhs, const Modulo &rhs) { return Modulo(lhs.get(), lhs.p) += rhs; } const Modulo operator+(const Modulo &lhs, const long long rhs) { return Modulo(lhs.get(), lhs.p) += rhs; } const Modulo operator+(const long long lhs, const Modulo &rhs) { return Modulo(lhs, rhs.p) += rhs; } // operator * const Modulo operator*(const Modulo &lhs, const Modulo &rhs) { return Modulo(lhs.get(), lhs.p) *= rhs; } const Modulo operator*(const Modulo &lhs, const long long rhs) { return Modulo(lhs.get(), lhs.p) *= rhs; } const Modulo operator*(const long long lhs, const Modulo &rhs) { return Modulo(lhs, rhs.p) *= rhs; } // operator - const Modulo operator-(const Modulo &lhs, const Modulo &rhs) { return Modulo(lhs.get(), lhs.p) -= rhs; } const Modulo operator-(const Modulo &lhs, const long long rhs) { return Modulo(lhs.get(), lhs.p) -= rhs; } const Modulo operator-(const long long lhs, const Modulo &rhs) { return Modulo(lhs, rhs.p) -= rhs; } // operator / const Modulo operator/(const Modulo &lhs, const Modulo &rhs) { return Modulo(lhs.get(), lhs.p) /= rhs; } const Modulo operator/(const Modulo &lhs, const long long rhs) { return Modulo(lhs.get(), lhs.p) /= rhs; } const Modulo operator/(const long long lhs, const Modulo &rhs) { return Modulo(lhs, rhs.p) /= rhs; } // very very useful functions // n**k Modulo pow(Modulo n, long long k) { Modulo tmp = n; tmp = tmp.pow(k); return tmp; } // n! Modulo fact(long long n, long long p = default_mod, long long init_size = 1e+6) { static bool is_init = false; static std::vector<long long> dat; if (!is_init) { is_init = true; dat.resize(init_size); dat[0] = 1; for (int i = 1; i < init_size; i++) dat[i] = dat[i - 1] * i % p; } return Modulo(dat[n], p); } // 1/n! Modulo ifact(long long n, long long p = default_mod, long long init_size = 1e+6) { static bool is_init = false; static std::vector<long long> dat; if (!is_init) { is_init = true; dat.resize(init_size); dat[0] = 1; for (int i = 1; i < init_size; i++) dat[i] = (Modulo(dat[i - 1], p) / i).n; } return Modulo(dat[n], p); } // initialize void initFact(long long init_size, long long p = default_mod) { fact(0, p, init_size); ifact(0, p, init_size); } // nCk Modulo comb(long long n, long long k, long long p = default_mod) { Modulo a = fact(n, p); Modulo b = ifact(k, p); Modulo c = ifact(n - k, p); return a * b * c; } // ax + by = d template <class T> T ext_gcd(T a, T b, T &x, T &y) { if (b == 0) { x = 1; y = 0; return a; } T d = ext_gcd(b, a % b, x, y); T x_ = x; x = y; y = x_ - a / b * y; return d; } int main() { ll X, Y; cin >> X >> Y; if ((X + Y) % 3 != 0) { cout << 0 << endl; return 0; } ll u = (X + Y) / 3; ll v = Y - X; if (u < v || (u - v) % 2 != 0) { cout << 0 << endl; return 0; } ll p = (u + v) / 2; ll q = (u - v) / 2; initFact(p + q + 5); cout << comb(p + q, p).n << endl; }
#include <algorithm> #include <bitset> #include <iostream> #include <queue> #include <set> #include <string> #include <unordered_map> #include <utility> #include <vector> using namespace std; using ll = long long; using Pii = pair<int, int>; using Pll = pair<ll, ll>; template <class T> using Vvec = vector<vector<T>>; constexpr long long default_mod = 1e9 + 7; // element of Z/pZ class Modulo { public: long long p; long long n; Modulo(long long n0, long long p0); bool operator==(const Modulo rhs) const { return n == rhs.n; } Modulo &operator+=(const long long rhs); Modulo &operator+=(const Modulo &rhs); Modulo &operator*=(const long long rhs); Modulo &operator*=(const Modulo &rhs); Modulo &operator-=(const long long rhs); Modulo &operator-=(const Modulo &rhs); Modulo pow(const long long k) const; Modulo inverse() const; Modulo &operator/=(const long long rhs); Modulo &operator/=(const Modulo &rhs); long long get() const { return n; }; }; const Modulo operator+(const Modulo &lhs, const Modulo &rhs); const Modulo operator+(const Modulo &lhs, const long long rhs); const Modulo operator+(const long long lhs, const Modulo &rhs); const Modulo operator*(const Modulo &lhs, const Modulo &rhs); const Modulo operator*(const Modulo &lhs, const long long rhs); const Modulo operator*(const long long lhs, const Modulo &rhs); template <class T> T ext_gcd(T a, T b, T &x, T &y); // constructor Modulo::Modulo(long long n0, long long p0 = default_mod) : p(p0) { n = n0 >= 0 ? n0 % p : (p - (-n0 % p)) % p; } // operator += inline Modulo &Modulo::operator+=(const long long rhs) { long long rhs1 = rhs >= 0 ? rhs % p : (p - (-rhs % p)) % p; n = (n + rhs1) % p; return *this; } inline Modulo &Modulo::operator+=(const Modulo &rhs) { (*this) += rhs.n; return *this; } // operator *= inline Modulo &Modulo::operator*=(const long long rhs) { long long rhs1 = rhs >= 0 ? rhs % p : (p - (-rhs % p)) % p; n = (n * rhs1) % p; return *this; } inline Modulo &Modulo::operator*=(const Modulo &rhs) { (*this) *= rhs.n; return *this; } // operator -= inline Modulo &Modulo::operator-=(const long long rhs) { return *this += -rhs; } inline Modulo &Modulo::operator-=(const Modulo &rhs) { (*this) -= rhs.get(); return *this; } //(*this)**k Modulo Modulo::pow(const long long k) const { if (k == 0) return Modulo(1, p); if (k == 1) return Modulo(n, p); long long k1 = k >= 0 ? k % (p - 1) : ((p - 1) - (-k % (p - 1))) % (p - 1); Modulo r = pow(k1 / 2); if (k1 % 2 == 0) return r * r; else return r * r * n; } //(*this)**(-1) Modulo Modulo::inverse() const { long long x, y; ext_gcd(n, p, x, y); return Modulo(x, p).n; } // operator /= inline Modulo &Modulo::operator/=(const long long rhs) { Modulo inv(rhs, p); inv = inv.inverse(); return *this *= inv; } inline Modulo &Modulo::operator/=(const Modulo &rhs) { (*this) /= rhs.get(); return *this; } // operator + const Modulo operator+(const Modulo &lhs, const Modulo &rhs) { return Modulo(lhs.get(), lhs.p) += rhs; } const Modulo operator+(const Modulo &lhs, const long long rhs) { return Modulo(lhs.get(), lhs.p) += rhs; } const Modulo operator+(const long long lhs, const Modulo &rhs) { return Modulo(lhs, rhs.p) += rhs; } // operator * const Modulo operator*(const Modulo &lhs, const Modulo &rhs) { return Modulo(lhs.get(), lhs.p) *= rhs; } const Modulo operator*(const Modulo &lhs, const long long rhs) { return Modulo(lhs.get(), lhs.p) *= rhs; } const Modulo operator*(const long long lhs, const Modulo &rhs) { return Modulo(lhs, rhs.p) *= rhs; } // operator - const Modulo operator-(const Modulo &lhs, const Modulo &rhs) { return Modulo(lhs.get(), lhs.p) -= rhs; } const Modulo operator-(const Modulo &lhs, const long long rhs) { return Modulo(lhs.get(), lhs.p) -= rhs; } const Modulo operator-(const long long lhs, const Modulo &rhs) { return Modulo(lhs, rhs.p) -= rhs; } // operator / const Modulo operator/(const Modulo &lhs, const Modulo &rhs) { return Modulo(lhs.get(), lhs.p) /= rhs; } const Modulo operator/(const Modulo &lhs, const long long rhs) { return Modulo(lhs.get(), lhs.p) /= rhs; } const Modulo operator/(const long long lhs, const Modulo &rhs) { return Modulo(lhs, rhs.p) /= rhs; } // very very useful functions // n**k Modulo pow(Modulo n, long long k) { Modulo tmp = n; tmp = tmp.pow(k); return tmp; } // n! Modulo fact(long long n, long long p = default_mod, long long init_size = 1e+6) { static bool is_init = false; static std::vector<long long> dat; if (!is_init) { is_init = true; dat.resize(init_size); dat[0] = 1; for (int i = 1; i < init_size; i++) dat[i] = dat[i - 1] * i % p; } return Modulo(dat[n], p); } // 1/n! Modulo ifact(long long n, long long p = default_mod, long long init_size = 1e+6) { static bool is_init = false; static std::vector<long long> dat; if (!is_init) { is_init = true; dat.resize(init_size); dat[0] = 1; for (int i = 1; i < init_size; i++) dat[i] = (Modulo(dat[i - 1], p) / i).n; } return Modulo(dat[n], p); } // initialize void initFact(long long init_size, long long p = default_mod) { fact(0, p, init_size); ifact(0, p, init_size); } // nCk Modulo comb(long long n, long long k, long long p = default_mod) { Modulo a = fact(n, p); Modulo b = ifact(k, p); Modulo c = ifact(n - k, p); return a * b * c; } // ax + by = d template <class T> T ext_gcd(T a, T b, T &x, T &y) { if (b == 0) { x = 1; y = 0; return a; } T d = ext_gcd(b, a % b, x, y); T x_ = x; x = y; y = x_ - a / b * y; return d; } int main() { ll X, Y; cin >> X >> Y; if ((X + Y) % 3 != 0) { cout << 0 << endl; return 0; } ll u = (X + Y) / 3; ll v = Y - X; if ((u + v) < 0 || u < v || (u - v) % 2 != 0) { cout << 0 << endl; return 0; } ll p = (u + v) / 2; ll q = (u - v) / 2; initFact(p + q + 5); cout << comb(p + q, p).n << endl; }
replace
248
249
248
249
0
p02862
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long const ll mod = 1e9 + 7; ll big_mod(ll a, ll p) { if (p == 0) return 1LL; ll ret = big_mod(a, p / 2); ret *= ret; ret %= mod; if (p & 1) { ret *= a; ret %= mod; } return ret; } ll inv(ll x) { return big_mod(x, mod - 2); } ll F[5000010]; int main() { F[0] = 1LL; for (int i = 1; i <= 5000000; i++) { F[i] = F[i - 1] * (ll)i; F[i] %= mod; } int x, y, A, B; cin >> x >> y; A = 2 * y - x; B = 2 * x - y; if (A % 3 || B % 3) { cout << 0 << endl; } else { A /= 3, B /= 3; ll ans = F[A + B]; ans *= inv(F[A]); ans %= mod; ans *= inv(F[B]); ans %= mod; cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long const ll mod = 1e9 + 7; ll big_mod(ll a, ll p) { if (p == 0) return 1LL; ll ret = big_mod(a, p / 2); ret *= ret; ret %= mod; if (p & 1) { ret *= a; ret %= mod; } return ret; } ll inv(ll x) { return big_mod(x, mod - 2); } ll F[5000010]; int main() { F[0] = 1LL; for (int i = 1; i <= 5000000; i++) { F[i] = F[i - 1] * (ll)i; F[i] %= mod; } int x, y, A, B; cin >> x >> y; A = 2 * y - x; B = 2 * x - y; if (A % 3 || B % 3 || A < 0 || B < 0) { cout << 0 << endl; } else { A /= 3, B /= 3; ll ans = F[A + B]; ans *= inv(F[A]); ans %= mod; ans *= inv(F[B]); ans %= mod; cout << ans << endl; } return 0; }
replace
42
43
42
43
0
p02862
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; typedef long long ll; ll mod = 1e9 + 7; vector<ll> fact; void init_fact(int n) { fact = vector<ll>(n); fact[0] = 1; for (int i = 1; i < n; i++) fact[i] = fact[i - 1] * i % mod; } ll calc_pow(ll x, ll pow) { if (pow == 0) return 1; ll a = calc_pow(x, pow / 2); a = a * a % mod; if (pow % 2 == 1) a *= x; return a % mod; } ll calc_div(ll a, ll b) { return a * calc_pow(b, mod - 2) % mod; } ll calc_comb(ll n, ll r) { if (n < r) return 0; return calc_div(fact[n], fact[r] * fact[n - r] % mod); } int main() { int x, y; cin >> x >> y; if ((x + y) % 3 != 0) { cout << 0 << endl; return 0; } int times = (x + y) / 3; init_fact(1000010); cout << calc_comb(times, x - times) << endl; return 0; }
#include <iostream> #include <vector> using namespace std; typedef long long ll; ll mod = 1e9 + 7; vector<ll> fact; void init_fact(int n) { fact = vector<ll>(n); fact[0] = 1; for (int i = 1; i < n; i++) fact[i] = fact[i - 1] * i % mod; } ll calc_pow(ll x, ll pow) { if (pow == 0) return 1; ll a = calc_pow(x, pow / 2); a = a * a % mod; if (pow % 2 == 1) a *= x; return a % mod; } ll calc_div(ll a, ll b) { return a * calc_pow(b, mod - 2) % mod; } ll calc_comb(ll n, ll r) { if (n < r || r < 0) return 0; return calc_div(fact[n], fact[r] * fact[n - r] % mod); } int main() { int x, y; cin >> x >> y; if ((x + y) % 3 != 0) { cout << 0 << endl; return 0; } int times = (x + y) / 3; init_fact(1000010); cout << calc_comb(times, x - times) << endl; return 0; }
replace
30
31
30
31
0
p02862
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define mod 1000000007 using ll = long long; ll factorial[2000000]; ll power(ll n, ll m) { ll ans = 1; for (int r = 0; m >> r > 0; r++) { if ((m >> r) & 1) ans = ans * n % mod; n = n * n % mod; } return ans; } ll inv(ll n) { return power(n, mod - 2); } void fact() { factorial[0] = 1; for (int i = 1; i < 2000000; i++) factorial[i] = i * factorial[i - 1] % mod; return; } ll cmb(ll n, ll r) { if (r > n) return 0; else return factorial[n] * inv(factorial[n - r]) % mod * inv(factorial[r]) % mod; } int main() { fact(); ll X, Y; cin >> X >> Y; ll x = (2 * X - Y) / 3, y = (2 * Y - X) / 3; if ((2 * X - Y) % 3 != 0 || (2 * Y - X) % 3 != 0 || X < 0 || y < 0) { cout << 0 << endl; return 0; } else { cout << cmb(x + y, x) << endl; return 0; } }
#include <bits/stdc++.h> using namespace std; #define mod 1000000007 using ll = long long; ll factorial[2000000]; ll power(ll n, ll m) { ll ans = 1; for (int r = 0; m >> r > 0; r++) { if ((m >> r) & 1) ans = ans * n % mod; n = n * n % mod; } return ans; } ll inv(ll n) { return power(n, mod - 2); } void fact() { factorial[0] = 1; for (int i = 1; i < 2000000; i++) factorial[i] = i * factorial[i - 1] % mod; return; } ll cmb(ll n, ll r) { if (r > n) return 0; else return factorial[n] * inv(factorial[n - r]) % mod * inv(factorial[r]) % mod; } int main() { fact(); ll X, Y; cin >> X >> Y; ll x = (2 * X - Y) / 3, y = (2 * Y - X) / 3; if ((2 * X - Y) % 3 != 0 || (2 * Y - X) % 3 != 0 || x < 0 || y < 0) { cout << 0 << endl; return 0; } else { cout << cmb(x + y, x) << endl; return 0; } }
replace
37
38
37
38
0
p02862
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define ld long double #pragma 03 using namespace std; ll mod = 1e9 + 7; ll factorial[1000005]; ll binpow(ll c, ll d) { ll a = c % mod, b = d % (mod - 1); if (b == 0) return 1; ll x = binpow(a, b / 2); (x *= x) %= mod; if (b % 2 == 0) return x; else return (x * a) % mod; } ll bindiv(ll a, ll b) { a %= mod; b %= mod; ll x = binpow(b, mod - 2); return (x * a) % mod; } ll com(ll a, ll b) { ll x = factorial[a] * factorial[b - a]; ll y = factorial[b]; return bindiv(y, x); } int main() { ll x, y; cin >> x >> y; if ((x + y) % 3 != 0) { cout << 0; return 0; } factorial[0] = 1LL; for (ll i = 1; i < 1000005; i++) factorial[i] = (factorial[i - 1] * i) % mod; if (2 * x < y) cout << 0; else cout << com((2 * x - y) / 3, (x + y) / 3); return 0; }
#include <bits/stdc++.h> #define ll long long #define ld long double #pragma 03 using namespace std; ll mod = 1e9 + 7; ll factorial[1000005]; ll binpow(ll c, ll d) { ll a = c % mod, b = d % (mod - 1); if (b == 0) return 1; ll x = binpow(a, b / 2); (x *= x) %= mod; if (b % 2 == 0) return x; else return (x * a) % mod; } ll bindiv(ll a, ll b) { a %= mod; b %= mod; ll x = binpow(b, mod - 2); return (x * a) % mod; } ll com(ll a, ll b) { ll x = factorial[a] * factorial[b - a]; ll y = factorial[b]; return bindiv(y, x); } int main() { ll x, y; cin >> x >> y; if ((x + y) % 3 != 0) { cout << 0; return 0; } factorial[0] = 1LL; for (ll i = 1; i < 1000005; i++) factorial[i] = (factorial[i - 1] * i) % mod; if (2 * x < y || 2 * y < x) cout << 0; else cout << com((2 * x - y) / 3, (x + y) / 3); return 0; }
replace
39
40
39
40
0
p02862
C++
Runtime Error
/* Author:N_o_o_B Created:November 19 2019 14:02:32 */ #include <bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define TRACE #ifdef TRACE #define trace(...) \ { \ cerr << "[ "; \ __trace__(#__VA_ARGS__, __VA_ARGS__); \ } #undef endl template <typename Arg1, typename Arg2> ostream &operator<<(ostream &out, const pair<Arg1, Arg2> &x) { return out << "(" << x.first << "," << x.second << ")"; } template <typename Arg1> ostream &operator<<(ostream &out, const vector<Arg1> &a) { out << "["; for (const auto &x : a) out << x << ","; return out << "]"; } template <typename Arg1> ostream &operator<<(ostream &out, const set<Arg1> &a) { out << "["; for (const auto &x : a) out << x << ","; return out << "]"; } template <typename Arg1, typename Arg2> ostream &operator<<(ostream &out, const map<Arg1, Arg2> &a) { out << "["; for (const auto &x : a) out << x << ","; return out << "]"; } template <typename Arg1> void __trace__(const string name, Arg1 &&arg1) { cerr << name << " : " << arg1 << " ] " << endl; } template <typename Arg1, typename... Args> void __trace__(const string names, Arg1 &&arg1, Args &&...args) { const string name = names.substr(0, names.find(',')); cerr << name << " : " << arg1 << " | "; __trace__(names.substr(1 + (int)name.size()), args...); } #else #define trace(args...) #endif typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<pair<int, int>> vii; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<pair<ll, ll>> vll; typedef vector<vl> vvl; typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> oset; #define fori(i, n) for (int i = 0; i < n; i++) #define rep(i, a, b) for (int i = a; i <= b; i++) #define repd(i, a, b) for (int i = a; i >= b; i--) #define ford(i, n) for (int i = n - 1; i >= 0; i--) #define all(x) x.begin(), x.end() #define pb push_back #define endl '\n' #define sz(a) (int)a.size() #define fi first #define se second clock_t time_p = clock(); void time_taken() { time_p = clock() - time_p; cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n"; } // const ll mod=998244353; const ll mod = 1e9 + 7; const ll INF = 1e18; // credits ---> ksun48 ll modinv(ll a, ll m) { assert(m > 0); if (m == 1) return 0; a %= m; if (a < 0) a += m; assert(a != 0); if (a == 1) return 1; return m - modinv(m, a) * m / a; } template <int MOD_> struct modnum { private: int v; public: static const int MOD = MOD_; modnum() : v(0) {} modnum(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; } explicit operator int() const { return v; } friend bool operator==(const modnum &a, const modnum &b) { return a.v == b.v; } friend bool operator!=(const modnum &a, const modnum &b) { return a.v != b.v; } modnum operator~() const { modnum res; res.v = modinv(v, MOD); return res; } modnum &operator+=(const modnum &o) { v += o.v; if (v >= MOD) v -= MOD; return *this; } modnum &operator-=(const modnum &o) { v -= o.v; if (v < 0) v += MOD; return *this; } modnum &operator*=(const modnum &o) { v = int(ll(v) * ll(o.v) % MOD); return *this; } modnum &operator/=(const modnum &o) { return *this *= (~o); } friend modnum operator+(const modnum &a, const modnum &b) { return modnum(a) += b; } friend modnum operator-(const modnum &a, const modnum &b) { return modnum(a) -= b; } friend modnum operator*(const modnum &a, const modnum &b) { return modnum(a) *= b; } friend modnum operator/(const modnum &a, const modnum &b) { return modnum(a) /= b; } }; using num = modnum<int(1e9) + 7>; vector<num> fact; vector<num> ifact; void init() { fact = {1}; for (int i = 1; i <= 2000000; i++) fact.push_back(i * fact[i - 1]); for (num x : fact) ifact.push_back(1 / x); } num ncr(int n, int k) { if (k < 0 || k > n) return 0; return fact[n] * ifact[k] * ifact[n - k]; } num powmod(num x, int a) { if (a == 0) return 1; if (a & 1) return x * powmod(x, a - 1); return powmod(x * x, a / 2); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout.precision(12); cout << fixed; int x, y; cin >> x >> y; init(); /* a*(1,2) + b*(2,1) = (x,y) a + 2*b = x; 2*a + b = y; a = (2*y-x)/3; b = (2*x-y)/3; */ ll a = (2 * y - x), b = (2 * x - y); if (a % 3 or b % 3) { cout << 0 << endl; return 0; } a /= 3; b /= 3; cout << (int)(fact[a + b] * ifact[a] * ifact[b]) << endl; time_taken(); return 0; }
/* Author:N_o_o_B Created:November 19 2019 14:02:32 */ #include <bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define TRACE #ifdef TRACE #define trace(...) \ { \ cerr << "[ "; \ __trace__(#__VA_ARGS__, __VA_ARGS__); \ } #undef endl template <typename Arg1, typename Arg2> ostream &operator<<(ostream &out, const pair<Arg1, Arg2> &x) { return out << "(" << x.first << "," << x.second << ")"; } template <typename Arg1> ostream &operator<<(ostream &out, const vector<Arg1> &a) { out << "["; for (const auto &x : a) out << x << ","; return out << "]"; } template <typename Arg1> ostream &operator<<(ostream &out, const set<Arg1> &a) { out << "["; for (const auto &x : a) out << x << ","; return out << "]"; } template <typename Arg1, typename Arg2> ostream &operator<<(ostream &out, const map<Arg1, Arg2> &a) { out << "["; for (const auto &x : a) out << x << ","; return out << "]"; } template <typename Arg1> void __trace__(const string name, Arg1 &&arg1) { cerr << name << " : " << arg1 << " ] " << endl; } template <typename Arg1, typename... Args> void __trace__(const string names, Arg1 &&arg1, Args &&...args) { const string name = names.substr(0, names.find(',')); cerr << name << " : " << arg1 << " | "; __trace__(names.substr(1 + (int)name.size()), args...); } #else #define trace(args...) #endif typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<pair<int, int>> vii; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<pair<ll, ll>> vll; typedef vector<vl> vvl; typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> oset; #define fori(i, n) for (int i = 0; i < n; i++) #define rep(i, a, b) for (int i = a; i <= b; i++) #define repd(i, a, b) for (int i = a; i >= b; i--) #define ford(i, n) for (int i = n - 1; i >= 0; i--) #define all(x) x.begin(), x.end() #define pb push_back #define endl '\n' #define sz(a) (int)a.size() #define fi first #define se second clock_t time_p = clock(); void time_taken() { time_p = clock() - time_p; cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n"; } // const ll mod=998244353; const ll mod = 1e9 + 7; const ll INF = 1e18; // credits ---> ksun48 ll modinv(ll a, ll m) { assert(m > 0); if (m == 1) return 0; a %= m; if (a < 0) a += m; assert(a != 0); if (a == 1) return 1; return m - modinv(m, a) * m / a; } template <int MOD_> struct modnum { private: int v; public: static const int MOD = MOD_; modnum() : v(0) {} modnum(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; } explicit operator int() const { return v; } friend bool operator==(const modnum &a, const modnum &b) { return a.v == b.v; } friend bool operator!=(const modnum &a, const modnum &b) { return a.v != b.v; } modnum operator~() const { modnum res; res.v = modinv(v, MOD); return res; } modnum &operator+=(const modnum &o) { v += o.v; if (v >= MOD) v -= MOD; return *this; } modnum &operator-=(const modnum &o) { v -= o.v; if (v < 0) v += MOD; return *this; } modnum &operator*=(const modnum &o) { v = int(ll(v) * ll(o.v) % MOD); return *this; } modnum &operator/=(const modnum &o) { return *this *= (~o); } friend modnum operator+(const modnum &a, const modnum &b) { return modnum(a) += b; } friend modnum operator-(const modnum &a, const modnum &b) { return modnum(a) -= b; } friend modnum operator*(const modnum &a, const modnum &b) { return modnum(a) *= b; } friend modnum operator/(const modnum &a, const modnum &b) { return modnum(a) /= b; } }; using num = modnum<int(1e9) + 7>; vector<num> fact; vector<num> ifact; void init() { fact = {1}; for (int i = 1; i <= 2000000; i++) fact.push_back(i * fact[i - 1]); for (num x : fact) ifact.push_back(1 / x); } num ncr(int n, int k) { if (k < 0 || k > n) return 0; return fact[n] * ifact[k] * ifact[n - k]; } num powmod(num x, int a) { if (a == 0) return 1; if (a & 1) return x * powmod(x, a - 1); return powmod(x * x, a / 2); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout.precision(12); cout << fixed; int x, y; cin >> x >> y; init(); /* a*(1,2) + b*(2,1) = (x,y) a + 2*b = x; 2*a + b = y; a = (2*y-x)/3; b = (2*x-y)/3; */ ll a = (2 * y - x), b = (2 * x - y); if (a < 0 or b < 0 or a % 3 or b % 3) { cout << 0 << endl; return 0; } a /= 3; b /= 3; cout << (int)(fact[a + b] * ifact[a] * ifact[b]) << endl; time_taken(); return 0; }
replace
209
210
209
210
TLE
p02862
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <queue> #include <set> #include <tuple> #include <vector> using namespace std; #define rep(i, n) for (int64_t i = 0; i < (n); i++) #define irep(i, n) for (int64_t i = 0; i <= (n); i++) #define rrep(i, n) for (int64_t i = (n)-1; i >= 0; i--) #define rirep(i, n) for (int64_t i = n; i >= 0; i--) const uint64_t MOD = 1'000'000'007L; uint64_t dpow(uint64_t x, uint64_t y) { if (y == 0) { return 1; } if (y % 2 == 0) { return dpow(x * x % MOD, y / 2); } else { return x * dpow(x, y - 1) % MOD; } } uint64_t inv(uint64_t x) { return dpow(x, MOD - 2); } uint64_t comb(uint64_t n, uint64_t k) { uint64_t a = 1, b = 1; for (uint64_t i = 0; i < k; i++) { a *= (n - i); a %= MOD; b *= i + 1; b %= MOD; } return a * inv(b) % MOD; } int main() { int x, y; cin >> x >> y; int z = 2 * x - y, w = -x + 2 * y; if (z % 3 != 0 || w % 3 != 0) { cout << 0 << endl; return 0; } z /= 3; w /= 3; cout << comb(z + w, z) << endl; return 0; }
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <queue> #include <set> #include <tuple> #include <vector> using namespace std; #define rep(i, n) for (int64_t i = 0; i < (n); i++) #define irep(i, n) for (int64_t i = 0; i <= (n); i++) #define rrep(i, n) for (int64_t i = (n)-1; i >= 0; i--) #define rirep(i, n) for (int64_t i = n; i >= 0; i--) const uint64_t MOD = 1'000'000'007L; uint64_t dpow(uint64_t x, uint64_t y) { if (y == 0) { return 1; } if (y % 2 == 0) { return dpow(x * x % MOD, y / 2); } else { return x * dpow(x, y - 1) % MOD; } } uint64_t inv(uint64_t x) { return dpow(x, MOD - 2); } uint64_t comb(uint64_t n, uint64_t k) { uint64_t a = 1, b = 1; for (uint64_t i = 0; i < k; i++) { a *= (n - i); a %= MOD; b *= i + 1; b %= MOD; } return a * inv(b) % MOD; } int main() { int x, y; cin >> x >> y; int z = 2 * x - y, w = -x + 2 * y; if (z % 3 != 0 || w % 3 != 0) { cout << 0 << endl; return 0; } if (z < 0 || w < 0) { cout << 0 << endl; return 0; } z /= 3; w /= 3; cout << comb(z + w, z) << endl; return 0; }
insert
53
53
53
57
TLE
p02862
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int MAX_N = 1000005; const int mod = 1e9 + 7; LL pow_mod(LL a, LL k, LL m) { LL r = 1; for (; k > 0; k >>= 1) { if (k & 1) r = (r * a) % m; a = (a * a) % m; } return r; } LL inverse(LL a, LL m) { return pow_mod(a, m - 2, m); } int x, y; LL fac[MAX_N], inv[MAX_N]; LL C(LL n, LL r) { return (((fac[n] * inv[r]) % mod) * inv[n - r]) % mod; } int main() { scanf("%d %d", &x, &y); int n = max(x, y); fac[0] = 1; inv[0] = 1; for (int i = 1; i <= n; i++) { fac[i] = (fac[i - 1] * i) % mod; inv[i] = inverse(fac[i], mod); } if ((x + y) % 3) printf("0\n"); else { int p = (x + y) / 3; x -= p; y -= p; cout << C(x + y, x) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int MAX_N = 1000005; const int mod = 1e9 + 7; LL pow_mod(LL a, LL k, LL m) { LL r = 1; for (; k > 0; k >>= 1) { if (k & 1) r = (r * a) % m; a = (a * a) % m; } return r; } LL inverse(LL a, LL m) { return pow_mod(a, m - 2, m); } int x, y; LL fac[MAX_N], inv[MAX_N]; LL C(LL n, LL r) { return (((fac[n] * inv[r]) % mod) * inv[n - r]) % mod; } int main() { scanf("%d %d", &x, &y); int n = max(x, y); fac[0] = 1; inv[0] = 1; for (int i = 1; i <= n; i++) { fac[i] = (fac[i - 1] * i) % mod; inv[i] = inverse(fac[i], mod); } if ((x + y) % 3) printf("0\n"); else { int p = (x + y) / 3; if (x < p || y < p) { printf("0\n"); } else { x -= p; y -= p; cout << C(x + y, x) << endl; } } return 0; }
replace
39
42
39
46
0
p02862
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int MAX_N = 1000005; const int mod = 1e9 + 7; LL pow_mod(LL a, LL k, LL m) { LL r = 1; for (; k > 0; k >>= 1) { if (k & 1) r = (r * a) % m; a = (a * a) % m; } return r; } LL inverse(LL a, LL m) { return pow_mod(a, m - 2, m); } int x, y; LL fac[MAX_N], inv[MAX_N]; int main() { scanf("%d %d", &x, &y); int n = max(x, y); fac[0] = 1; inv[0] = 1; for (int i = 1; i <= n; i++) { fac[i] = (fac[i - 1] * i) % mod; inv[i] = inverse(fac[i], mod); } LL ans = 0; for (int i = 0; i <= n; i++) { if ((x - i) % 2) continue; int b = (x - i) / 2; if (2 * i + b != y) continue; ans = (ans + ((((fac[i + b] * inv[i]) % mod) * inv[b]) % mod)) % mod; } printf("%lld\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int MAX_N = 1000005; const int mod = 1e9 + 7; LL pow_mod(LL a, LL k, LL m) { LL r = 1; for (; k > 0; k >>= 1) { if (k & 1) r = (r * a) % m; a = (a * a) % m; } return r; } LL inverse(LL a, LL m) { return pow_mod(a, m - 2, m); } int x, y; LL fac[MAX_N], inv[MAX_N]; int main() { scanf("%d %d", &x, &y); int n = max(x, y); fac[0] = 1; inv[0] = 1; for (int i = 1; i <= n; i++) { fac[i] = (fac[i - 1] * i) % mod; inv[i] = inverse(fac[i], mod); } LL ans = 0; for (int i = 0; i <= n; i++) { if (i > x) break; if ((x - i) % 2) continue; int b = (x - i) / 2; if (2 * i + b != y) continue; ans = (ans + ((((fac[i + b] * inv[i]) % mod) * inv[b]) % mod)) % mod; } printf("%lld\n", ans); return 0; }
insert
35
35
35
37
0
p02862
C++
Runtime Error
#include <bits/stdc++.h> // #include <ext/numeric> // #include <ext/pb_ds/assoc_container.hpp> // Common file // #include <ext/pb_ds/tree_policy.hpp> // Including // tree_order_statistics_node_update #define oo 0x3f3f3f3f #define OO 0x3f3f3f3f3f3f3f3f #define popcount(n) __builtin_popcount(n) #define popcountll(n) __builtin_popcountll(n) #define ll long long using namespace std; // using namespace __gnu_cxx; // using namespace __gnu_pbds; // template<typename T> // using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, // tree_order_statistics_node_update>; const double PI = acos(-1.0); const int N = 1e6 + 5, MOD = 1e9 + 7; long long fact[N], fact_inv[N]; long long mul(long long a, long long b) { return a * b % MOD; } long long power(long long a, long long b) { if (!b) return 1; long long r = power(a, b / 2); r = mul(r, r); if (b & 1) return mul(r, a); return r; } long long mod_inv(long long x) { return power(x, MOD - 2); } long long nCr(long long n, long long r) { if (!n) return !r; if (r > n) return 0; return mul(fact[n], mul(fact_inv[n - r], fact_inv[r])); } void pre() { fact[0] = fact_inv[0] = 1; for (int i = 1; i < N; i++) { fact[i] = mul(fact[i - 1], i); fact_inv[i] = mod_inv(fact[i]); } } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "rt", stdin); // freopen("friday.out", "w", stdout); #endif ios::sync_with_stdio(false), cin.tie(0), cout.tie(0), cout.precision(10), cout << fixed; pre(); int x, y; cin >> x >> y; long long ans = 0; for (int i = 0; i <= x; ++i) { int a = x - i; int b = y - 2 * i; if (a % 2 == 0 && a / 2 == b) { ans += nCr(i + b, b); ans %= MOD; } } cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> // #include <ext/numeric> // #include <ext/pb_ds/assoc_container.hpp> // Common file // #include <ext/pb_ds/tree_policy.hpp> // Including // tree_order_statistics_node_update #define oo 0x3f3f3f3f #define OO 0x3f3f3f3f3f3f3f3f #define popcount(n) __builtin_popcount(n) #define popcountll(n) __builtin_popcountll(n) #define ll long long using namespace std; // using namespace __gnu_cxx; // using namespace __gnu_pbds; // template<typename T> // using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, // tree_order_statistics_node_update>; const double PI = acos(-1.0); const int N = 1e6 + 5, MOD = 1e9 + 7; long long fact[N], fact_inv[N]; long long mul(long long a, long long b) { return a * b % MOD; } long long power(long long a, long long b) { if (!b) return 1; long long r = power(a, b / 2); r = mul(r, r); if (b & 1) return mul(r, a); return r; } long long mod_inv(long long x) { return power(x, MOD - 2); } long long nCr(long long n, long long r) { if (!n) return !r; if (r > n) return 0; return mul(fact[n], mul(fact_inv[n - r], fact_inv[r])); } void pre() { fact[0] = fact_inv[0] = 1; for (int i = 1; i < N; i++) { fact[i] = mul(fact[i - 1], i); fact_inv[i] = mod_inv(fact[i]); } } int main() { #ifndef ONLINE_JUDGE // freopen("input.txt", "rt", stdin); // freopen("friday.out", "w", stdout); #endif ios::sync_with_stdio(false), cin.tie(0), cout.tie(0), cout.precision(10), cout << fixed; pre(); int x, y; cin >> x >> y; long long ans = 0; for (int i = 0; i <= x; ++i) { int a = x - i; int b = y - 2 * i; if (a % 2 == 0 && a / 2 == b) { ans += nCr(i + b, b); ans %= MOD; } } cout << ans << '\n'; return 0; }
replace
55
56
55
56
0
p02862
C++
Runtime Error
#include <bits/stdc++.h> #define ALL(x) x.begin(), x.end() using namespace std; typedef long long ll; typedef vector<ll> vi; const ll mod = 1e9 + 7; const ll N = 2e6 + 123; ll fact[N]; ll mpow(ll x, ll n) { if (n == 0) return 1; ll res = mpow(x, n / 2); res = res * res % mod; if (n % 2 == 1) res = res * x % mod; return res; } ll inverse(ll x) { return mpow(x, mod - 2); } void solve(void) { ll x, y; cin >> x >> y; fact[0] = 1; for (ll i = 1; i < N; i++) { fact[i] = fact[i - 1] * i % mod; } if ((2 * x - y) % 3 || (2 * y - x) % 3) { cout << 0 << '\n'; } else { ll a = (2 * x - y) / 3; ll b = (2 * y - x) / 3; ll ans = fact[a + b] * inverse(fact[a]) % mod * inverse(fact[b]) % mod; cout << ans << '\n'; } } int main(void) { solve(); return 0; }
#include <bits/stdc++.h> #define ALL(x) x.begin(), x.end() using namespace std; typedef long long ll; typedef vector<ll> vi; const ll mod = 1e9 + 7; const ll N = 2e6 + 123; ll fact[N]; ll mpow(ll x, ll n) { if (n == 0) return 1; ll res = mpow(x, n / 2); res = res * res % mod; if (n % 2 == 1) res = res * x % mod; return res; } ll inverse(ll x) { return mpow(x, mod - 2); } void solve(void) { ll x, y; cin >> x >> y; fact[0] = 1; for (ll i = 1; i < N; i++) { fact[i] = fact[i - 1] * i % mod; } if ((2 * x - y) % 3 || (2 * y - x) % 3 || (2 * y - x) < 0 || (2 * x - y) < 0) { cout << 0 << '\n'; } else { ll a = (2 * x - y) / 3; ll b = (2 * y - x) / 3; ll ans = fact[a + b] * inverse(fact[a]) % mod * inverse(fact[b]) % mod; cout << ans << '\n'; } } int main(void) { solve(); return 0; }
replace
35
36
35
37
0
p02862
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define mod 1000000007 using namespace std; const int MAXN = 2e6 + 7; inline int add(int x, int y) { int res = x + y; if (res >= mod) res -= mod; return res; } inline int mul(int x, int y) { ll res = (ll)x * y; res -= res / mod * mod; return res; } int fac[MAXN], inv[MAXN]; int ksm(int a, int b) { int res = 1; while (b) { if (b & 1) res = mul(res, a); a = mul(a, a); b /= 2; } return res; } inline int C(int n, int m) { return mul(fac[n], mul(inv[m], inv[n - m])); } int main() { int x, y; scanf("%d%d", &x, &y); int a = (2 * y - x) / 3, b = (2 * x - y) / 3; if (a * 3 != 2 * y - x || b * 3 != 2 * x - y) { puts("0"); return 0; } int N = 2000000; fac[0] = 1; for (int i = 1; i <= N; i++) fac[i] = mul(fac[i - 1], i); inv[N] = ksm(fac[N], mod - 2); for (int i = N; i; --i) inv[i - 1] = mul(inv[i], i); printf("%d\n", C(a + b, b)); return 0; }
#include <bits/stdc++.h> #define ll long long #define mod 1000000007 using namespace std; const int MAXN = 2e6 + 7; inline int add(int x, int y) { int res = x + y; if (res >= mod) res -= mod; return res; } inline int mul(int x, int y) { ll res = (ll)x * y; res -= res / mod * mod; return res; } int fac[MAXN], inv[MAXN]; int ksm(int a, int b) { int res = 1; while (b) { if (b & 1) res = mul(res, a); a = mul(a, a); b /= 2; } return res; } inline int C(int n, int m) { return mul(fac[n], mul(inv[m], inv[n - m])); } int main() { int x, y; scanf("%d%d", &x, &y); int a = (2 * y - x) / 3, b = (2 * x - y) / 3; if (a * 3 != 2 * y - x || b * 3 != 2 * x - y || a < 0 || b < 0) { puts("0"); return 0; } int N = 2000000; fac[0] = 1; for (int i = 1; i <= N; i++) fac[i] = mul(fac[i - 1], i); inv[N] = ksm(fac[N], mod - 2); for (int i = N; i; --i) inv[i - 1] = mul(inv[i], i); printf("%d\n", C(a + b, b)); return 0; }
replace
32
33
32
33
0
p02862
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define INF_LL (int64)1e18 #define INF (int32)1e9 #define REP(i, n) for (int64 i = 0; i < (n); i++) #define FOR(i, a, b) for (int64 i = (a); i < (b); i++) #define all(x) x.begin(), x.end() #define fs first #define sc second using int32 = int_fast32_t; using uint32 = uint_fast32_t; using int64 = int_fast64_t; using uint64 = uint_fast64_t; using PII = pair<int32, int32>; using PLL = pair<int64, int64>; const double eps = 1e-10; template <typename A, typename B> inline void chmin(A &a, B b) { if (a > b) a = b; } template <typename A, typename B> inline void chmax(A &a, B b) { if (a < b) a = b; } template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } template <typename T, typename U, typename... V> typename enable_if<is_same<T, U>::value != 0>::type fill_v(U &u, const V... v) { u = U(v...); } template <typename T, typename U, typename... V> typename enable_if<is_same<T, U>::value == 0>::type fill_v(U &u, const V... v) { for (auto &e : u) fill_v<T>(e, v...); } template <::std::uint_fast64_t mod> class ModInt { private: using value_type = ::std::uint_fast64_t; value_type n; public: ModInt() : n(0) {} ModInt(value_type n_) : n(n_ % mod) {} ModInt(const ModInt &m) : n(m.n) {} template <typename T> explicit operator T() const { return static_cast<T>(n); } value_type get() const { return n; } friend ::std::ostream &operator<<(::std::ostream &os, const ModInt<mod> &a) { return os << a.n; } friend ::std::istream &operator>>(::std::istream &is, ModInt<mod> &a) { value_type x; is >> x; a = ModInt<mod>(x); return is; } bool operator==(const ModInt &m) const { return n == m.n; } bool operator!=(const ModInt &m) const { return n != m.n; } ModInt &operator*=(const ModInt &m) { n = n * m.n % mod; return *this; } ModInt pow(value_type b) const { ModInt ans = 1, m = ModInt(*this); while (b) { if (b & 1) ans *= m; m *= m; b >>= 1; } return ans; } ModInt inv() const { return (*this).pow(mod - 2); } ModInt &operator+=(const ModInt &m) { n += m.n; n = (n < mod ? n : n - mod); return *this; } ModInt &operator-=(const ModInt &m) { n += mod - m.n; n = (n < mod ? n : n - mod); return *this; } ModInt &operator/=(const ModInt &m) { *this *= m.inv(); return *this; } ModInt operator+(const ModInt &m) const { return ModInt(*this) += m; } ModInt operator-(const ModInt &m) const { return ModInt(*this) -= m; } ModInt operator*(const ModInt &m) const { return ModInt(*this) *= m; } ModInt operator/(const ModInt &m) const { return ModInt(*this) /= m; } ModInt &operator++() { n += 1; return *this; } ModInt &operator--() { n -= 1; return *this; } ModInt operator++(int) { ModInt old(n); n += 1; return old; } ModInt operator--(int) { ModInt old(n); n -= 1; return old; } ModInt operator-() const { return ModInt(mod - n); } }; template <::std::size_t size, ::std::uint_fast64_t mod = 1000000007> class Factorial { private: using value_type = ModInt<mod>; ::std::vector<value_type> fact, inv; public: Factorial() : fact(size + 1, 1), inv(size + 1, 1) { for (::std::size_t i = 1; i <= size; ++i) { fact[i] = fact[i - 1] * value_type(i); inv[i] = fact[i].inv(); } } value_type comb(::std::int64_t a, ::std::int64_t b) { assert(a >= b); assert(b >= 0); return fact[a] * inv[b] * inv[a - b]; } value_type &operator[](::std::size_t k) { return fact[k]; } }; constexpr int64 mod = 1e9 + 7; using Mint = ModInt<mod>; Factorial<2123456> f; int main(void) { int64 X, Y; cin >> X >> Y; Mint res = 0; REP(i, X + Y + 1) { int64 x = X - i, y = Y - i; if (x + y != i) continue; res += f.comb(x + y, x); } cout << res << endl; }
#include <bits/stdc++.h> using namespace std; #define INF_LL (int64)1e18 #define INF (int32)1e9 #define REP(i, n) for (int64 i = 0; i < (n); i++) #define FOR(i, a, b) for (int64 i = (a); i < (b); i++) #define all(x) x.begin(), x.end() #define fs first #define sc second using int32 = int_fast32_t; using uint32 = uint_fast32_t; using int64 = int_fast64_t; using uint64 = uint_fast64_t; using PII = pair<int32, int32>; using PLL = pair<int64, int64>; const double eps = 1e-10; template <typename A, typename B> inline void chmin(A &a, B b) { if (a > b) a = b; } template <typename A, typename B> inline void chmax(A &a, B b) { if (a < b) a = b; } template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } template <typename T, typename U, typename... V> typename enable_if<is_same<T, U>::value != 0>::type fill_v(U &u, const V... v) { u = U(v...); } template <typename T, typename U, typename... V> typename enable_if<is_same<T, U>::value == 0>::type fill_v(U &u, const V... v) { for (auto &e : u) fill_v<T>(e, v...); } template <::std::uint_fast64_t mod> class ModInt { private: using value_type = ::std::uint_fast64_t; value_type n; public: ModInt() : n(0) {} ModInt(value_type n_) : n(n_ % mod) {} ModInt(const ModInt &m) : n(m.n) {} template <typename T> explicit operator T() const { return static_cast<T>(n); } value_type get() const { return n; } friend ::std::ostream &operator<<(::std::ostream &os, const ModInt<mod> &a) { return os << a.n; } friend ::std::istream &operator>>(::std::istream &is, ModInt<mod> &a) { value_type x; is >> x; a = ModInt<mod>(x); return is; } bool operator==(const ModInt &m) const { return n == m.n; } bool operator!=(const ModInt &m) const { return n != m.n; } ModInt &operator*=(const ModInt &m) { n = n * m.n % mod; return *this; } ModInt pow(value_type b) const { ModInt ans = 1, m = ModInt(*this); while (b) { if (b & 1) ans *= m; m *= m; b >>= 1; } return ans; } ModInt inv() const { return (*this).pow(mod - 2); } ModInt &operator+=(const ModInt &m) { n += m.n; n = (n < mod ? n : n - mod); return *this; } ModInt &operator-=(const ModInt &m) { n += mod - m.n; n = (n < mod ? n : n - mod); return *this; } ModInt &operator/=(const ModInt &m) { *this *= m.inv(); return *this; } ModInt operator+(const ModInt &m) const { return ModInt(*this) += m; } ModInt operator-(const ModInt &m) const { return ModInt(*this) -= m; } ModInt operator*(const ModInt &m) const { return ModInt(*this) *= m; } ModInt operator/(const ModInt &m) const { return ModInt(*this) /= m; } ModInt &operator++() { n += 1; return *this; } ModInt &operator--() { n -= 1; return *this; } ModInt operator++(int) { ModInt old(n); n += 1; return old; } ModInt operator--(int) { ModInt old(n); n -= 1; return old; } ModInt operator-() const { return ModInt(mod - n); } }; template <::std::size_t size, ::std::uint_fast64_t mod = 1000000007> class Factorial { private: using value_type = ModInt<mod>; ::std::vector<value_type> fact, inv; public: Factorial() : fact(size + 1, 1), inv(size + 1, 1) { for (::std::size_t i = 1; i <= size; ++i) { fact[i] = fact[i - 1] * value_type(i); inv[i] = fact[i].inv(); } } value_type comb(::std::int64_t a, ::std::int64_t b) { assert(a >= b); assert(b >= 0); return fact[a] * inv[b] * inv[a - b]; } value_type &operator[](::std::size_t k) { return fact[k]; } }; constexpr int64 mod = 1e9 + 7; using Mint = ModInt<mod>; Factorial<2123456> f; int main(void) { int64 X, Y; cin >> X >> Y; Mint res = 0; REP(i, min(X, Y) + 1) { int64 x = X - i, y = Y - i; if (x + y != i) continue; res += f.comb(x + y, x); } cout << res << endl; }
replace
161
162
161
162
0
p02862
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep2(i, s, n) for (int i = (s); i < (int)(n); i++) using namespace std; typedef long long ll; ll mod = 1000000007; ll f[2000102]; void init() { f[0] = 1; rep2(i, 1, 2000100) f[i] = (f[i - 1] * i) % mod; } ll inv(ll x) { ll res = 1; ll k = mod - 2; ll y = x; while (k) { if (k & 1) res = (res * y) % mod; y = (y * y) % mod; k /= 2; } return res; } ll C(int n, int k) { ll a = f[n]; // = n! ll b = f[n - k]; // = (n-k)! ll c = f[k]; // = k! ll bc = (b * c) % mod; return (a * inv(bc)) % mod; } int main() { ll x, y; cin >> x >> y; ll count = 0; ll cur_x = x; ll cur_y = y; while (true) { if (cur_x * 2 == cur_y) break; cur_x--; cur_y++; count++; if (cur_x <= 0) { cout << 0 << endl; return 0; } } ll n = cur_x; ll k = count; // cout << "n:" << n << endl; // cout << "k:" << k << endl; init(); cout << C(n, k) << endl; return 0; }
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep2(i, s, n) for (int i = (s); i < (int)(n); i++) using namespace std; typedef long long ll; ll mod = 1000000007; ll f[2000102]; void init() { f[0] = 1; rep2(i, 1, 2000100) f[i] = (f[i - 1] * i) % mod; } ll inv(ll x) { ll res = 1; ll k = mod - 2; ll y = x; while (k) { if (k & 1) res = (res * y) % mod; y = (y * y) % mod; k /= 2; } return res; } ll C(int n, int k) { ll a = f[n]; // = n! ll b = f[n - k]; // = (n-k)! ll c = f[k]; // = k! ll bc = (b * c) % mod; return (a * inv(bc)) % mod; } int main() { ll x, y; cin >> x >> y; ll count = 0; ll cur_x = x; ll cur_y = y; while (true) { if (cur_x * 2 == cur_y) break; cur_x--; cur_y++; count++; if (cur_x <= 0) { cout << 0 << endl; return 0; } } ll n = cur_x; ll k = count; // cout << "n:" << n << endl; // cout << "k:" << k << endl; if (count > n) { cout << 0 << endl; return 0; } init(); cout << C(n, k) << endl; return 0; }
insert
64
64
64
69
0
p02862
C++
Runtime Error
#include <bits/stdc++.h> #define int long long using namespace std; int a, b; const int hell = 1e9 + 7; int fact[1000001]; int binpower(int a, int b) { int res = 1; while (b) { if (b & 1) (res *= a) %= hell; b /= 2; (a *= a) %= hell; } return res; } int modinverse(int n) { return binpower(n, hell - 2); } int ncr(int n, int r) { if (r == 0) return 1; fact[0] = 1; for (int i = 1; i <= n; i++) { fact[i] = fact[i - 1] * i % hell; } return ((fact[n] * modinverse(fact[r])) % hell * modinverse(fact[n - r]) % hell) % hell; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> a >> b; if ((a + b) % 3 != 0) { cout << 0 << endl; return 0; } int n = (2 * a - b) / 3, m = (2 * b - a) / 3; if (a < 0 or b < 0) { cout << 0 << endl; return 0; } // cout << n <<' ' << m << endl ; cout << ncr(n + m, m) << endl; return 0; }
#include <bits/stdc++.h> #define int long long using namespace std; int a, b; const int hell = 1e9 + 7; int fact[1000001]; int binpower(int a, int b) { int res = 1; while (b) { if (b & 1) (res *= a) %= hell; b /= 2; (a *= a) %= hell; } return res; } int modinverse(int n) { return binpower(n, hell - 2); } int ncr(int n, int r) { if (r == 0) return 1; fact[0] = 1; for (int i = 1; i <= n; i++) { fact[i] = fact[i - 1] * i % hell; } return ((fact[n] * modinverse(fact[r])) % hell * modinverse(fact[n - r]) % hell) % hell; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> a >> b; if ((a + b) % 3 != 0) { cout << 0 << endl; return 0; } int n = (2 * a - b) / 3, m = (2 * b - a) / 3; if (n < 0 or m < 0) { cout << 0 << endl; return 0; } // cout << n <<' ' << m << endl ; cout << ncr(n + m, m) << endl; return 0; }
replace
47
48
47
48
0
p02862
C++
Runtime Error
// #pragma GCC optimize(3, "Ofast", "inline") #include <bits/stdc++.h> #define String std::string #define fir first #define sec second #define mp std::make_pair #define Pair std::pair<int, int> #define Map std::map<int, int> #define Vector std::vector<int> typedef long long ll; typedef unsigned long long ull; const int N = 700000 + 5; const int MOD = 1e9 + 7; const int inv2 = 500000004; const int dx[] = {0, 1, -1, 0, 1, -1, 1, -1}; const int dy[] = {1, 0, 0, -1, 1, -1, -1, 1}; inline ll read() { ll res = 0, f = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -1; for (; isdigit(ch); ch = getchar()) res = (res << 1) + (res << 3) + ch - '0'; return res * f; } inline void exgcd(ll a, ll b, ll &g, ll &x, ll &y) { if (!b) g = a, x = 1, y = 0; else exgcd(b, a % b, g, y, x), y -= x * (a / b); } inline ll inv(ll a) { ll g, res, tmp; exgcd(a, MOD, g, res, tmp); return ((res % MOD) + MOD) % MOD; } ll f[N], fi[N]; inline void prepare() { f[0] = 1; int n = 700000; for (int i = 1; i <= n; i++) f[i] = f[i - 1] * i % MOD; fi[n] = inv(f[n]); for (int i = n - 1; i >= 1; i--) fi[i] = fi[i + 1] * (i + 1) % MOD; } inline ll C(int n, int m) { if (!m) return 1; if (n == m) return 1; if (m > n) return 0; return f[n] * fi[m] % MOD * fi[n - m] % MOD; } int main() { int x, y; int z; prepare(); scanf("%d %d", &x, &y); z = x + y; if (z % 3) { puts("0"); } else { printf("%lld\n", C(z / 3, y - z / 3)); } return 0; }
// #pragma GCC optimize(3, "Ofast", "inline") #include <bits/stdc++.h> #define String std::string #define fir first #define sec second #define mp std::make_pair #define Pair std::pair<int, int> #define Map std::map<int, int> #define Vector std::vector<int> typedef long long ll; typedef unsigned long long ull; const int N = 700000 + 5; const int MOD = 1e9 + 7; const int inv2 = 500000004; const int dx[] = {0, 1, -1, 0, 1, -1, 1, -1}; const int dy[] = {1, 0, 0, -1, 1, -1, -1, 1}; inline ll read() { ll res = 0, f = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -1; for (; isdigit(ch); ch = getchar()) res = (res << 1) + (res << 3) + ch - '0'; return res * f; } inline void exgcd(ll a, ll b, ll &g, ll &x, ll &y) { if (!b) g = a, x = 1, y = 0; else exgcd(b, a % b, g, y, x), y -= x * (a / b); } inline ll inv(ll a) { ll g, res, tmp; exgcd(a, MOD, g, res, tmp); return ((res % MOD) + MOD) % MOD; } ll f[N], fi[N]; inline void prepare() { f[0] = 1; int n = 700000; for (int i = 1; i <= n; i++) f[i] = f[i - 1] * i % MOD; fi[n] = inv(f[n]); for (int i = n - 1; i >= 1; i--) fi[i] = fi[i + 1] * (i + 1) % MOD; } inline ll C(int n, int m) { if (!m) return 1; if (n == m) return 1; if (m > n) return 0; if (m < 0) return 0; return f[n] * fi[m] % MOD * fi[n - m] % MOD; } int main() { int x, y; int z; prepare(); scanf("%d %d", &x, &y); z = x + y; if (z % 3) { puts("0"); } else { printf("%lld\n", C(z / 3, y - z / 3)); } return 0; }
insert
62
62
62
64
0
p02863
C++
Runtime Error
// // sublime text 3, // emre yazicioglu // #include <algorithm> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; typedef pair<ll, ll> pll; typedef vector<ll> vll; typedef vector<pll> vpll; #define pb push_back #define fi first #define se second #define INF (ll)(1e18 + 7) #define MOD (ll)(1e9 + 7) const int N = 2e3 + 7; ll mn = INF, mx = -INF; ll n, m, k, s, q, t; ll a, b, c, d, e; ll ans, cur, sum, cnt; ll w[N], v[N], dp[N][N][2]; string str; bool h[N]; pll pp[N]; ll f(ll cur, ll rem, bool type) { if (cur == n + 1) return 0; if (rem == 0) return 0; ll &r = dp[cur][rem][type]; if (r != -1) return r; r = -INF; if (!type) r = max(r, f(cur + 1, rem - 1, 1) + v[cur]); if (rem >= w[cur]) r = max(r, f(cur + 1, rem - w[cur], type) + v[cur]); r = max(r, f(cur + 1, rem, type)); return r; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); // freopen("../in.txt","r",stdin); // freopen("../out.txt","w",stdout); memset(dp, -1, sizeof dp); cin >> n >> k; for (int i = 1; i <= n; i++) cin >> w[i] >> v[i]; cout << f(1, k, 0); }
// // sublime text 3, // emre yazicioglu // #include <algorithm> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; typedef pair<ll, ll> pll; typedef vector<ll> vll; typedef vector<pll> vpll; #define pb push_back #define fi first #define se second #define INF (ll)(1e18 + 7) #define MOD (ll)(1e9 + 7) const int N = 3e3 + 7; ll mn = INF, mx = -INF; ll n, m, k, s, q, t; ll a, b, c, d, e; ll ans, cur, sum, cnt; ll w[N], v[N], dp[N][N][2]; string str; bool h[N]; pll pp[N]; ll f(ll cur, ll rem, bool type) { if (cur == n + 1) return 0; if (rem == 0) return 0; ll &r = dp[cur][rem][type]; if (r != -1) return r; r = -INF; if (!type) r = max(r, f(cur + 1, rem - 1, 1) + v[cur]); if (rem >= w[cur]) r = max(r, f(cur + 1, rem - w[cur], type) + v[cur]); r = max(r, f(cur + 1, rem, type)); return r; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); // freopen("../in.txt","r",stdin); // freopen("../out.txt","w",stdout); memset(dp, -1, sizeof dp); cin >> n >> k; for (int i = 1; i <= n; i++) cin >> w[i] >> v[i]; cout << f(1, k, 0); }
replace
31
32
31
32
0
p02863
C++
Time Limit Exceeded
#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; int main() { int N, T; cin >> N >> T; vi A(N), B(N); for (int i = 0; i < N; i++) { cin >> A[i] >> B[i]; } vector<vector<vi>> dp(T, vector<vi>(N, vi(2, 0))); for (int t = 0; t < T; t++) { for (int i = 0; i < N; i++) { // 食べる if (t + A[i] < T) { if (i != 0) { dp[t + A[i]][i][0] = max(dp[t + A[i]][i][0], dp[t][i - 1][0] + B[i]); dp[t + A[i]][i][1] = max(dp[t + A[i]][i][1], dp[t][i - 1][1] + B[i]); } else if (i == 0) { dp[t + A[i]][i][0] = max(dp[t + A[i]][i][0], B[i]); dp[t + A[i]][i][1] = max(dp[t + A[i]][i][1], B[i]); } } // 食べない if (i != 0) { dp[t][i][0] = max(dp[t][i][0], dp[t][i - 1][0]); dp[t][i][1] = max(dp[t][i][1], dp[t][i - 1][1]); // デザート dp[t][i][1] = max(dp[t][i][1], dp[t][i - 1][0] + B[i]); } if (t != 0) { dp[t][i][0] = max(dp[t][i][0], dp[t - 1][i][0]); dp[t][i][1] = max(dp[t][i][1], dp[t - 1][i][1]); } } } cout << dp[T - 1][N - 1][1] << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; int main() { int N, T; cin >> N >> T; vi A(N), B(N); for (int i = 0; i < N; i++) { cin >> A[i] >> B[i]; } vector<vector<vi>> dp(T, vector<vi>(N, vi(2, 0))); for (int t = 0; t < T; t++) { for (int i = 0; i < N; i++) { // 食べる if (t + A[i] < T) { if (i != 0) { dp[t + A[i]][i][0] = max(dp[t + A[i]][i][0], dp[t][i - 1][0] + B[i]); dp[t + A[i]][i][1] = max(dp[t + A[i]][i][1], dp[t][i - 1][1] + B[i]); } else if (i == 0) { dp[t + A[i]][i][0] = max(dp[t + A[i]][i][0], B[i]); dp[t + A[i]][i][1] = max(dp[t + A[i]][i][1], B[i]); } } // 食べない if (i != 0) { dp[t][i][0] = max(dp[t][i][0], dp[t][i - 1][0]); dp[t][i][1] = max(dp[t][i][1], dp[t][i - 1][1]); // デザート dp[t][i][1] = max(dp[t][i][1], dp[t][i - 1][0] + B[i]); } if (t != 0) { dp[t][i][0] = max(dp[t][i][0], dp[t - 1][i][0]); dp[t][i][1] = max(dp[t][i][1], dp[t - 1][i][1]); } } } cout << dp[T - 1][N - 1][1] << endl; }
delete
0
1
0
0
TLE
p02863
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define ll long long int ll dp[3005][3005]; ll n, t, res; pair<ll, ll> ar[3005]; ll f(int cur_index, int cur_time) { if (cur_time >= t || cur_index > n) return 0; if (dp[cur_index][cur_time]) return dp[cur_index][cur_time]; ll r = 0; r = max(r, f(cur_index + 1, cur_time)); r = max(r, f(cur_index + 1, cur_time + ar[cur_index].first) + ar[cur_index].second); res = max(r, res); return r; } int main() { cin >> n >> t; for (int i = 1; i <= n; i++) { cin >> ar[i].first >> ar[i].second; } sort(ar + 1, ar + n + 1); f(1, 0); cout << res; }
#include <bits/stdc++.h> using namespace std; #define ll long long int ll dp[3005][3005]; ll n, t, res; pair<ll, ll> ar[3005]; ll f(int cur_index, int cur_time) { if (cur_time >= t || cur_index > n) return 0; if (dp[cur_index][cur_time]) return dp[cur_index][cur_time]; ll r = 0; r = max(r, f(cur_index + 1, cur_time)); r = max(r, f(cur_index + 1, cur_time + ar[cur_index].first) + ar[cur_index].second); res = max(r, res); return dp[cur_index][cur_time] = r; } int main() { cin >> n >> t; for (int i = 1; i <= n; i++) { cin >> ar[i].first >> ar[i].second; } sort(ar + 1, ar + n + 1); f(1, 0); cout << res; }
replace
18
19
18
19
TLE
p02863
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) int main() { int n, t; cin >> n >> t; long long a[n], b[n]; rep(i, n) cin >> a[i] >> b[i]; long long dp[n + 1][t + 3010][2]; // rep(i,t+3010) dp[0][i][0]=0; // rep(i,t+3010) dp[0][i][1]=0; rep(i, n + 1) { rep(j, t + 3010) { dp[i][j][0] = 0; dp[i][j][1] = 0; } } // dp[0][0][0] = 0; // dp[0][0][1] = 1; long long ans = 0; rep(i, n) { rep(j, t + 2) { dp[i + 1][j + a[i]][0] = max(dp[i + 1][j + a[i]][0], dp[i][j][0] + b[i]); dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][0]); dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][0] + b[i]); } rep(j, t + 2) { dp[i + 1][j + a[i]][1] = max(dp[i + 1][j + a[i]][1], dp[i][j][1] + b[i]); dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][1]); } } // rep(i,t+1) ans = max(ans,dp[n][i][0]); // rep(i,t) ans = max(ans,dp[n][i][1]); ans = max(dp[n][t - 1][1], dp[n][t][0]); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) int main() { int n, t; cin >> n >> t; long long a[n], b[n]; rep(i, n) cin >> a[i] >> b[i]; // long long dp[n+1][t+3010][2]; vector<vector<vector<long long>>> dp; vector<vector<long long>> tmp; vector<long long> tmpp = {0, 0}; rep(i, t + 3010) tmp.push_back(tmpp); rep(i, n + 1) { dp.push_back(tmp); } // rep(i,t+3010) dp[0][i][0]=0; // rep(i,t+3010) dp[0][i][1]=0; rep(i, n + 1) { rep(j, t + 3010) { dp[i][j][0] = 0; dp[i][j][1] = 0; } } // dp[0][0][0] = 0; // dp[0][0][1] = 1; long long ans = 0; rep(i, n) { rep(j, t + 2) { dp[i + 1][j + a[i]][0] = max(dp[i + 1][j + a[i]][0], dp[i][j][0] + b[i]); dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][0]); dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][0] + b[i]); } rep(j, t + 2) { dp[i + 1][j + a[i]][1] = max(dp[i + 1][j + a[i]][1], dp[i][j][1] + b[i]); dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][1]); } } // rep(i,t+1) ans = max(ans,dp[n][i][0]); // rep(i,t) ans = max(ans,dp[n][i][1]); ans = max(dp[n][t - 1][1], dp[n][t][0]); cout << ans << endl; }
replace
8
9
8
14
0
p02863
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <string> #include <tuple> #include <utility> #include <vector> using namespace std; void solve(long long N, long long T, std::vector<long long> A, std::vector<long long> B) { vector<pair<long long, long long>> c; for (int i = 0; i < N; ++i) { c.emplace_back(A[i], B[i]); } sort(c.begin(), c.end()); vector<vector<long long>> dp(N, vector<long long>(T, 0)); dp[0][c[0].first] = c[0].second; for (int i = 1; i < N; ++i) { for (int j = 0; j + c[i].first < T; ++j) { dp[i][j + c[i].first] = max(dp[i - 1][j] + c[i].second, dp[i - 1][j + c[i].first]); } } long long last = 0; long long ret = 0; for (int i = N - 1; i >= 0; --i) { for (int j = 0; j < T; ++j) { ret = max(dp[i][j] + last, ret); } last = max(c[i].second, last); } ret = (last, ret); std::cout << ret << std::endl; } int main() { long long N; scanf("%lld", &N); long long T; scanf("%lld", &T); std::vector<long long> A(N); std::vector<long long> B(N); for (int i = 0; i < N; i++) { scanf("%lld", &A[i]); scanf("%lld", &B[i]); } solve(N, T, std::move(A), std::move(B)); return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <string> #include <tuple> #include <utility> #include <vector> using namespace std; void solve(long long N, long long T, std::vector<long long> A, std::vector<long long> B) { vector<pair<long long, long long>> c; for (int i = 0; i < N; ++i) { c.emplace_back(A[i], B[i]); } sort(c.begin(), c.end()); vector<vector<long long>> dp(N, vector<long long>(T, 0)); if (c[0].first < T) { dp[0][c[0].first] = c[0].second; } for (int i = 1; i < N; ++i) { for (int j = 0; j + c[i].first < T; ++j) { dp[i][j + c[i].first] = max(dp[i - 1][j] + c[i].second, dp[i - 1][j + c[i].first]); } } long long last = 0; long long ret = 0; for (int i = N - 1; i >= 0; --i) { for (int j = 0; j < T; ++j) { ret = max(dp[i][j] + last, ret); } last = max(c[i].second, last); } ret = (last, ret); std::cout << ret << std::endl; } int main() { long long N; scanf("%lld", &N); long long T; scanf("%lld", &T); std::vector<long long> A(N); std::vector<long long> B(N); for (int i = 0; i < N; i++) { scanf("%lld", &A[i]); scanf("%lld", &B[i]); } solve(N, T, std::move(A), std::move(B)); return 0; }
replace
25
26
25
28
0
p02863
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<ll, ll>; #define rep(i, n) for (ll i = 0; i < (int)(n); i++) #define ve vector int main() { int n, t; cin >> n >> t; ve<P> ab; rep(i, n) { ll a, b; cin >> a >> b; ab.push_back(P(a, b)); } sort(ab.begin(), ab.end()); ve<ve<ll>> dp1(n + 1, ve<ll>(t + 1)); rep(i, n) { ll a = ab[i].first, b = ab[i].second; rep(j, a) dp1[i + 1][j] = dp1[i][j]; rep(j, t - a + 1) { dp1[i + 1][j + a] = max(dp1[i][j + a], dp1[i][j] + b); } } ll ans = 0; rep(i, n) { ll memo = ab[i].second; ans = max(ans, memo + dp1[i][t - 1]); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<ll, ll>; #define rep(i, n) for (ll i = 0; i < (int)(n); i++) #define ve vector int main() { int n, t; cin >> n >> t; ve<P> ab; rep(i, n) { ll a, b; cin >> a >> b; ab.push_back(P(a, b)); } sort(ab.begin(), ab.end()); ve<ve<ll>> dp1(n + 1, ve<ll>(t + 1)); rep(i, n) { ll a = ab[i].first, b = ab[i].second; rep(j, min(a, (ll)t)) dp1[i + 1][j] = dp1[i][j]; rep(j, t - a + 1) { dp1[i + 1][j + a] = max(dp1[i][j + a], dp1[i][j] + b); } } ll ans = 0; rep(i, n) { ll memo = ab[i].second; ans = max(ans, memo + dp1[i][t - 1]); } cout << ans << endl; return 0; }
replace
22
23
22
23
-6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)
p02863
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> P; int dp[4000][4000][2]; int main() { int N, T; cin >> N >> T; vector<P> v; for (int i = 0; i < N; i++) { int a, b; cin >> a >> b; v.push_back(P(a, b)); } for (int i = 1; i <= N; i++) { for (int j = 0; j < T; j++) { if (j >= v[i - 1].first) { dp[i][j][0] = max(dp[i - 1][j - v[i - 1].first][0] + v[i - 1].second, dp[i - 1][j][0]); dp[i][j][1] = max(dp[i - 1][j][0] + v[i - 1].second, max(dp[i - 1][j - v[i - 1].first][1] + v[i - 1].second, dp[i - 1][j][1])); } else { dp[i][j][0] = dp[i - 1][j][0]; dp[i][j][1] = max(dp[i - 1][j - v[i - 1].first][0] + v[i - 1].second, dp[i - 1][j][1]); } } } int ans = dp[N][T - 1][1]; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> P; int dp[4000][4000][2]; int main() { int N, T; cin >> N >> T; vector<P> v; for (int i = 0; i < N; i++) { int a, b; cin >> a >> b; v.push_back(P(a, b)); } for (int i = 1; i <= N; i++) { for (int j = 0; j < T; j++) { if (j >= v[i - 1].first) { dp[i][j][0] = max(dp[i - 1][j - v[i - 1].first][0] + v[i - 1].second, dp[i - 1][j][0]); dp[i][j][1] = max(dp[i - 1][j][0] + v[i - 1].second, max(dp[i - 1][j - v[i - 1].first][1] + v[i - 1].second, dp[i - 1][j][1])); } else { dp[i][j][0] = dp[i - 1][j][0]; dp[i][j][1] = max(dp[i - 1][j][0] + v[i - 1].second, dp[i - 1][j][1]); } } } int ans = dp[N][T - 1][1]; cout << ans << endl; return 0; }
replace
31
33
31
32
-11
p02863
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL_DEBUG #include "LOCAL_DEBUG.hpp" #endif #define int long long signed main() { int n, t; cin >> n >> t; vector<int> a(n), b(n); for (int i = 0; i < n; i++) { cin >> a[i] >> b[i]; } vector<int> v; vector<int> order(n); iota(order.begin(), order.end(), 0); sort(order.begin(), order.end(), [&](int i, int j) { if (a[i] == a[j]) return b[i] > b[j]; return a[i] < a[j]; }); for (int i = 0; i < min(n, 10LL); i++) { v.push_back(order[i]); } iota(order.begin(), order.end(), 0); sort(order.begin(), order.end(), [&](int i, int j) { if (b[i] == b[j]) return a[i] < a[j]; return b[i] > b[j]; }); for (int i = 0; i < min(n, 10LL); i++) { v.push_back(order[i]); } int ans = 0; for (int u = 0; u < v.size(); u++) { vector<int> dp(t, 0); for (int i = 0; i < n; i++) { if (i == v[u]) continue; for (int j = t; j >= 0; j--) { if (j + a[i] > t) continue; dp[j + a[i]] = max(dp[j + a[i]], dp[j] + b[i]); } } ans = max(ans, dp[t - 1] + b[v[u]]); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL_DEBUG #include "LOCAL_DEBUG.hpp" #endif #define int long long signed main() { int n, t; cin >> n >> t; vector<int> a(n), b(n); for (int i = 0; i < n; i++) { cin >> a[i] >> b[i]; } vector<int> v; vector<int> order(n); iota(order.begin(), order.end(), 0); sort(order.begin(), order.end(), [&](int i, int j) { if (a[i] == a[j]) return b[i] > b[j]; return a[i] < a[j]; }); for (int i = 0; i < min(n, 10LL); i++) { v.push_back(order[i]); } iota(order.begin(), order.end(), 0); sort(order.begin(), order.end(), [&](int i, int j) { if (b[i] == b[j]) return a[i] < a[j]; return b[i] > b[j]; }); for (int i = 0; i < min(n, 10LL); i++) { v.push_back(order[i]); } int ans = 0; for (int u = 0; u < v.size(); u++) { vector<int> dp(t + 10, 0); for (int i = 0; i < n; i++) { if (i == v[u]) continue; for (int j = t; j >= 0; j--) { if (j + a[i] > t) continue; dp[j + a[i]] = max(dp[j + a[i]], dp[j] + b[i]); } } ans = max(ans, dp[t - 1] + b[v[u]]); } cout << ans << endl; return 0; }
replace
41
42
41
42
0
p02863
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> using namespace std; const int MAX_N = 3000; const int MAX_T = 3000; int N, T; vector<int> As(MAX_N); vector<int> Bs(MAX_N); // memo1[i][j] := 0 ~ iでj分以内で食べられる最大値 vector<vector<int>> memo1(MAX_N, vector<int>(MAX_T + 1, -1)); // memo2[i][j] := i ~ T - 1でj分以内で食べられる最大値 vector<vector<int>> memo2(MAX_N, vector<int>(MAX_T + 1, -1)); int dp1(int i, int j) { if (memo1[i][j] == -1) { if (i == 0) { if (j >= As[i]) { memo1[i][j] = Bs[i]; } else { memo1[i][j] = 0; } } else { if (j >= As[i]) { memo1[i][j] = max(dp1(i - 1, j), dp1(i - 1, j - As[i]) + Bs[i]); } else { memo1[i][j] = dp1(i - 1, j); } } } return memo1[i][j]; } int dp2(int i, int j) { if (memo2[i][j] == -1) { if (i == T - 1) { if (j >= As[i]) { memo2[i][j] = Bs[i]; } else { memo2[i][j] = 0; } } else { if (j >= As[i]) { memo2[i][j] = max(dp2(i + 1, j), dp2(i + 1, j - As[i]) + Bs[i]); } else { memo2[i][j] = dp2(i + 1, j); } } } return memo2[i][j]; } int main() { cin >> N >> T; for (int i = 0; i < N; i++) { cin >> As[i]; cin >> Bs[i]; } vector<int> ans(N, -1); for (int i = 0; i < N; i++) { for (int j = 0; j < T; j++) { if (i == 0) { ans[i] = max(ans[i], Bs[i] + dp2(i + 1, T - 1 - j)); } else if (i == N - 1) { ans[i] = max(ans[i], Bs[i] + dp1(i - 1, j)); } else { ans[i] = max(ans[i], Bs[i] + dp1(i - 1, j) + dp2(i + 1, T - 1 - j)); } } } int r = ans[0]; for (int i = 0; i < N; i++) { r = max(r, ans[i]); } cout << r << endl; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; const int MAX_N = 3000; const int MAX_T = 3000; int N, T; vector<int> As(MAX_N); vector<int> Bs(MAX_N); // memo1[i][j] := 0 ~ iでj分以内で食べられる最大値 vector<vector<int>> memo1(MAX_N, vector<int>(MAX_T + 1, -1)); // memo2[i][j] := i ~ T - 1でj分以内で食べられる最大値 vector<vector<int>> memo2(MAX_N, vector<int>(MAX_T + 1, -1)); int dp1(int i, int j) { if (memo1[i][j] == -1) { if (i == 0) { if (j >= As[i]) { memo1[i][j] = Bs[i]; } else { memo1[i][j] = 0; } } else { if (j >= As[i]) { memo1[i][j] = max(dp1(i - 1, j), dp1(i - 1, j - As[i]) + Bs[i]); } else { memo1[i][j] = dp1(i - 1, j); } } } return memo1[i][j]; } int dp2(int i, int j) { if (memo2[i][j] == -1) { if (i == N - 1) { if (j >= As[i]) { memo2[i][j] = Bs[i]; } else { memo2[i][j] = 0; } } else { if (j >= As[i]) { memo2[i][j] = max(dp2(i + 1, j), dp2(i + 1, j - As[i]) + Bs[i]); } else { memo2[i][j] = dp2(i + 1, j); } } } return memo2[i][j]; } int main() { cin >> N >> T; for (int i = 0; i < N; i++) { cin >> As[i]; cin >> Bs[i]; } vector<int> ans(N, -1); for (int i = 0; i < N; i++) { for (int j = 0; j < T; j++) { if (i == 0) { ans[i] = max(ans[i], Bs[i] + dp2(i + 1, T - 1 - j)); } else if (i == N - 1) { ans[i] = max(ans[i], Bs[i] + dp1(i - 1, j)); } else { ans[i] = max(ans[i], Bs[i] + dp1(i - 1, j) + dp2(i + 1, T - 1 - j)); } } } int r = ans[0]; for (int i = 0; i < N; i++) { r = max(r, ans[i]); } cout << r << endl; }
replace
38
39
38
39
0
p02863
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } int main() { ll n, t; cin >> n >> t; vector<vector<ll>> dp(t, vector<ll>(2)); for (ll i = 0; i < n; i++) { ll a, b; cin >> a >> b; for (ll i = t; i-- > a;) chmax(dp[i][1], dp[i - a][1] + b); for (ll i = 0; i < t; i++) chmax(dp[i][1], dp[i][0] + b); for (ll i = t; i >= a; i--) chmax(dp[i][0], dp[i - a][0] + b); } cout << max(dp[t - 1][0], dp[t - 1][1]) << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } int main() { ll n, t; cin >> n >> t; vector<vector<ll>> dp(t, vector<ll>(2)); for (ll i = 0; i < n; i++) { ll a, b; cin >> a >> b; for (ll i = t; i-- > a;) chmax(dp[i][1], dp[i - a][1] + b); for (ll i = 0; i < t; i++) chmax(dp[i][1], dp[i][0] + b); for (ll i = t; i-- > a;) chmax(dp[i][0], dp[i - a][0] + b); } cout << max(dp[t - 1][0], dp[t - 1][1]) << endl; }
replace
22
23
22
23
-11
p02863
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <vector> using namespace std; using lint = long long; constexpr int MOD = 1000000007, INF = 1010101010; constexpr lint LINF = 1LL << 60; template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) { for (const auto &e : vec) os << e << (&e == &vec.back() ? "\n" : " "); return os; } #ifdef _DEBUG template <class T> void dump(const char *str, T &&h) { cerr << str << " = " << h << "\n"; }; template <class Head, class... Tail> void dump(const char *str, Head &&h, Tail &&...t) { while (*str != ',') cerr << *str++; cerr << " = " << h << "\n"; dump(str + (*(str + 1) == ' ' ? 2 : 1), t...); } #define DMP(...) dump(#__VA_ARGS__, __VA_ARGS__) #else #define DMP(...) ((void)0) #endif template <class T> inline bool chmax(T &a, const T b) { return a < b && (a = b, true); } template <class T> inline bool chmin(T &a, const T b) { return a > b && (a = b, true); } int naive(const vector<pair<int, int>> &AB, const int T) { int res = 0; for (int bit = 0; bit < (1 << AB.size()); bit++) { int last = 0, sum = 0, time = 0; for (int i = 0; i < AB.size(); i++) { if (bit & (1 << i)) { int tmp = last; if (chmax(last, AB[i].first)) time += tmp; else time += AB[i].first; sum += AB[i].second; } if (time >= T) break; } if (time < T) if (chmax(res, sum)) DMP(bit); } return res; } template <class T> vector<T> make_vec(size_t s, T val) { return vector<T>(s, val); } template <class... Size> auto make_vec(size_t s, Size... tail) { return vector<decltype(make_vec(tail...))>(s, make_vec(tail...)); } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int N, T; cin >> N >> T; vector<pair<int, int>> AB(N); for (int i = 0; i < N; i++) cin >> AB[i].first >> AB[i].second; sort(AB.begin(), AB.end()); vector<int> dp(N + 1); for (int i = 1; i <= N; i++) { for (int j = 0; j < T; j++) { chmax(dp[min(j + AB[i - 1].first, T)], dp[j] + AB[i - 1].second); } } cout << dp[T] << "\n"; DMP(naive(AB, T)); return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <vector> using namespace std; using lint = long long; constexpr int MOD = 1000000007, INF = 1010101010; constexpr lint LINF = 1LL << 60; template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) { for (const auto &e : vec) os << e << (&e == &vec.back() ? "\n" : " "); return os; } #ifdef _DEBUG template <class T> void dump(const char *str, T &&h) { cerr << str << " = " << h << "\n"; }; template <class Head, class... Tail> void dump(const char *str, Head &&h, Tail &&...t) { while (*str != ',') cerr << *str++; cerr << " = " << h << "\n"; dump(str + (*(str + 1) == ' ' ? 2 : 1), t...); } #define DMP(...) dump(#__VA_ARGS__, __VA_ARGS__) #else #define DMP(...) ((void)0) #endif template <class T> inline bool chmax(T &a, const T b) { return a < b && (a = b, true); } template <class T> inline bool chmin(T &a, const T b) { return a > b && (a = b, true); } int naive(const vector<pair<int, int>> &AB, const int T) { int res = 0; for (int bit = 0; bit < (1 << AB.size()); bit++) { int last = 0, sum = 0, time = 0; for (int i = 0; i < AB.size(); i++) { if (bit & (1 << i)) { int tmp = last; if (chmax(last, AB[i].first)) time += tmp; else time += AB[i].first; sum += AB[i].second; } if (time >= T) break; } if (time < T) if (chmax(res, sum)) DMP(bit); } return res; } template <class T> vector<T> make_vec(size_t s, T val) { return vector<T>(s, val); } template <class... Size> auto make_vec(size_t s, Size... tail) { return vector<decltype(make_vec(tail...))>(s, make_vec(tail...)); } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int N, T; cin >> N >> T; vector<pair<int, int>> AB(N); for (int i = 0; i < N; i++) cin >> AB[i].first >> AB[i].second; sort(AB.begin(), AB.end()); vector<int> dp(T + 1); for (int i = 0; i < N; i++) { for (int j = T - 1; j >= 0; j--) { chmax(dp[min(j + AB[i].first, T)], dp[j] + AB[i].second); } } cout << dp[T] << "\n"; DMP(naive(AB, T)); return 0; }
replace
95
99
95
99
0
p02863
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; int mod = 1000000007; int main(void) { vector<int> val_list; int N, T; cin >> N >> T; int A[N + 1], B[N + 1]; A[0] = 0; B[0] = 0; rep(i, N) { cin >> A[i + 1] >> B[i + 1]; } pair<pair<int, int>, int> dp[N + 1][T]; // 遷移元と美味しさのpair rep(i, N + 1) rep(j, T) dp[i][j] = make_pair(make_pair(0, 0), 0); for (int i = 1; i <= N; i++) { int a = A[i]; int b = B[i]; for (int j = 0; j < T; j++) { if (j - a >= 0) { if (dp[i - 1][j].second < dp[i - 1][j - a].second + b) { dp[i][j] = make_pair(make_pair(i - 1, j - a), dp[i - 1][j - a].second + b); } else dp[i][j] = make_pair(make_pair(i - 1, j), dp[i - 1][j].second); } else { dp[i][j] = make_pair(make_pair(i - 1, j), dp[i - 1][j].second); } } } // cout << "reach" << endl; pair<int, int> nl = make_pair(0, 0); pair<pair<int, int>, int> op = dp[N][T - 1]; while (op.first != nl) { if (dp[op.first.first][op.first.second].second != op.second) { val_list.push_back(op.second - dp[op.first.first][op.first.second].second); } op = dp[op.first.first][op.first.second]; } sort(val_list.begin(), val_list.end(), greater<int>()); sort(B, B + N + 1, greater<int>()); rep(i, N + 1) { if (val_list[i] != B[i]) { cout << dp[N][T - 1].second + B[i] << endl; return 0; } } cout << dp[N][T - 1].second << 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; int mod = 1000000007; int main(void) { vector<int> val_list; int N, T; cin >> N >> T; int A[N + 1], B[N + 1]; A[0] = 0; B[0] = 0; rep(i, N) { cin >> A[i + 1] >> B[i + 1]; } pair<pair<int, int>, int> dp[N + 1][T]; // 遷移元と美味しさのpair rep(i, N + 1) rep(j, T) dp[i][j] = make_pair(make_pair(0, 0), 0); for (int i = 1; i <= N; i++) { int a = A[i]; int b = B[i]; for (int j = 0; j < T; j++) { if (j - a >= 0) { if (dp[i - 1][j].second < dp[i - 1][j - a].second + b) { dp[i][j] = make_pair(make_pair(i - 1, j - a), dp[i - 1][j - a].second + b); } else dp[i][j] = make_pair(make_pair(i - 1, j), dp[i - 1][j].second); } else { dp[i][j] = make_pair(make_pair(i - 1, j), dp[i - 1][j].second); } } } // cout << "reach" << endl; pair<int, int> nl = make_pair(0, 0); pair<pair<int, int>, int> op = dp[N][T - 1]; while (op.first != nl) { if (dp[op.first.first][op.first.second].second != op.second) { val_list.push_back(op.second - dp[op.first.first][op.first.second].second); } op = dp[op.first.first][op.first.second]; } sort(val_list.begin(), val_list.end(), greater<int>()); sort(B, B + N + 1, greater<int>()); rep(i, val_list.size() + 1) { if (i == val_list.size()) { cout << dp[N][T - 1].second + B[i] << endl; return 0; } if (val_list[i] != B[i]) { cout << dp[N][T - 1].second + B[i] << endl; return 0; } } cout << dp[N][T - 1].second << endl; return 0; }
replace
46
47
46
51
0
p02863
C++
Runtime Error
#include <bits/stdc++.h> #define ff first #define ss second #define pb push_back #define mp make_pair using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<ull, ull> pull; typedef pair<ll, ll> pii; typedef pair<ld, ld> pld; typedef long double ld; pll a[3009]; ll dp[3009][8009]; ll ans, n, t; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); cin >> n >> t; for (int i = 1; i <= n; ++i) cin >> a[i].ff >> a[i].ss; sort(a + 1, a + n + 1); for (int i = 1; i <= n; ++i) for (int j = 0; j < t; ++j) { dp[i][j] = max(dp[i][j], dp[i - 1][j]); dp[i][j + a[i].ff] = max(dp[i][j + a[i].ff], dp[i - 1][j] + a[i].ss); ans = max(ans, dp[i][j + a[i].ff]); } cout << ans << "\n"; }
#include <bits/stdc++.h> #define ff first #define ss second #define pb push_back #define mp make_pair using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<ull, ull> pull; typedef pair<ll, ll> pii; typedef pair<ld, ld> pld; typedef long double ld; pll a[3009]; ll dp[3009][8009]; ll ans, n, t; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); cin >> n >> t; for (int i = 1; i <= n; ++i) cin >> a[i].ff >> a[i].ss; sort(a + 1, a + n + 1); for (int i = 1; i <= n; ++i) for (int j = 0; j < t; ++j) { dp[i][j] = max(dp[i][j], dp[i - 1][j]); dp[i][j + a[i].ff] = max(dp[i][j + a[i].ff], dp[i - 1][j] + a[i].ss); ans = max(ans, dp[i][j + a[i].ff]); } cout << ans << "\n"; }
replace
21
23
21
23
-11
p02863
Python
Runtime Error
N, W = map(int, input().split()) items = sorted([list(map(int, input().split())) for i in range(N)]) # dp1[i][j] := 商品0 ~ i が対象 dp = [[0] * (W + 1) for i in range(N + 1)] # dpテーブルを埋める for i in range(N): wi, vi = items[i] for j in range(W + 1): if j + wi <= W: dp[i + 1][j + wi] = max(dp[i + 1][j + wi], dp[i][j] + vi) dp[i + 1][j] = max(dp[i][j], dp[i + 1][j]) ans = 0 for i, (wi, vi) in enumerate(items): for j in range(W): ans = max(ans, dp[i] + vi) print(ans)
N, W = map(int, input().split()) items = sorted([list(map(int, input().split())) for i in range(N)]) # dp1[i][j] := 商品0 ~ i が対象 dp = [[0] * (W + 1) for i in range(N + 1)] # dpテーブルを埋める for i in range(N): wi, vi = items[i] for j in range(W + 1): if j + wi <= W: dp[i + 1][j + wi] = max(dp[i + 1][j + wi], dp[i][j] + vi) dp[i + 1][j] = max(dp[i][j], dp[i + 1][j]) ans = 0 for i, (wi, vi) in enumerate(items): for j in range(W): ans = max(ans, dp[i][j] + vi) print(ans)
replace
18
19
18
19
TypeError: can only concatenate list (not "int") to list
Traceback (most recent call last): File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02863/Python/s826740020.py", line 19, in <module> ans = max(ans, dp[i] + vi) TypeError: can only concatenate list (not "int") to list
p02863
Python
Time Limit Exceeded
def solve(n, t, ab_list): ab_list_s = sorted(ab_list, key=lambda x: x[0]) res = 0 dp = [0] * t for i in range(n): for s in range(t - 1, -1, -1): if dp[s] > 0 or s == 0: u = s + ab_list_s[i][0] r = dp[s] + ab_list_s[i][1] if u >= t: res = max(res, r) else: dp[u] = max(dp[u], r) return max(res, max(dp)) def main(): n, t = map(int, input().split()) ab_list = [] for _ in range(n): a, b = map(int, input().split()) ab_list.append([a, b]) res = solve(n, t, ab_list) print(res) def test(): assert solve(2, 60, [[10, 10], [100, 100]]) == 110 assert solve(3, 60, [[10, 10], [10, 20], [10, 30]]) == 60 assert solve(3, 60, [[30, 10], [30, 20], [30, 30]]) == 50 if __name__ == "__main__": test() main()
def solve(n, t, ab_list): ab_list_s = sorted(ab_list, key=lambda x: x[0]) res = 0 dp = [0] * t for i in range(n): for s in range(t - 1, -1, -1): if dp[s] > 0 or s == 0: u = s + ab_list_s[i][0] r = dp[s] + ab_list_s[i][1] if u >= t: res = max(res, r) else: dp[u] = max(dp[u], r) return max(res, max(dp)) def main(): n, t = map(int, input().split()) ab_list = [] for _ in range(n): a, b = map(int, input().split()) ab_list.append([a, b]) res = solve(n, t, ab_list) print(res) def test(): assert solve(2, 60, [[10, 10], [100, 100]]) == 110 assert solve(3, 60, [[10, 10], [10, 20], [10, 30]]) == 60 assert solve(3, 60, [[30, 10], [30, 20], [30, 30]]) == 50 if __name__ == "__main__": # test() main()
replace
33
34
33
34
TLE
p02863
Python
Runtime Error
n, t = map(int, input().split()) x = [list(map(int, input().split())) for _ in range(n)] a_max = max([y[1] for y in x]) dp_pre = [0 for j in range(t + a_max)] dp = [0 for j in range(t + a_max)] for ai, bi in x: for j in range(ai): dp[j] = dp_pre[j] for j in range(ai, t + ai): dp[j] = max(dp_pre[j], dp_pre[j - ai] + bi) for j in range(t + ai, t + a_max): dp[j] = dp_pre[j] dp_pre, dp = dp, dp_pre print(max(dp_pre))
n, t = map(int, input().split()) x = [list(map(int, input().split())) for _ in range(n)] x.sort() a_max = x[-1][0] dp_pre = [0 for j in range(t + a_max)] dp = [0 for j in range(t + a_max)] for ai, bi in x: for j in range(ai): dp[j] = dp_pre[j] for j in range(ai, t + ai): dp[j] = max(dp_pre[j], dp_pre[j - ai] + bi) for j in range(t + ai, t + a_max): dp[j] = dp_pre[j] dp_pre, dp = dp, dp_pre print(max(dp_pre))
replace
2
3
2
4
0
p02863
Python
Time Limit Exceeded
import sys read = sys.stdin.read readline = sys.stdin.readline readlines = sys.stdin.readlines sys.setrecursionlimit(10**9) INF = 1 << 60 MOD = 1000000007 def main(): N, T, *AB = map(int, read().split()) D = [(a, b) for a, b in zip(*[iter(AB)] * 2)] D.sort() dp = [0] * T ans = 0 for i in range(N - 1): for t in range(T - 1, -1, -1): if t - D[i][0] >= 0 and dp[t] < dp[t - D[i][0]] + D[i][1]: dp[t] = dp[t - D[i][0]] + D[i][1] if ans < dp[T - 1] + D[i + 1][1]: ans = dp[T - 1] + D[i + 1][1] print(ans) return if __name__ == "__main__": main()
import sys read = sys.stdin.read readline = sys.stdin.readline readlines = sys.stdin.readlines sys.setrecursionlimit(10**9) INF = 1 << 60 MOD = 1000000007 def main(): N, T, *AB = map(int, read().split()) D = [(a, b) for a, b in zip(*[iter(AB)] * 2)] D.sort() dp = [0] * T ans = 0 for i, (a, b) in enumerate(D[:-1]): for t in range(T - 1, a - 1, -1): if dp[t] < dp[t - a] + b: dp[t] = dp[t - a] + b if ans < dp[T - 1] + D[i + 1][1]: ans = dp[T - 1] + D[i + 1][1] print(ans) return if __name__ == "__main__": main()
replace
19
23
19
23
TLE
p02863
Python
Time Limit Exceeded
N, T = map(int, input().split()) AB = [] for i in range(N): a, b = map(int, input().split()) AB.append((a, b)) AB.sort() ans = 0 dp = dp = [[0 for _ in range(3005)] for _ in range(3005)] for i in range(N): a, b = AB[i] for j in range(T): # iを使わないパターン dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]) # iを使うパターン nj = j + a if nj < T: dp[i + 1][nj] = max(dp[i + 1][nj], dp[i][j] + b) now = dp[i][T - 1] + b ans = max(ans, now) print(ans)
N, T = map(int, input().split()) AB = [] for i in range(N): a, b = map(int, input().split()) AB.append((a, b)) AB.sort() ans = 0 dp = [[0 for _ in range(3005)] for _ in range(3005)] for i in range(N): a, b = AB[i] for j in range(T): # iを使わないパターン dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]) # iを使うパターン nj = j + a if nj < T: dp[i + 1][nj] = max(dp[i + 1][nj], dp[i][j] + b) now = dp[i][T - 1] + b ans = max(ans, now) print(ans)
replace
8
9
8
9
TLE
p02863
C++
Runtime Error
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #define loop(i, n) for (ll i = 0; i < n; i++) #define rep(i, x, n) for (ll i = x; i <= n; i++) using namespace std; using namespace __gnu_pbds; typedef long long ll; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; const ll N = 3e3 + 5, M = 5e5 + 5, MOD = 1e9 + 7, MAX = 5e6, OO = 1e9; int memo[N][N], n, t; pair<int, int> arr[N]; int dp(int i, int sum) { if (i >= n) return 0; if (sum >= t) return 0; int &res = memo[i][sum]; if (res != -1) return res; return res = max(dp(i + 1, sum + arr[i].first) + arr[i].second, dp(i + 1, sum)); } int main() { #ifndef ONLINE_JUDGE freopen("in.txt", "rt", stdin); #endif ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); memset(memo, -1, sizeof(memo)); cin >> n >> t; loop(i, n) { cin >> arr[i].first >> arr[i].second; } sort(arr, arr + n); cout << dp(0, 0) << endl; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #define loop(i, n) for (ll i = 0; i < n; i++) #define rep(i, x, n) for (ll i = x; i <= n; i++) using namespace std; using namespace __gnu_pbds; typedef long long ll; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; const ll N = 3e3 + 5, M = 5e5 + 5, MOD = 1e9 + 7, MAX = 5e6, OO = 1e9; int memo[N][N], n, t; pair<int, int> arr[N]; int dp(int i, int sum) { if (i >= n) return 0; if (sum >= t) return 0; int &res = memo[i][sum]; if (res != -1) return res; return res = max(dp(i + 1, sum + arr[i].first) + arr[i].second, dp(i + 1, sum)); } int main() { #ifndef ONLINE_JUDGE // freopen("in.txt", "rt", stdin); #endif ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); memset(memo, -1, sizeof(memo)); cin >> n >> t; loop(i, n) { cin >> arr[i].first >> arr[i].second; } sort(arr, arr + n); cout << dp(0, 0) << endl; }
replace
29
30
29
30
0
p02863
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int maxn = 3000 + 5; pii dish[maxn]; int dp[maxn][2 * maxn + 5], t; bool cmp(const pii &p1, const pii &p2) { if ((p1.first < t && p2.first >= t) || (p1.first >= t && p2.first < t)) return p1.first < p2.first; if ((double)p1.second / p1.first != (double)p2.second / p2.first) return (double)p1.second / p1.first > (double)p2.second / p2.first; else return p1.first <= p2.first; } int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n >> t; for (int i = 1; i <= n; ++i) cin >> dish[i].first >> dish[i].second; sort(dish + 1, dish + 1 + n, cmp); for (int i = 1; i <= n; ++i) for (int j = 0; j <= 2 * maxn; ++j) if (j < dish[i].first || j - dish[i].first >= t) dp[i][j] = dp[i - 1][j]; else dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - dish[i].first] + dish[i].second); cout << *max_element(dp[n], dp[n] + 2 * maxn + 3) << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int maxn = 3000 + 5; pii dish[maxn]; int dp[maxn][2 * maxn + 5], t; bool cmp(const pii &p1, const pii &p2) { return p1.first < p2.first; } int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n >> t; for (int i = 1; i <= n; ++i) cin >> dish[i].first >> dish[i].second; sort(dish + 1, dish + 1 + n, cmp); for (int i = 1; i <= n; ++i) for (int j = 0; j <= 2 * maxn; ++j) if (j < dish[i].first || j - dish[i].first >= t) dp[i][j] = dp[i - 1][j]; else dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - dish[i].first] + dish[i].second); cout << *max_element(dp[n], dp[n] + 2 * maxn + 3) << '\n'; return 0; }
replace
8
16
8
9
0
p02863
C++
Runtime Error
#include <algorithm> #include <assert.h> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <typeinfo> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; using ll = long long; using ull = unsigned long long; const ll INF = 1e18; const ll MOD = 1e9 + 7; #define REP(i, n) for (ll i = 0; i < n; i++) int main() { ll n, t; cin >> n >> t; vector<pair<ll, ll>> v(n); REP(i, n) { cin >> v[i].first >> v[i].second; } sort(v.begin(), v.end()); vector<ll> dp(t + 3000); REP(i, n) { vector<ll> tmp(t); REP(j, t) { tmp[j + v[i].first] = dp[j] + v[i].second; } REP(j, t + 3000) { dp[j] = max(dp[j], tmp[j]); } } cout << *max_element(dp.begin(), dp.end()) << endl; }
#include <algorithm> #include <assert.h> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <typeinfo> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; using ll = long long; using ull = unsigned long long; const ll INF = 1e18; const ll MOD = 1e9 + 7; #define REP(i, n) for (ll i = 0; i < n; i++) int main() { ll n, t; cin >> n >> t; vector<pair<ll, ll>> v(n); REP(i, n) { cin >> v[i].first >> v[i].second; } sort(v.begin(), v.end()); vector<ll> dp(t + 3000); REP(i, n) { vector<ll> tmp(t + 3000); REP(j, t) { tmp[j + v[i].first] = dp[j] + v[i].second; } REP(j, t + 3000) { dp[j] = max(dp[j], tmp[j]); } } cout << *max_element(dp.begin(), dp.end()) << endl; }
replace
38
39
38
39
-6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)
p02863
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T &t) { t = 0; char ch = getchar(); int f = 1; while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } do { (t *= 10) += ch - '0'; ch = getchar(); } while ('0' <= ch && ch <= '9'); t *= f; } const int maxn = 3010; int n, T, a[maxn], b[maxn]; int dp[maxn][maxn], ans; int dp2[maxn][maxn], mx[maxn][maxn]; void update(int &x, int y) { x = max(x, y); } int main() { // freopen("1.txt","r",stdin); read(n); read(T); T--; for (int i = 1; i <= n; i++) read(a[i]), read(b[i]); for (int i = 1; i <= n; i++) { for (int j = 0; j <= T; j++) update(dp[i][j], dp[i - 1][j]); for (int j = 0; j + a[i] <= T; j++) update(dp[i][j + a[i]], dp[i - 1][j] + b[i]); } for (int i = n; i >= 1; i--) { for (int j = 0; j <= T; j++) update(dp2[i][j], dp2[i + 1][j]); for (int j = 0; j + a[i] <= T; j++) update(dp2[i][j + a[i]], dp2[i + 1][j] + b[i]); } for (int i = 1; i <= n; i++) { mx[i][0] = dp2[i][0]; for (int j = 1; j <= T; j++) mx[i][j] = max(mx[i][j - 1], dp2[i][j]); } for (int i = 1; i <= n; i++) { for (int j = 0; j <= T; j++) for (int k = 0; k + j <= T; k++) { update(ans, dp[i - 1][j] + dp2[i + 1][k] + b[i]); } } printf("%d\n", ans); return 0; } /* REMEMBER: 1. Think TWICE, Code ONCE! Are there any counterexamples to your algo? 2. Be careful about the BOUNDARIES! N=1? P=1? Something about 0? 3. Do not make STUPID MISTAKES! Array size? Integer overflow? Time complexity? Memory usage? Precision error? */
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T &t) { t = 0; char ch = getchar(); int f = 1; while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } do { (t *= 10) += ch - '0'; ch = getchar(); } while ('0' <= ch && ch <= '9'); t *= f; } const int maxn = 3010; int n, T, a[maxn], b[maxn]; int dp[maxn][maxn], ans; int dp2[maxn][maxn], mx[maxn][maxn]; void update(int &x, int y) { x = max(x, y); } int main() { // freopen("1.txt","r",stdin); read(n); read(T); T--; for (int i = 1; i <= n; i++) read(a[i]), read(b[i]); for (int i = 1; i <= n; i++) { for (int j = 0; j <= T; j++) update(dp[i][j], dp[i - 1][j]); for (int j = 0; j + a[i] <= T; j++) update(dp[i][j + a[i]], dp[i - 1][j] + b[i]); } for (int i = n; i >= 1; i--) { for (int j = 0; j <= T; j++) update(dp2[i][j], dp2[i + 1][j]); for (int j = 0; j + a[i] <= T; j++) update(dp2[i][j + a[i]], dp2[i + 1][j] + b[i]); } for (int i = 1; i <= n; i++) { mx[i][0] = dp2[i][0]; for (int j = 1; j <= T; j++) mx[i][j] = max(mx[i][j - 1], dp2[i][j]); } for (int i = 1; i <= n; i++) { for (int j = 0; j <= T; j++) update(ans, dp[i - 1][j] + mx[i + 1][T - j] + b[i]); } printf("%d\n", ans); return 0; } /* REMEMBER: 1. Think TWICE, Code ONCE! Are there any counterexamples to your algo? 2. Be careful about the BOUNDARIES! N=1? P=1? Something about 0? 3. Do not make STUPID MISTAKES! Array size? Integer overflow? Time complexity? Memory usage? Precision error? */
replace
48
51
48
49
TLE
p02863
C++
Runtime Error
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <unordered_map> #include <vector> #define MOD 1000000007 #define int long long // #define PI 3.14159265358979 #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; template <typename T> ostream &operator<<(ostream &os, const vector<T> &A) { for (int i = 0; i < A.size(); i++) os << A[i] << " "; os << endl; return os; } template <> ostream &operator<<(ostream &os, const vector<vector<int>> &A) { int N = A.size(); int M; if (N > 0) M = A[0].size(); for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) os << A[i][j] << " "; os << endl; } return os; } typedef pair<int, int> pii; typedef long long ll; struct edge { int from, to, d, c; edge(int _from = 0, int _to = 0, int _d = 0, int _c = 0) { from = _from; to = _to; d = _d; c = _c; } bool operator<(const edge &rhs) const { return (d == rhs.d) ? (c < rhs.c) : (d < rhs.d); } }; typedef vector<edge> edges; typedef vector<edges> graph; struct flow { int to, cap, rev, cost; flow(int to = 0, int cap = 0, int rev = 0, int cost = 0) : to(to), cap(cap), rev(rev), cost(cost) {} }; typedef vector<vector<flow>> flows; const int di[4] = {0, -1, 0, 1}; const int dj[4] = {-1, 0, 1, 0}; const int ci[5] = {0, 0, -1, 0, 1}; const int cj[5] = {0, -1, 0, 1, 0}; const ll LINF = LLONG_MAX / 2; const int INF = INT_MAX / 2; const double PI = acos(-1); template <typename T, typename U> bool chmin(T &x, const U &y) { if (x > y) { x = y; return true; } return false; } template <typename T, typename U> bool chmax(T &x, const U &y) { if (x < y) { x = y; return true; } return false; } struct initializer { initializer() { cout << fixed << setprecision(11); } }; initializer _____; int N, M, K, T, Q; signed main() { cin >> N >> T; vector<pii> A(N); rep(i, N) cin >> A[i].first >> A[i].second; sort(A.begin(), A.end()); vector<int> dp(T * 2 + 10, -1); dp[0] = 0; rep(i, N) { for (int j = dp.size() - 1; j >= 0; j--) { if (dp[j] != -1 && j < T && dp[j + A[i].first] < dp[j] + A[i].second) { dp[j + A[i].first] = dp[j] + A[i].second; } } } int ans = 0; rep(i, dp.size()) chmax(ans, dp[i]); cout << ans << endl; return 0; }
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <unordered_map> #include <vector> #define MOD 1000000007 #define int long long // #define PI 3.14159265358979 #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; template <typename T> ostream &operator<<(ostream &os, const vector<T> &A) { for (int i = 0; i < A.size(); i++) os << A[i] << " "; os << endl; return os; } template <> ostream &operator<<(ostream &os, const vector<vector<int>> &A) { int N = A.size(); int M; if (N > 0) M = A[0].size(); for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) os << A[i][j] << " "; os << endl; } return os; } typedef pair<int, int> pii; typedef long long ll; struct edge { int from, to, d, c; edge(int _from = 0, int _to = 0, int _d = 0, int _c = 0) { from = _from; to = _to; d = _d; c = _c; } bool operator<(const edge &rhs) const { return (d == rhs.d) ? (c < rhs.c) : (d < rhs.d); } }; typedef vector<edge> edges; typedef vector<edges> graph; struct flow { int to, cap, rev, cost; flow(int to = 0, int cap = 0, int rev = 0, int cost = 0) : to(to), cap(cap), rev(rev), cost(cost) {} }; typedef vector<vector<flow>> flows; const int di[4] = {0, -1, 0, 1}; const int dj[4] = {-1, 0, 1, 0}; const int ci[5] = {0, 0, -1, 0, 1}; const int cj[5] = {0, -1, 0, 1, 0}; const ll LINF = LLONG_MAX / 2; const int INF = INT_MAX / 2; const double PI = acos(-1); template <typename T, typename U> bool chmin(T &x, const U &y) { if (x > y) { x = y; return true; } return false; } template <typename T, typename U> bool chmax(T &x, const U &y) { if (x < y) { x = y; return true; } return false; } struct initializer { initializer() { cout << fixed << setprecision(11); } }; initializer _____; int N, M, K, T, Q; signed main() { cin >> N >> T; vector<pii> A(N); rep(i, N) cin >> A[i].first >> A[i].second; sort(A.begin(), A.end()); vector<int> dp(10000, -1); dp[0] = 0; rep(i, N) { for (int j = dp.size() - 1; j >= 0; j--) { if (dp[j] != -1 && j < T && dp[j + A[i].first] < dp[j] + A[i].second) { dp[j + A[i].first] = dp[j] + A[i].second; } } } int ans = 0; rep(i, dp.size()) chmax(ans, dp[i]); cout << ans << endl; return 0; }
replace
102
103
102
103
0
p02863
C++
Runtime Error
#include <algorithm> #include <array> #include <stdio.h> #include <tuple> #define FOR(i, a, b) for (int(i) = (a); (i) < (b); ++(i)) #define REP(i, n) FOR(i, 0, n) using lli = long long int; constexpr int N = 3000; std::pair<int, int> A[N]; std::array<lli, N + 1> R; int main(void) { int n, t; scanf("%d%d", &n, &t); REP(i, n) scanf("%d%d", &A[i].first, &A[i].second); std::sort(A, A + n); REP(i, n) { int a = A[i].first, b = A[i].second; int j; for (j = t - 1; j + a >= t; --j) if (R[t] < R[j] + b) R[t] = R[j] + b; for (; j >= 0; --j) if (R[j + a] < R[j] + b) R[j + a] = R[j] + b; } lli res = 0; for (int i = 0; i <= t; ++i) if (res < R[i]) res = R[i]; printf("%lld\n", res); return 0; }
#include <algorithm> #include <array> #include <stdio.h> #include <tuple> #define FOR(i, a, b) for (int(i) = (a); (i) < (b); ++(i)) #define REP(i, n) FOR(i, 0, n) using lli = long long int; constexpr int N = 3000; std::pair<int, int> A[N]; std::array<lli, N + 1> R; int main(void) { int n, t; scanf("%d%d", &n, &t); REP(i, n) scanf("%d%d", &A[i].first, &A[i].second); std::sort(A, A + n); REP(i, n) { int a = A[i].first, b = A[i].second; int j, e = (t > a) ? t - a : 0; for (j = t - 1; j >= e; --j) if (R[t] < R[j] + b) R[t] = R[j] + b; for (; j >= 0; --j) if (R[j + a] < R[j] + b) R[j + a] = R[j] + b; } lli res = 0; for (int i = 0; i <= t; ++i) if (res < R[i]) res = R[i]; printf("%lld\n", res); return 0; }
replace
21
23
21
23
0
p02863
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <queue> #include <string.h> #include <vector> using namespace std; int i, j, k, T, A[3000], B[3000], N, memo1[3000][3001], memo2[3000][3001], ans; int dp1(int n, int m) { int s, t, u; if (memo1[n][m] != -1) { return memo1[n][m]; } if (n == 0) { if (A[n] <= m) { memo1[n][m] = B[n]; return B[n]; } else { memo1[n][m] = 0; return 0; } } t = dp1(n - 1, m); if (A[n] <= m) { t = max(t, dp1(n - 1, m - A[n]) + B[n]); } memo1[n][m] = t; return t; } int dp2(int n, int m) { int s, t, u; if (memo2[n][m] != -1) { return memo2[n][m]; } if (n == N - 1) { if (A[n] <= m) { memo1[n][m] = B[n]; return B[n]; } else { memo1[n][m] = 0; return 0; } } t = dp2(n + 1, m); if (A[n] <= m) { t = max(t, dp2(n + 1, m - A[n]) + B[n]); } memo2[n][m] = t; return t; } int main() { cin >> N >> T; for (i = 0; i < N; i++) { cin >> A[i] >> B[i]; } for (i = 0; i < N; i++) { for (j = 0; j < T + 1; j++) { memo1[i][j] = -1; memo2[i][j] = -1; } memo1[i][0] = 0; memo2[i][0] = 0; } ans = 0; for (i = 0; i < N; i++) { for (j = 0; j <= T - 1; j++) { ans = max(ans, dp1(i - 1, j) + dp2(i + 1, T - j - 1) + B[i]); } } cout << ans; return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <queue> #include <string.h> #include <vector> using namespace std; int i, j, k, T, A[3000], B[3000], N, memo1[3000][3001], memo2[3000][3001], ans; int dp1(int n, int m) { int s, t, u; if (memo1[n][m] != -1) { return memo1[n][m]; } if (n == 0) { if (A[n] <= m) { memo1[n][m] = B[n]; return B[n]; } else { memo1[n][m] = 0; return 0; } } t = dp1(n - 1, m); if (A[n] <= m) { t = max(t, dp1(n - 1, m - A[n]) + B[n]); } memo1[n][m] = t; return t; } int dp2(int n, int m) { int s, t, u; if (memo2[n][m] != -1) { return memo2[n][m]; } if (n == N - 1) { if (A[n] <= m) { memo1[n][m] = B[n]; return B[n]; } else { memo1[n][m] = 0; return 0; } } t = dp2(n + 1, m); if (A[n] <= m) { t = max(t, dp2(n + 1, m - A[n]) + B[n]); } memo2[n][m] = t; return t; } int main() { cin >> N >> T; for (i = 0; i < N; i++) { cin >> A[i] >> B[i]; } for (i = 0; i < N; i++) { for (j = 0; j < T + 1; j++) { memo1[i][j] = -1; memo2[i][j] = -1; } memo1[i][0] = 0; memo2[i][0] = 0; } ans = 0; ans = max(ans, dp2(1, T - 1) + B[0]); ans = max(ans, dp1(N - 2, T - 1) + B[N - 1]); for (i = 1; i < N - 1; i++) { for (j = 0; j <= T - 1; j++) { ans = max(ans, dp1(i - 1, j) + dp2(i + 1, T - j - 1) + B[i]); } } cout << ans; return 0; }
replace
81
82
81
85
0
p02863
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; ++i) #define ALL(c) (c).begin(), (c).end() #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()) using namespace std; using ll = long long; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline void dump(vector<T> v) { for (auto &x : v) cerr << x << " "; cerr << endl; } template <class T> inline void dump(vector<pair<T, T>> v) { for (auto &p : v) cerr << p.first << " " << p.second << endl; } template <class T> inline void dump(vector<vector<T>> vv) { for (auto &v : vv) { for (auto &x : v) cerr << x << " "; cerr << endl; } } constexpr int INF = 1e9 + 5; constexpr long long INFLL = 1LL << 62; constexpr double eps = (1e-9); int main() { cin.tie(0); ios::sync_with_stdio(false); int n, t; cin >> n >> t; vector<pair<ll, ll>> ab; rep(i, n) { int a, b; cin >> a >> b; ab.push_back({a, b}); } sort(ALL(ab)); vector<vector<ll>> dp(n, vector<ll>(t + 1, 0)); for (int j = ab[0].first; j < t; j++) { dp[0][j] = ab[0].second; } dp[0][ab[0].first] = ab[0].second; for (int i = 1; i < n; i++) { ll a = ab[i].first; ll b = ab[i].second; for (int j = 0; j < t; j++) dp[i][j] = dp[i - 1][j]; for (int j = a; j < t; j++) { chmax(dp[i][j], dp[i - 1][j - a] + b); } } ll ans = max(dp[n - 1][t - 1], ab[0].second); for (int i = 1; i < n; i++) { chmax(ans, dp[i - 1][t - 1] + ab[i].second); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; ++i) #define ALL(c) (c).begin(), (c).end() #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()) using namespace std; using ll = long long; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline void dump(vector<T> v) { for (auto &x : v) cerr << x << " "; cerr << endl; } template <class T> inline void dump(vector<pair<T, T>> v) { for (auto &p : v) cerr << p.first << " " << p.second << endl; } template <class T> inline void dump(vector<vector<T>> vv) { for (auto &v : vv) { for (auto &x : v) cerr << x << " "; cerr << endl; } } constexpr int INF = 1e9 + 5; constexpr long long INFLL = 1LL << 62; constexpr double eps = (1e-9); int main() { cin.tie(0); ios::sync_with_stdio(false); int n, t; cin >> n >> t; vector<pair<ll, ll>> ab; rep(i, n) { int a, b; cin >> a >> b; ab.push_back({a, b}); } sort(ALL(ab)); vector<vector<ll>> dp(n, vector<ll>(t + 1, 0)); for (int j = ab[0].first; j < t; j++) { dp[0][j] = ab[0].second; } for (int i = 1; i < n; i++) { ll a = ab[i].first; ll b = ab[i].second; for (int j = 0; j < t; j++) dp[i][j] = dp[i - 1][j]; for (int j = a; j < t; j++) { chmax(dp[i][j], dp[i - 1][j - a] + b); } } ll ans = max(dp[n - 1][t - 1], ab[0].second); for (int i = 1; i < n; i++) { chmax(ans, dp[i - 1][t - 1] + ab[i].second); } cout << ans << endl; return 0; }
replace
65
66
65
66
0
p02863
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, cc, n) for (int i = cc; i <= n; ++i) #define drep(i, cc, n) for (int i = cc; i >= n; --i) #define sz(s) (int)(s.size()) #define vecprint(v) \ rep(i, 0, v.size() - 1) cout << v[i] << " "; \ cout << endl; #define arrprint(v, n) \ rep(i, 0, n - 1) cout << v[i] << " "; \ cout << endl; #define matprint(v, n, m) \ rep(i, 0, n - 1) { \ rep(j, 0, m - 1) { cout << v[i][j] << " "; } \ cout << endl; \ } #define SORT(v) sort(v.begin(), v.end()) #define REV(v) reverse(v.begin(), v.end()) using namespace std; const int mod = 1000000007; const int INF = 1001001001; typedef long long ll; typedef pair<int, int> P; template <typename T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>; // 小さいものから取り出す int main() { int n, t; cin >> n >> t; vector<P> ab(n); rep(i, 0, n - 1) cin >> ab[i].first >> ab[i].second; SORT(ab); // REV(ab); int dp[n - 1][t]; int ans = 0; rep(i, 0, t - 1) { if (i - ab[0].first >= 0) dp[0][i] = ab[0].second; else dp[0][i] = 0; } ans = ab[0].second; rep(i, 1, n - 1) { rep(j, 0, t - 1) { if (j - ab[i].first >= 0) dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - ab[i].first] + ab[i].second); else dp[i][j] = dp[i - 1][j]; } ans = max(ans, dp[i - 1][t - 1] + ab[i].second); } // matprint(dp,n-1,t); // int ans = dp[n-2][t-1] + ab[n-1].first; cout << ans << endl; // printf("%.10f\n",ans); return 0; }
#include <bits/stdc++.h> #define rep(i, cc, n) for (int i = cc; i <= n; ++i) #define drep(i, cc, n) for (int i = cc; i >= n; --i) #define sz(s) (int)(s.size()) #define vecprint(v) \ rep(i, 0, v.size() - 1) cout << v[i] << " "; \ cout << endl; #define arrprint(v, n) \ rep(i, 0, n - 1) cout << v[i] << " "; \ cout << endl; #define matprint(v, n, m) \ rep(i, 0, n - 1) { \ rep(j, 0, m - 1) { cout << v[i][j] << " "; } \ cout << endl; \ } #define SORT(v) sort(v.begin(), v.end()) #define REV(v) reverse(v.begin(), v.end()) using namespace std; const int mod = 1000000007; const int INF = 1001001001; typedef long long ll; typedef pair<int, int> P; template <typename T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>; // 小さいものから取り出す int main() { int n, t; cin >> n >> t; vector<P> ab(n); rep(i, 0, n - 1) cin >> ab[i].first >> ab[i].second; SORT(ab); // REV(ab); int dp[n][t]; int ans = 0; rep(i, 0, t - 1) { if (i - ab[0].first >= 0) dp[0][i] = ab[0].second; else dp[0][i] = 0; } ans = ab[0].second; rep(i, 1, n - 1) { rep(j, 0, t - 1) { if (j - ab[i].first >= 0) dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - ab[i].first] + ab[i].second); else dp[i][j] = dp[i - 1][j]; } ans = max(ans, dp[i - 1][t - 1] + ab[i].second); } // matprint(dp,n-1,t); // int ans = dp[n-2][t-1] + ab[n-1].first; cout << ans << endl; // printf("%.10f\n",ans); return 0; }
replace
33
34
33
34
0
p02863
C++
Runtime Error
#pragma GCC optimize(2) #include <bits/stdc++.h> using namespace std; const int man = 3e3 + 10; #define IOS ios::sync_with_stdio(0) template <typename T> T gcd(T a, T b) { return b == 0 ? a : gcd(b, a % b); } template <typename T> T exgcd(T a, T b, T &g, T &x, T &y) { if (!b) { g = a, x = 1, y = 0; } else { exgcd(b, a % b, g, y, x); y -= x * (a / b); } } #ifndef ONLINE_JUDGE #define debug(fmt, ...) \ { \ printf("debug "); \ printf(fmt, ##__VA_ARGS__); \ puts(""); \ } #else #define debug(fmt, ...) #endif typedef long long ll; const ll mod = 1e9 + 7; struct node { int w, v; friend bool operator<(node a, node b) { return a.w < b.w; } } a[man]; int dp[man]; int main() { #ifndef ONLINE_JUDGE // freopen("in.txt", "r", stdin); // freopen("out.txt","w",stdout); #endif int n, t; cin >> n >> t; for (int i = 1; i <= n; i++) cin >> a[i].w >> a[i].v; sort(a + 1, a + 1 + n); for (int i = 1; i <= n; i++) { for (int j = t + a[i].w - 1; j >= a[i].w; j--) { dp[j] = max(dp[j], dp[j - a[i].w] + a[i].v); } } int ans = 0; for (int i = 1; i <= n; i++) ans = max(ans, dp[t + a[i].w - 1]); cout << ans << endl; return 0; }
#pragma GCC optimize(2) #include <bits/stdc++.h> using namespace std; const int man = 1e4 + 10; #define IOS ios::sync_with_stdio(0) template <typename T> T gcd(T a, T b) { return b == 0 ? a : gcd(b, a % b); } template <typename T> T exgcd(T a, T b, T &g, T &x, T &y) { if (!b) { g = a, x = 1, y = 0; } else { exgcd(b, a % b, g, y, x); y -= x * (a / b); } } #ifndef ONLINE_JUDGE #define debug(fmt, ...) \ { \ printf("debug "); \ printf(fmt, ##__VA_ARGS__); \ puts(""); \ } #else #define debug(fmt, ...) #endif typedef long long ll; const ll mod = 1e9 + 7; struct node { int w, v; friend bool operator<(node a, node b) { return a.w < b.w; } } a[man]; int dp[man]; int main() { #ifndef ONLINE_JUDGE // freopen("in.txt", "r", stdin); // freopen("out.txt","w",stdout); #endif int n, t; cin >> n >> t; for (int i = 1; i <= n; i++) cin >> a[i].w >> a[i].v; sort(a + 1, a + 1 + n); for (int i = 1; i <= n; i++) { for (int j = t + a[i].w - 1; j >= a[i].w; j--) { dp[j] = max(dp[j], dp[j - a[i].w] + a[i].v); } } int ans = 0; for (int i = 1; i <= n; i++) ans = max(ans, dp[t + a[i].w - 1]); cout << ans << endl; return 0; }
replace
3
4
3
4
0
p02863
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <complex> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <vector> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--) #define repi(i, a, b) for (int i = int(a); i < int(b); i++) #define all(x) (x).begin(), (x).end() template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } typedef long long ll; using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); ll n, t; cin >> n >> t; struct di { ll a, b; }; vector<di> ds(n); rep(i, n) { cin >> ds[i].a >> ds[i].b; } sort(all(ds), [&](di a, di b) { return a.a < b.b; }); vector<ll> dp(t); ll res = 0; rep(i, n) { auto tdp = dp; rep(j, t) { if (j + ds[i].a < t) chmax(tdp[j + ds[i].a], dp[j] + ds[i].b); else chmax(res, dp[j] + ds[i].b); } dp = tdp; } rep(i, t) { chmax(res, dp[i]); } cout << res << endl; return 0; }
#include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <complex> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <vector> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--) #define repi(i, a, b) for (int i = int(a); i < int(b); i++) #define all(x) (x).begin(), (x).end() template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } typedef long long ll; using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); ll n, t; cin >> n >> t; struct di { ll a, b; }; vector<di> ds(n); rep(i, n) { cin >> ds[i].a >> ds[i].b; } sort(all(ds), [&](di a, di b) { return a.a < b.a; }); vector<ll> dp(t); ll res = 0; rep(i, n) { auto tdp = dp; rep(j, t) { if (j + ds[i].a < t) chmax(tdp[j + ds[i].a], dp[j] + ds[i].b); else chmax(res, dp[j] + ds[i].b); } dp = tdp; } rep(i, t) { chmax(res, dp[i]); } cout << res << endl; return 0; }
replace
51
52
51
52
0
p02863
C++
Time Limit Exceeded
// include //------------------------------------------ #include <algorithm> #include <bitset> #include <deque> #include <functional> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #include <complex> #include <numeric> #include <utility> #include <iomanip> #include <iostream> #include <sstream> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> // namespace using namespace std; // type alias using ll = long long; using ull = unsigned long long; using comp = complex<double>; // constant static const ll MOD = 1000000007LL; static const double PI = 3.14159265358979323846; // conversion inline ll toint(string s) { ll v; istringstream sin(s); sin >> v; return v; } template <class t> inline string tostring(t x) { ostringstream sout; sout << x; return sout.str(); } // print #define RET(x) return cout << x << endl, 0; // for loop #define REP(i, a, b) for ((i) = (ll)(a); (i) < (ll)(b); (i)++) #define REPD(i, a, b) for (ll i = (ll)(a); (i) < (ll)(b); (i)++) #define REPI(v, vs) for (auto &v : vs) // debug #define DUMP(x) cerr << #x << " = " << (x) << endl #define DEBUG(x) \ cerr << #x << " = " << (x) << " (l" << __LINE__ << ")" \ << " " << __FILE__ << endl #define MAX_VALUE 9223372036854775807 vector<vector<ll>> dps; ll calc_dps(ll idx, ll t, const vector<pair<ll, ll>> &ABs) { if (idx < 0) return 0; if (t < 0) return 0; if (dps[idx][t] != -1) return dps[idx][t]; ll res = 0; REPD(i, 0, idx + 1) { if (t < ABs[i].first) continue; res = max(res, calc_dps(i - 1, t - ABs[i].first, ABs) + ABs[i].second); } dps[idx][t] = res; return dps[idx][t]; } int solve() { ll N, T; cin >> N >> T; vector<pair<ll, ll>> ABs(N); ll A, B; REPD(i, 0, N) { cin >> A >> B; ABs[i] = {A, B}; } sort(ABs.begin(), ABs.end()); dps = vector<vector<ll>>(N, vector<ll>(T, -1)); ll res = ABs[0].second; REPD(i, 0, N) res = max(res, ABs[i].second + calc_dps(i - 1, T - 1, ABs)); RET(res); } // main function int main() { cin.tie(0); ios::sync_with_stdio(false); solve(); // ll t; // cin >> t; // REPD(i, 0, t) solve(); return 0; }
// include //------------------------------------------ #include <algorithm> #include <bitset> #include <deque> #include <functional> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #include <complex> #include <numeric> #include <utility> #include <iomanip> #include <iostream> #include <sstream> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> // namespace using namespace std; // type alias using ll = long long; using ull = unsigned long long; using comp = complex<double>; // constant static const ll MOD = 1000000007LL; static const double PI = 3.14159265358979323846; // conversion inline ll toint(string s) { ll v; istringstream sin(s); sin >> v; return v; } template <class t> inline string tostring(t x) { ostringstream sout; sout << x; return sout.str(); } // print #define RET(x) return cout << x << endl, 0; // for loop #define REP(i, a, b) for ((i) = (ll)(a); (i) < (ll)(b); (i)++) #define REPD(i, a, b) for (ll i = (ll)(a); (i) < (ll)(b); (i)++) #define REPI(v, vs) for (auto &v : vs) // debug #define DUMP(x) cerr << #x << " = " << (x) << endl #define DEBUG(x) \ cerr << #x << " = " << (x) << " (l" << __LINE__ << ")" \ << " " << __FILE__ << endl #define MAX_VALUE 9223372036854775807 vector<vector<ll>> dps; ll calc_dps(ll idx, ll t, const vector<pair<ll, ll>> &ABs) { if (idx < 0) return 0; if (t < 0) return 0; if (dps[idx][t] != -1) return dps[idx][t]; ll res = calc_dps(idx - 1, t, ABs); if (t >= ABs[idx].first) { res = max(res, calc_dps(idx - 1, t - ABs[idx].first, ABs) + ABs[idx].second); } dps[idx][t] = res; return dps[idx][t]; } int solve() { ll N, T; cin >> N >> T; vector<pair<ll, ll>> ABs(N); ll A, B; REPD(i, 0, N) { cin >> A >> B; ABs[i] = {A, B}; } sort(ABs.begin(), ABs.end()); dps = vector<vector<ll>>(N, vector<ll>(T, -1)); ll res = ABs[0].second; REPD(i, 0, N) res = max(res, ABs[i].second + calc_dps(i - 1, T - 1, ABs)); RET(res); } // main function int main() { cin.tie(0); ios::sync_with_stdio(false); solve(); // ll t; // cin >> t; // REPD(i, 0, t) solve(); return 0; }
replace
80
85
80
84
TLE
p02863
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using Int = int_fast64_t; int main() { cin.tie(0); ios::sync_with_stdio(false); int n, t; cin >> n >> t; using P = pair<int, int>; vector<P> v(n); for (int i = 0; i < n; ++i) { int a, b; cin >> a >> b; v[i] = P(a, b); } sort(v.begin(), v.end()); vector<vector<int>> dp(n, vector<int>(t)); dp[0][v[0].first] = v[0].second; for (int i = 1; i < t; ++i) dp[0][i] = max(dp[0][i], dp[0][i - 1]); for (int i = 1; i < n - 1; ++i) for (int j = 1; j < t; ++j) { dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]); if (j - v[i].first >= 0) dp[i][j] = max(dp[i][j], dp[i - 1][j - v[i].first] + v[i].second); } // for(int i=0; i<n; ++i) // cout << i << " " << v[i].first << " " << v[i].second << "\n"; for (int i = n - 2; i >= 0; --i) v[i].second = max(v[i].second, v[i + 1].second); int ans = 0; for (int i = 0; i < n - 1; ++i) { ans = max(ans, dp[i][t - 1] + v[i + 1].second); // cout << ans << " " << i << " " << dp[i][t-1] << "\n"; } cout << ans << "\n"; // for(int i=2; i<4; ++i) // for(int j=0; j<t; ++j) // cout << i << " " << j << " " << dp[i][j] << "\n"; }
#include <bits/stdc++.h> using namespace std; using Int = int_fast64_t; int main() { cin.tie(0); ios::sync_with_stdio(false); int n, t; cin >> n >> t; using P = pair<int, int>; vector<P> v(n); for (int i = 0; i < n; ++i) { int a, b; cin >> a >> b; v[i] = P(a, b); } sort(v.begin(), v.end()); vector<vector<int>> dp(n, vector<int>(t)); if (v[0].first < t) dp[0][v[0].first] = v[0].second; for (int i = 1; i < t; ++i) dp[0][i] = max(dp[0][i], dp[0][i - 1]); for (int i = 1; i < n - 1; ++i) for (int j = 1; j < t; ++j) { dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]); if (j - v[i].first >= 0) dp[i][j] = max(dp[i][j], dp[i - 1][j - v[i].first] + v[i].second); } // for(int i=0; i<n; ++i) // cout << i << " " << v[i].first << " " << v[i].second << "\n"; for (int i = n - 2; i >= 0; --i) v[i].second = max(v[i].second, v[i + 1].second); int ans = 0; for (int i = 0; i < n - 1; ++i) { ans = max(ans, dp[i][t - 1] + v[i + 1].second); // cout << ans << " " << i << " " << dp[i][t-1] << "\n"; } cout << ans << "\n"; // for(int i=2; i<4; ++i) // for(int j=0; j<t; ++j) // cout << i << " " << j << " " << dp[i][j] << "\n"; }
replace
18
19
18
20
0
p02863
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define _overload3(_1, _2, _3, name, ...) name #define _rep(i, n) repi(i, 0, n) #define repi(i, a, b) for (ll i = (ll)(a); i < (ll)(b); ++i) #define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__) #define ll long long #define lld long double #define ALL(x) x.begin(), x.end() #ifdef DEBUG #define line() cerr << "[" << __LINE__ << "] "; #define dump(i) cerr << #i ": " << i << " "; #define dumpl(i) cerr << #i ": " << i << endl; #else #define line(i) #define dump(i) #define dumpl(i) #endif using namespace std; int main(int argc, char const *argv[]) { int n, t; cin >> n >> t; vector<int> a(n), b(n); map<pair<int, int>, int> mp; rep(i, n) { cin >> a[i] >> b[i]; mp[{a[i], b[i]}]++; } vector<vector<int>> dp(n + 1, vector<int>(t, 0)); rep(i, n) { rep(j, t) { // cerr << "change " << dp[i + 1][j] << "->" << dp[i][j] << endl; dp[i + 1][j] = dp[i][j]; } rep(j, t) { if (a[i] + j < t) { if (dp[i + 1][j + a[i]] < dp[i][j] + b[i]) { dp[i + 1][j + a[i]] = dp[i][j] + b[i]; } } } } int ans = 0; rep(j, t) { // cerr << dp[n][j] << endl; rep(i, n) { // cerr << mp[{n, j}][i] << " "; int tt = 0; bool ff = (j - a[i] < 0); int nn = mp[{a[i], b[i]}]; rep(k, nn) { if (j - (k + 1) * a[i] < 0) { ff = true; break; } ff |= (dp[n][j] > dp[n][j - (k + 1) * a[i]] + (k + 1) * b[i]); } if (ff) { // cerr << j << " " << i << " " << a[i] << " " << b[i] << endl; // cerr << dp[n][j] << " " << dp[n][j - a[i]] + b[i] << endl; tt = b[i]; } ans = max(ans, dp[n][j] + tt); } // cerr << endl; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define _overload3(_1, _2, _3, name, ...) name #define _rep(i, n) repi(i, 0, n) #define repi(i, a, b) for (ll i = (ll)(a); i < (ll)(b); ++i) #define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__) #define ll long long #define lld long double #define ALL(x) x.begin(), x.end() #ifdef DEBUG #define line() cerr << "[" << __LINE__ << "] "; #define dump(i) cerr << #i ": " << i << " "; #define dumpl(i) cerr << #i ": " << i << endl; #else #define line(i) #define dump(i) #define dumpl(i) #endif using namespace std; int main(int argc, char const *argv[]) { int n, t; cin >> n >> t; vector<int> a(n), b(n); map<pair<int, int>, int> mp; rep(i, n) { cin >> a[i] >> b[i]; mp[{a[i], b[i]}]++; } vector<vector<int>> dp(n + 1, vector<int>(t, 0)); rep(i, n) { rep(j, t) { // cerr << "change " << dp[i + 1][j] << "->" << dp[i][j] << endl; dp[i + 1][j] = dp[i][j]; } rep(j, t) { if (a[i] + j < t) { if (dp[i + 1][j + a[i]] < dp[i][j] + b[i]) { dp[i + 1][j + a[i]] = dp[i][j] + b[i]; } } } } int ans = 0; rep(j, t) { // cerr << dp[n][j] << endl; rep(i, n) { // cerr << mp[{n, j}][i] << " "; int tt = 0; bool ff = (j - a[i] < 0); int nn = mp[{a[i], b[i]}]; rep(k, nn) { if (j - (k + 1) * a[i] < 0) { ff = true; break; } ff |= (dp[n][j] > dp[n][j - (k + 1) * a[i]] + (k + 1) * b[i]); if (k == 100) break; } if (ff) { // cerr << j << " " << i << " " << a[i] << " " << b[i] << endl; // cerr << dp[n][j] << " " << dp[n][j - a[i]] + b[i] << endl; tt = b[i]; } ans = max(ans, dp[n][j] + tt); } // cerr << endl; } cout << ans << endl; return 0; }
insert
56
56
56
58
TLE
p02863
C++
Runtime Error
#define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <functional> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <vector> #define _USE_MATH_DEFINES #include <bitset> #include <deque> #include <iostream> #include <list> #include <map> #include <math.h> #include <queue> #include <set> using namespace std; typedef long long ll; #define rep(i, a, b) for (auto i = a; i < b; i++) #define all(_x) _x.begin(), _x.end() #define r_sort(_x) sort(_x.begin(), _x.end(), std::greater<int>()) #define vec_cnt(_a, _n) (upper_bound(all(_a), _n) - lower_bound(all(_a), _n)) #define vec_unique(_a) _a.erase(std::unique(all(_a)), _a.end()); #define vvec vector<vector<ll>> ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); } ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; } #define INF 1 << 30 const int mod = 1000000007; ll power(ll x, ll p) { ll a = 1; while (p > 0) { if (p % 2 == 0) { x *= x; p /= 2; } else { a *= x; p--; } } return a; } ll mpower(ll x, ll p) { ll a = 1; while (p > 0) { if (p % 2 == 0) { x = x * x % mod; p /= 2; } else { a = a * x % mod; p--; } } return a; } ll ac(ll n, ll k) { ll a = 1; rep(i, 1, k) { a *= n - i + 1; a /= i; } return a; } ll mc(ll n, ll m) { ll k = 1, l = 1; rep(i, n - m + 1, n + 1) k = k * i % mod; rep(i, 1, m + 1) l = l * i % mod; l = mpower(l, mod - 2); return k * l % mod; } int dp[3001][3001]; int main() { int n, t, A, B; cin >> n >> t; vector<int> a(n), b(n); vector<pair<int, int>> data(n); rep(i, 0, n) cin >> A >> B, data[i] = {A, B}; sort(all(data)); rep(i, 0, n) a[i] = data[i].first, b[i] = data[i].second; vector<int> kouho; int M = 0; rep(i, 0, t) { rep(i2, 0, n) { if (i + a[i2] < t) dp[i2 + 1][i + a[i2]] = max(dp[i2][i] + b[i2], dp[i2 + 1][i + a[i2]]); else { M = max(max(dp[i2][i] + b[i2], dp[i2 + 1][i + a[i2]]), M); } dp[i2 + 1][i] = max(dp[i2][i], dp[i2 + 1][i]); } } printf("%d\n", M); return 0; }
#define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <functional> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <vector> #define _USE_MATH_DEFINES #include <bitset> #include <deque> #include <iostream> #include <list> #include <map> #include <math.h> #include <queue> #include <set> using namespace std; typedef long long ll; #define rep(i, a, b) for (auto i = a; i < b; i++) #define all(_x) _x.begin(), _x.end() #define r_sort(_x) sort(_x.begin(), _x.end(), std::greater<int>()) #define vec_cnt(_a, _n) (upper_bound(all(_a), _n) - lower_bound(all(_a), _n)) #define vec_unique(_a) _a.erase(std::unique(all(_a)), _a.end()); #define vvec vector<vector<ll>> ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); } ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; } #define INF 1 << 30 const int mod = 1000000007; ll power(ll x, ll p) { ll a = 1; while (p > 0) { if (p % 2 == 0) { x *= x; p /= 2; } else { a *= x; p--; } } return a; } ll mpower(ll x, ll p) { ll a = 1; while (p > 0) { if (p % 2 == 0) { x = x * x % mod; p /= 2; } else { a = a * x % mod; p--; } } return a; } ll ac(ll n, ll k) { ll a = 1; rep(i, 1, k) { a *= n - i + 1; a /= i; } return a; } ll mc(ll n, ll m) { ll k = 1, l = 1; rep(i, n - m + 1, n + 1) k = k * i % mod; rep(i, 1, m + 1) l = l * i % mod; l = mpower(l, mod - 2); return k * l % mod; } int dp[6001][6001]; int main() { int n, t, A, B; cin >> n >> t; vector<int> a(n), b(n); vector<pair<int, int>> data(n); rep(i, 0, n) cin >> A >> B, data[i] = {A, B}; sort(all(data)); rep(i, 0, n) a[i] = data[i].first, b[i] = data[i].second; vector<int> kouho; int M = 0; rep(i, 0, t) { rep(i2, 0, n) { if (i + a[i2] < t) dp[i2 + 1][i + a[i2]] = max(dp[i2][i] + b[i2], dp[i2 + 1][i + a[i2]]); else { M = max(max(dp[i2][i] + b[i2], dp[i2 + 1][i + a[i2]]), M); } dp[i2 + 1][i] = max(dp[i2][i], dp[i2 + 1][i]); } } printf("%d\n", M); return 0; }
replace
70
71
70
71
0
p02863
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, N) for (int i = 0; i < int(N); ++i) #define rep1(i, N) for (int i = 1; i < int(N); ++i) #define all(a) (a).begin(), (a).end() #define bit(k) (1LL << (k)) #define SUM(v) accumulate(all(v), 0LL) typedef pair<int, int> i_i; typedef pair<ll, ll> l_l; template <class T> using vec = vector<T>; template <class T> using vvec = vector<vec<T>>; struct fast_ios { fast_ios() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } #define TOSTRING(x) string(#x) template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "["; for (auto _ : v) os << _ << ", "; os << "]"; return os; }; template <typename T> ostream &operator<<(ostream &os, set<T> &st) { os << "("; for (auto _ : st) { os << _ << ", "; } os << ")"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << "{" << p.first << ", " << p.second << "}"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, const map<T, U> &mp) { os << "["; for (auto _ : mp) { os << _ << ", "; } os << "]" << endl; return os; } #define DUMPOUT cerr void dump_func() { DUMPOUT << endl; } template <class Head, class... Tail> void dump_func(Head &&head, Tail &&...tail) { DUMPOUT << head; if (sizeof...(Tail) > 0) { DUMPOUT << ", "; } dump_func(std::move(tail)...); } #ifdef DEBUG #define dbg(...) dump_func(__VA_ARGS__) #define dump(...) \ DUMPOUT << string(#__VA_ARGS__) << ": "; \ dump_func(__VA_ARGS__) #else #define dbg(...) #define dump(...) #endif const int INF = (ll)1e9; const ll INFLL = (ll)1e18 + 1; const ll MOD = 1000000007; // const ll MOD = 998244353; const long double PI = acos(-1.0); /* const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; const string dir = "DRUL"; */ const int MAXT = 3002; int main() { ll N, T; cin >> N >> T; vector<l_l> AB; rep(i, N) { ll a, b; cin >> a >> b; AB.emplace_back(a, b); } sort(all(AB)); vector<ll> dp(T + 1, -1); dp[0] = 0; rep(i, N) { auto [a, b] = AB[i]; vector<ll> dp2(T + 1, -1); rep(j, MAXT) chmax(dp2[j], dp[j]); rep(j, T) { if (dp[j] >= 0) chmax(dp2[min(T, j + a)], dp[j] + b); } swap(dp, dp2); } cout << *max_element(all(dp)) << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, N) for (int i = 0; i < int(N); ++i) #define rep1(i, N) for (int i = 1; i < int(N); ++i) #define all(a) (a).begin(), (a).end() #define bit(k) (1LL << (k)) #define SUM(v) accumulate(all(v), 0LL) typedef pair<int, int> i_i; typedef pair<ll, ll> l_l; template <class T> using vec = vector<T>; template <class T> using vvec = vector<vec<T>>; struct fast_ios { fast_ios() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } #define TOSTRING(x) string(#x) template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "["; for (auto _ : v) os << _ << ", "; os << "]"; return os; }; template <typename T> ostream &operator<<(ostream &os, set<T> &st) { os << "("; for (auto _ : st) { os << _ << ", "; } os << ")"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << "{" << p.first << ", " << p.second << "}"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, const map<T, U> &mp) { os << "["; for (auto _ : mp) { os << _ << ", "; } os << "]" << endl; return os; } #define DUMPOUT cerr void dump_func() { DUMPOUT << endl; } template <class Head, class... Tail> void dump_func(Head &&head, Tail &&...tail) { DUMPOUT << head; if (sizeof...(Tail) > 0) { DUMPOUT << ", "; } dump_func(std::move(tail)...); } #ifdef DEBUG #define dbg(...) dump_func(__VA_ARGS__) #define dump(...) \ DUMPOUT << string(#__VA_ARGS__) << ": "; \ dump_func(__VA_ARGS__) #else #define dbg(...) #define dump(...) #endif const int INF = (ll)1e9; const ll INFLL = (ll)1e18 + 1; const ll MOD = 1000000007; // const ll MOD = 998244353; const long double PI = acos(-1.0); /* const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; const string dir = "DRUL"; */ const int MAXT = 3002; int main() { ll N, T; cin >> N >> T; vector<l_l> AB; rep(i, N) { ll a, b; cin >> a >> b; AB.emplace_back(a, b); } sort(all(AB)); vector<ll> dp(T + 1, -1); dp[0] = 0; rep(i, N) { auto [a, b] = AB[i]; vector<ll> dp2(T + 1, -1); rep(j, T + 1) chmax(dp2[j], dp[j]); rep(j, T) { if (dp[j] >= 0) chmax(dp2[min(T, j + a)], dp[j] + b); } swap(dp, dp2); } cout << *max_element(all(dp)) << endl; }
replace
121
122
121
122
-6
double free or corruption (!prev)
p02863
C++
Runtime Error
#include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <iostream> #include <string> #include <unordered_map> #include <unordered_set> using namespace std; #define ll long long #define rep(i, n) for (ll i = 0; i < (n); i++) #define FOR(i, a, b) for (ll i = (a); i < (b); i++) #define FORR(i, a, b) for (ll i = (a); i <= (b); i++) #define repR(i, n) for (ll i = n; i >= 0; i--) #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define F first #define S second #define pb push_back #define pu push #define COUT(x) cout << (x) << endl #define PQ priority_queue<ll> #define PQR priority_queue<ll, vector<ll>, greater<ll>> #define YES(n) cout << ((n) ? "YES" : "NO") << endl #define Yes(n) cout << ((n) ? "Yes" : "No") << endl #define mp make_pair #define maxs(x, y) (x = max(x, y)) #define mins(x, y) (x = min(x, y)) #define sz(x) (ll)(x).size() typedef pair<int, int> pii; typedef pair<ll, ll> pll; const ll MOD = 1000000007LL; const ll INF = 1LL << 60; using vll = vector<ll>; using vb = vector<bool>; using vvb = vector<vb>; using vvll = vector<vll>; using vstr = vector<string>; using pll = pair<ll, ll>; using vc = vector<char>; using vvc = vector<vc>; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } ll dx[4] = {0, 1, 0, -1}; ll dy[4] = {1, 0, -1, 0}; int main() { ll n, t; cin >> n >> t; vector<pll> p(n); rep(i, n) { cin >> p[i].F >> p[i].S; } sort(all(p)); vvll dp(n + 1, vll(t + 10)); rep(i, n) { rep(j, t) { dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]); if (p[i].F + j < t) { dp[i + 1][j + p[i].F] = max(dp[i + 1][j + p[i].F], dp[i][j] + p[i].S); } } } ll ans = 0; rep(i, n) { ans = max(ans, dp[i - 1][t - 1] + p[i].S); } COUT(ans); }
#include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <iostream> #include <string> #include <unordered_map> #include <unordered_set> using namespace std; #define ll long long #define rep(i, n) for (ll i = 0; i < (n); i++) #define FOR(i, a, b) for (ll i = (a); i < (b); i++) #define FORR(i, a, b) for (ll i = (a); i <= (b); i++) #define repR(i, n) for (ll i = n; i >= 0; i--) #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define F first #define S second #define pb push_back #define pu push #define COUT(x) cout << (x) << endl #define PQ priority_queue<ll> #define PQR priority_queue<ll, vector<ll>, greater<ll>> #define YES(n) cout << ((n) ? "YES" : "NO") << endl #define Yes(n) cout << ((n) ? "Yes" : "No") << endl #define mp make_pair #define maxs(x, y) (x = max(x, y)) #define mins(x, y) (x = min(x, y)) #define sz(x) (ll)(x).size() typedef pair<int, int> pii; typedef pair<ll, ll> pll; const ll MOD = 1000000007LL; const ll INF = 1LL << 60; using vll = vector<ll>; using vb = vector<bool>; using vvb = vector<vb>; using vvll = vector<vll>; using vstr = vector<string>; using pll = pair<ll, ll>; using vc = vector<char>; using vvc = vector<vc>; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } ll dx[4] = {0, 1, 0, -1}; ll dy[4] = {1, 0, -1, 0}; int main() { ll n, t; cin >> n >> t; vector<pll> p(n); rep(i, n) { cin >> p[i].F >> p[i].S; } sort(all(p)); vvll dp(n + 1, vll(t + 10)); rep(i, n) { rep(j, t) { dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]); if (p[i].F + j < t) { dp[i + 1][j + p[i].F] = max(dp[i + 1][j + p[i].F], dp[i][j] + p[i].S); } } } ll ans = 0; rep(i, n) { ans = max(ans, dp[i][t - 1] + p[i].S); } COUT(ans); }
replace
72
73
72
73
-11
p02863
C++
Runtime Error
#pragma GCC optimize("O3") #include "bits/stdc++.h" using namespace std; using ll = long long int; #define debugos cout #define debug(v) \ { \ printf("L%d %s > ", __LINE__, #v); \ debugos << (v) << endl; \ } #define debugv(v) \ { \ printf("L%d %s > ", __LINE__, #v); \ for (auto e : (v)) { \ debugos << e << " "; \ } \ debugos << endl; \ } #define debuga(m, w) \ { \ printf("L%d %s > ", __LINE__, #m); \ for (int x = 0; x < (w); x++) { \ debugos << (m)[x] << " "; \ } \ debugos << endl; \ } #define debugaa(m, h, w) \ { \ printf("L%d %s >\n", __LINE__, #m); \ for (int y = 0; y < (h); y++) { \ for (int x = 0; x < (w); x++) { \ debugos << (m)[y][x] << " "; \ } \ debugos << endl; \ } \ } #define all(v) (v).begin(), (v).end() #define repeat(cnt, l) \ for (auto cnt = remove_reference<decltype(l)>::type(); (cnt) < (l); ++(cnt)) #define rrepeat(cnt, l) for (auto cnt = (l)-1; 0 <= (cnt); --(cnt)) #define iterate(cnt, b, e) for (auto cnt = (b); (cnt) != (e); ++(cnt)) #define diterate(cnt, b, e) for (auto cnt = (b); (cnt) != (e); --(cnt)) const ll MD = 1000000007ll; const long double PI = 3.1415926535897932384626433832795L; inline void assert_call(bool assertion, function<void()> f) { if (!assertion) { cerr << "assertion fault:" << endl; f(); abort(); } } template <typename T1, typename T2> inline ostream &operator<<(ostream &o, const pair<T1, T2> p) { o << '(' << p.first << ':' << p.second << ')'; return o; } template <typename Vec> inline ostream &_ostream_vecprint(ostream &os, const Vec &a) { os << '['; for (const auto &e : a) os << ' ' << e << ' '; os << ']'; return os; } template <typename T> inline ostream &operator<<(ostream &o, const vector<T> &v) { return _ostream_vecprint(o, v); } template <typename T, size_t S> inline ostream &operator<<(ostream &o, const array<T, S> &v) { return _ostream_vecprint(o, v); } template <typename T> inline T &chmax(T &to, const T &val) { return to = max(to, val); } template <typename T> inline T &chmin(T &to, const T &val) { return to = min(to, val); } void bye(string s, int code = 0) { cout << s << endl; exit(code); } mt19937_64 randdev(8901016); template <typename T, typename Random = decltype(randdev), typename enable_if<is_integral<T>::value>::type * = nullptr> inline T rand(T l, T h, Random &rand = randdev) { return uniform_int_distribution<T>(l, h)(rand); } template <typename T, typename Random = decltype(randdev), typename enable_if<is_floating_point<T>::value>::type * = nullptr> inline T rand(T l, T h, Random &rand = randdev) { return uniform_real_distribution<T>(l, h)(rand); } #if defined(_WIN32) || defined(_WIN64) #define getc_x _getc_nolock #define putc_x _putc_nolock #elif defined(__GNUC__) #define getc_x getc_unlocked #define putc_x putc_unlocked #else #define getc_x getc #define putc_x putc #endif #define isvisiblechar(c) (0x21 <= (c) && (c) <= 0x7E) class MaiScanner { FILE *fp_; public: inline MaiScanner(FILE *fp) : fp_(fp) {} template <typename T> void input_integer(T &var) noexcept { var = 0; T sign = 1; int cc = getc_x(fp_); for (; cc < '0' || '9' < cc; cc = getc_x(fp_)) if (cc == '-') sign = -1; for (; '0' <= cc && cc <= '9'; cc = getc_x(fp_)) var = (var << 3) + (var << 1) + cc - '0'; var = var * sign; } inline int c() noexcept { return getc_x(fp_); } inline MaiScanner &operator>>(int &var) noexcept { input_integer<int>(var); return *this; } inline MaiScanner &operator>>(long long &var) noexcept { input_integer<long long>(var); return *this; } inline MaiScanner &operator>>(string &var) { int cc = getc_x(fp_); for (; !isvisiblechar(cc); cc = getc_x(fp_)) ; for (; isvisiblechar(cc); cc = getc_x(fp_)) var.push_back(cc); return *this; } template <typename IT> inline void in(IT begin, IT end) { for (auto it = begin; it != end; ++it) *this >> *it; } }; class MaiPrinter { FILE *fp_; public: inline MaiPrinter(FILE *fp) : fp_(fp) {} template <typename T> void output_integer(T var) noexcept { if (var == 0) { putc_x('0', fp_); return; } if (var < 0) putc_x('-', fp_), var = -var; char stack[32]; int stack_p = 0; while (var) stack[stack_p++] = '0' + (var % 10), var /= 10; while (stack_p) putc_x(stack[--stack_p], fp_); } inline MaiPrinter &operator<<(char c) noexcept { putc_x(c, fp_); return *this; } inline MaiPrinter &operator<<(int var) noexcept { output_integer<int>(var); return *this; } inline MaiPrinter &operator<<(long long var) noexcept { output_integer<long long>(var); return *this; } inline MaiPrinter &operator<<(char *str_p) noexcept { while (*str_p) putc_x(*(str_p++), fp_); return *this; } inline MaiPrinter &operator<<(const string &str) { const char *p = str.c_str(); const char *l = p + str.size(); while (p < l) putc_x(*p++, fp_); return *this; } template <typename IT> void join(IT begin, IT end, char sep = ' ') { for (bool b = 0; begin != end; ++begin, b = 1) b ? *this << sep << *begin : *this << *begin; } }; MaiScanner scanner(stdin); MaiPrinter printer(stdout); // int N; int T; array<int, 2> AB[3030]; // int main() { scanner >> N >> T; repeat(i, N) { int a, b; scanner >> a >> b; AB[i] = {a, b}; } sort(AB, AB + N); int best = 0; vector<int> dp1; dp1.resize(T * 2 + 1, -1); dp1[0] = 0; int tm = 1; repeat(i, N) { int a, b; a = AB[i][0]; b = AB[i][1]; rrepeat(t, tm) { chmax(best, chmax(dp1[t + a], dp1[t] + b)); } tm += a; if (tm > T) tm = T; } cout << best << endl; return 0; }
#pragma GCC optimize("O3") #include "bits/stdc++.h" using namespace std; using ll = long long int; #define debugos cout #define debug(v) \ { \ printf("L%d %s > ", __LINE__, #v); \ debugos << (v) << endl; \ } #define debugv(v) \ { \ printf("L%d %s > ", __LINE__, #v); \ for (auto e : (v)) { \ debugos << e << " "; \ } \ debugos << endl; \ } #define debuga(m, w) \ { \ printf("L%d %s > ", __LINE__, #m); \ for (int x = 0; x < (w); x++) { \ debugos << (m)[x] << " "; \ } \ debugos << endl; \ } #define debugaa(m, h, w) \ { \ printf("L%d %s >\n", __LINE__, #m); \ for (int y = 0; y < (h); y++) { \ for (int x = 0; x < (w); x++) { \ debugos << (m)[y][x] << " "; \ } \ debugos << endl; \ } \ } #define all(v) (v).begin(), (v).end() #define repeat(cnt, l) \ for (auto cnt = remove_reference<decltype(l)>::type(); (cnt) < (l); ++(cnt)) #define rrepeat(cnt, l) for (auto cnt = (l)-1; 0 <= (cnt); --(cnt)) #define iterate(cnt, b, e) for (auto cnt = (b); (cnt) != (e); ++(cnt)) #define diterate(cnt, b, e) for (auto cnt = (b); (cnt) != (e); --(cnt)) const ll MD = 1000000007ll; const long double PI = 3.1415926535897932384626433832795L; inline void assert_call(bool assertion, function<void()> f) { if (!assertion) { cerr << "assertion fault:" << endl; f(); abort(); } } template <typename T1, typename T2> inline ostream &operator<<(ostream &o, const pair<T1, T2> p) { o << '(' << p.first << ':' << p.second << ')'; return o; } template <typename Vec> inline ostream &_ostream_vecprint(ostream &os, const Vec &a) { os << '['; for (const auto &e : a) os << ' ' << e << ' '; os << ']'; return os; } template <typename T> inline ostream &operator<<(ostream &o, const vector<T> &v) { return _ostream_vecprint(o, v); } template <typename T, size_t S> inline ostream &operator<<(ostream &o, const array<T, S> &v) { return _ostream_vecprint(o, v); } template <typename T> inline T &chmax(T &to, const T &val) { return to = max(to, val); } template <typename T> inline T &chmin(T &to, const T &val) { return to = min(to, val); } void bye(string s, int code = 0) { cout << s << endl; exit(code); } mt19937_64 randdev(8901016); template <typename T, typename Random = decltype(randdev), typename enable_if<is_integral<T>::value>::type * = nullptr> inline T rand(T l, T h, Random &rand = randdev) { return uniform_int_distribution<T>(l, h)(rand); } template <typename T, typename Random = decltype(randdev), typename enable_if<is_floating_point<T>::value>::type * = nullptr> inline T rand(T l, T h, Random &rand = randdev) { return uniform_real_distribution<T>(l, h)(rand); } #if defined(_WIN32) || defined(_WIN64) #define getc_x _getc_nolock #define putc_x _putc_nolock #elif defined(__GNUC__) #define getc_x getc_unlocked #define putc_x putc_unlocked #else #define getc_x getc #define putc_x putc #endif #define isvisiblechar(c) (0x21 <= (c) && (c) <= 0x7E) class MaiScanner { FILE *fp_; public: inline MaiScanner(FILE *fp) : fp_(fp) {} template <typename T> void input_integer(T &var) noexcept { var = 0; T sign = 1; int cc = getc_x(fp_); for (; cc < '0' || '9' < cc; cc = getc_x(fp_)) if (cc == '-') sign = -1; for (; '0' <= cc && cc <= '9'; cc = getc_x(fp_)) var = (var << 3) + (var << 1) + cc - '0'; var = var * sign; } inline int c() noexcept { return getc_x(fp_); } inline MaiScanner &operator>>(int &var) noexcept { input_integer<int>(var); return *this; } inline MaiScanner &operator>>(long long &var) noexcept { input_integer<long long>(var); return *this; } inline MaiScanner &operator>>(string &var) { int cc = getc_x(fp_); for (; !isvisiblechar(cc); cc = getc_x(fp_)) ; for (; isvisiblechar(cc); cc = getc_x(fp_)) var.push_back(cc); return *this; } template <typename IT> inline void in(IT begin, IT end) { for (auto it = begin; it != end; ++it) *this >> *it; } }; class MaiPrinter { FILE *fp_; public: inline MaiPrinter(FILE *fp) : fp_(fp) {} template <typename T> void output_integer(T var) noexcept { if (var == 0) { putc_x('0', fp_); return; } if (var < 0) putc_x('-', fp_), var = -var; char stack[32]; int stack_p = 0; while (var) stack[stack_p++] = '0' + (var % 10), var /= 10; while (stack_p) putc_x(stack[--stack_p], fp_); } inline MaiPrinter &operator<<(char c) noexcept { putc_x(c, fp_); return *this; } inline MaiPrinter &operator<<(int var) noexcept { output_integer<int>(var); return *this; } inline MaiPrinter &operator<<(long long var) noexcept { output_integer<long long>(var); return *this; } inline MaiPrinter &operator<<(char *str_p) noexcept { while (*str_p) putc_x(*(str_p++), fp_); return *this; } inline MaiPrinter &operator<<(const string &str) { const char *p = str.c_str(); const char *l = p + str.size(); while (p < l) putc_x(*p++, fp_); return *this; } template <typename IT> void join(IT begin, IT end, char sep = ' ') { for (bool b = 0; begin != end; ++begin, b = 1) b ? *this << sep << *begin : *this << *begin; } }; MaiScanner scanner(stdin); MaiPrinter printer(stdout); // int N; int T; array<int, 2> AB[3030]; // int main() { scanner >> N >> T; repeat(i, N) { int a, b; scanner >> a >> b; AB[i] = {a, b}; } sort(AB, AB + N); int best = 0; vector<int> dp1; dp1.resize(T + AB[N - 1][0], -1); dp1[0] = 0; int tm = 1; repeat(i, N) { int a, b; a = AB[i][0]; b = AB[i][1]; rrepeat(t, tm) { chmax(best, chmax(dp1[t + a], dp1[t] + b)); } tm += a; if (tm > T) tm = T; } cout << best << endl; return 0; }
replace
217
218
217
218
0
p02863
C++
Runtime Error
#include <cstring> #include <iostream> #include <stdlib.h> using namespace std; typedef struct { int a; int b; } Eat; int comp(const void *a, const void *b) { Eat c = *(Eat *)a; Eat d = *(Eat *)b; return c.a - d.a; } int main(void) { int n, t; cin >> n >> t; Eat eat[3002]; for (int i = 0; i < n; i++) { cin >> eat[i].a >> eat[i].b; } qsort(eat, n, sizeof(Eat), comp); // cout << eat[1].b << endl; int **dp; int h = 3001, w = 3001; dp = (int **)malloc(sizeof(int *) * w); for (int i = 0; i <= w; i++) { dp[i] = (int *)malloc(sizeof(int) * h); } for (int i = 0; i < n; i++) { for (int j = 0; j <= t - 1; j++) { if (j >= eat[i].a) dp[i + 1][j] = max(dp[i][j - eat[i].a] + eat[i].b, dp[i][j]); else dp[i + 1][j] = dp[i][j]; } } /*for(int i=0;i<n;i++){ for(int j=0;j<=t-1;j++){ printf("%d ",dp[i][j]); }puts(""); }*/ int MAX = 0; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) MAX = max(dp[i][t - 1] + eat[j].b, MAX); } cout << MAX << endl; free(dp); }
#include <cstring> #include <iostream> #include <stdlib.h> using namespace std; typedef struct { int a; int b; } Eat; int comp(const void *a, const void *b) { Eat c = *(Eat *)a; Eat d = *(Eat *)b; return c.a - d.a; } int main(void) { int n, t; cin >> n >> t; Eat eat[3002]; for (int i = 0; i < n; i++) { cin >> eat[i].a >> eat[i].b; } qsort(eat, n, sizeof(Eat), comp); // cout << eat[1].b << endl; int **dp; int h = 3001, w = 3001; dp = (int **)malloc(sizeof(int *) * w); for (int i = 0; i <= w; i++) { dp[i] = (int *)malloc(sizeof(int) * h); } for (int i = 0; i < n; i++) { for (int j = 0; j <= t - 1; j++) { if (j >= eat[i].a) dp[i + 1][j] = max(dp[i][j - eat[i].a] + eat[i].b, dp[i][j]); else dp[i + 1][j] = dp[i][j]; } } /*for(int i=0;i<n;i++){ for(int j=0;j<=t-1;j++){ printf("%d ",dp[i][j]); }puts(""); }*/ int MAX = 0; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) MAX = max(dp[i][t - 1] + eat[j].b, MAX); } cout << MAX << endl; }
delete
47
48
47
47
-6
double free or corruption (!prev)
p02863
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, N) for (int i = 0, i##_max = (N); i < i##_max; ++i) #define repp(i, l, r) for (int i = (l), i##_max = (r); i < i##_max; ++i) #define per(i, N) for (int i = (N)-1; i >= 0; --i) #define perr(i, l, r) for (int i = r - 1, i##_min(l); i >= i##_min; --i) #define all(arr) (arr).begin(), (arr).end() #define SP << " " << #define SPF << " " #define SPEEDUP \ cin.tie(0); \ ios::sync_with_stdio(false); #define MAX_I INT_MAX // 1e9 #define MIN_I INT_MIN //-1e9 #define MAX_UI UINT_MAX // 1e9 #define MAX_LL LLONG_MAX // 1e18 #define MIN_LL LLONG_MIN //-1e18 #define MAX_ULL ULLONG_MAX // 1e19 typedef long long ll; typedef pair<int, int> PII; typedef pair<char, char> PCC; typedef pair<ll, ll> PLL; typedef pair<char, int> PCI; typedef pair<int, char> PIC; typedef pair<ll, int> PLI; typedef pair<int, ll> PIL; typedef pair<ll, char> PLC; typedef pair<char, ll> PCL; inline void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; } inline void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; } inline void Yay(bool b) { cout << (b ? "Yay!" : ":(") << endl; } const int NMAX = 3010, TMAX = 3010; ll dp1[NMAX][TMAX]; ll dp2[NMAX][TMAX]; int solve() { int N, T; cin >> N >> T; vector<PII> v(N); rep(i, N) cin >> v[i].first >> v[i].second; rep(i, N) { int j = N - 1 - i; rep(t, T) { dp1[i + 1][t] = max(dp1[i + 1][t], dp1[i][t]); dp2[j][t] = max(dp2[j][t], dp2[j + 1][t]); int t_; t_ = t + v[i].first; if (t_ < T) dp1[i + 1][t_] = max(dp1[i + 1][t_], dp1[i][t] + v[i].second); t_ = t + v[j].first; if (t_ < T) dp2[j][t_] = max(dp2[j][t_], dp2[j - 1][t] + v[j].second); } } ll ans = 0; rep(i, N) { rep(t, T) { ans = max(ans, dp1[i][t] + dp2[i + 1][T - 1 - t] + v[i].second); } } cout << ans << endl; return 0; } int main(void) { SPEEDUP cout << setprecision(15); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, N) for (int i = 0, i##_max = (N); i < i##_max; ++i) #define repp(i, l, r) for (int i = (l), i##_max = (r); i < i##_max; ++i) #define per(i, N) for (int i = (N)-1; i >= 0; --i) #define perr(i, l, r) for (int i = r - 1, i##_min(l); i >= i##_min; --i) #define all(arr) (arr).begin(), (arr).end() #define SP << " " << #define SPF << " " #define SPEEDUP \ cin.tie(0); \ ios::sync_with_stdio(false); #define MAX_I INT_MAX // 1e9 #define MIN_I INT_MIN //-1e9 #define MAX_UI UINT_MAX // 1e9 #define MAX_LL LLONG_MAX // 1e18 #define MIN_LL LLONG_MIN //-1e18 #define MAX_ULL ULLONG_MAX // 1e19 typedef long long ll; typedef pair<int, int> PII; typedef pair<char, char> PCC; typedef pair<ll, ll> PLL; typedef pair<char, int> PCI; typedef pair<int, char> PIC; typedef pair<ll, int> PLI; typedef pair<int, ll> PIL; typedef pair<ll, char> PLC; typedef pair<char, ll> PCL; inline void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; } inline void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; } inline void Yay(bool b) { cout << (b ? "Yay!" : ":(") << endl; } const int NMAX = 3010, TMAX = 3010; ll dp1[NMAX][TMAX]; ll dp2[NMAX][TMAX]; int solve() { int N, T; cin >> N >> T; vector<PII> v(N); rep(i, N) cin >> v[i].first >> v[i].second; rep(i, N) { int j = N - 1 - i; rep(t, T) { dp1[i + 1][t] = max(dp1[i + 1][t], dp1[i][t]); dp2[j][t] = max(dp2[j][t], dp2[j + 1][t]); int t_; t_ = t + v[i].first; if (t_ < T) dp1[i + 1][t_] = max(dp1[i + 1][t_], dp1[i][t] + v[i].second); t_ = t + v[j].first; if (t_ < T) dp2[j][t_] = max(dp2[j][t_], dp2[j + 1][t] + v[j].second); } } ll ans = 0; rep(i, N) { rep(t, T) { ans = max(ans, dp1[i][t] + dp2[i + 1][T - 1 - t] + v[i].second); } } cout << ans << endl; return 0; } int main(void) { SPEEDUP cout << setprecision(15); solve(); return 0; }
replace
55
56
55
56
-11
p02863
C++
Runtime Error
#include <algorithm> #include <assert.h> #include <bitset> #include <complex> #include <cstring> #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; typedef pair<int, int> pint; typedef pair<ll, int> pli; const int inf = 1e9 + 7; const ll longinf = 1LL << 60; const ll mod = 1e9 + 7; int dpa[3000][3000]; int dpb[3000][3000]; int main() { int N, T; cin >> N >> T; int a[N], b[N]; rep(i, N) { cin >> a[i] >> b[i]; } rep(i, N) { rep(j, T) { if (j >= a[i]) dpa[i + 1][j] = max(dpa[i][j], dpa[i][j - a[i]] + b[i]); else dpa[i + 1][j] = dpa[i][j]; } } rep(i, N) { rep(j, T) { if (j >= a[N - 1 - i]) dpb[i + 1][j] = max(dpb[i][j], dpb[i][j - a[N - 1 - i]] + b[N - 1 - i]); else dpb[i + 1][j] = dpb[i][j]; } } int ans = 0; rep(i, N) { rep(j, T) { ans = max(dpa[i][j] + dpb[N - i - 1][T - 1 - j] + b[i], ans); } } cout << ans << endl; }
#include <algorithm> #include <assert.h> #include <bitset> #include <complex> #include <cstring> #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; typedef pair<int, int> pint; typedef pair<ll, int> pli; const int inf = 1e9 + 7; const ll longinf = 1LL << 60; const ll mod = 1e9 + 7; int dpa[3010][3010]; int dpb[3010][3010]; int main() { int N, T; cin >> N >> T; int a[N], b[N]; rep(i, N) { cin >> a[i] >> b[i]; } rep(i, N) { rep(j, T) { if (j >= a[i]) dpa[i + 1][j] = max(dpa[i][j], dpa[i][j - a[i]] + b[i]); else dpa[i + 1][j] = dpa[i][j]; } } rep(i, N) { rep(j, T) { if (j >= a[N - 1 - i]) dpb[i + 1][j] = max(dpb[i][j], dpb[i][j - a[N - 1 - i]] + b[N - 1 - i]); else dpb[i + 1][j] = dpb[i][j]; } } int ans = 0; rep(i, N) { rep(j, T) { ans = max(dpa[i][j] + dpb[N - i - 1][T - 1 - j] + b[i], ans); } } cout << ans << endl; }
replace
26
28
26
28
0
p02863
C++
Runtime Error
#include <stdint.h> #include <stdlib.h> #include <algorithm> #include <iostream> #include <numeric> #include <vector> using namespace std; using default_counter_t = int64_t; // macro #define let auto const & #define overload4(a, b, c, d, name, ...) name #define rep1(n) \ for (default_counter_t i = 0, end_i = default_counter_t(n); i < end_i; ++i) #define rep2(i, n) \ for (default_counter_t i = 0, end_##i = default_counter_t(n); i < end_##i; \ ++i) #define rep3(i, a, b) \ for (default_counter_t i = default_counter_t(a), \ end_##i = default_counter_t(b); \ i < end_##i; ++i) #define rep4(i, a, b, c) \ for (default_counter_t i = default_counter_t(a), \ end_##i = default_counter_t(b); \ i < end_##i; i += default_counter_t(c)) #define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define rrep1(n) \ for (default_counter_t i = default_counter_t(n) - 1; i >= 0; --i) #define rrep2(i, n) \ for (default_counter_t i = default_counter_t(n) - 1; i >= 0; --i) #define rrep3(i, a, b) \ for (default_counter_t i = default_counter_t(b) - 1, \ begin_##i = default_counter_t(a); \ i >= begin_##i; --i) #define rrep4(i, a, b, c) \ for (default_counter_t \ i = (default_counter_t(b) - default_counter_t(a) - 1) / \ default_counter_t(c) * default_counter_t(c) + \ default_counter_t(a), \ begin_##i = default_counter_t(a); \ i >= begin_##i; i -= default_counter_t(c)) #define rrep(...) \ overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__) #define ALL(f, c, ...) \ (([&](decltype((c)) cccc) { \ return (f)(std::begin(cccc), std::end(cccc), ##__VA_ARGS__); \ })(c)) // function template <class C> constexpr C &Sort(C &a) { std::sort(std::begin(a), std::end(a)); return a; } template <class C> constexpr auto &Min(C const &a) { return *std::min_element(std::begin(a), std::end(a)); } template <class C> constexpr auto &Max(C const &a) { return *std::max_element(std::begin(a), std::end(a)); } template <class C> constexpr auto Total(C const &c) { return std::accumulate(std::begin(c), std::end(c), C(0)); } template <typename T> auto CumSum(std::vector<T> const &v) { std::vector<T> a(v.size() + 1, T(0)); for (std::size_t i = 0; i < a.size() - size_t(1); ++i) a[i + 1] = a[i] + v[i]; return a; } template <typename T> constexpr bool ChMax(T &a, T const &b) { if (a < b) { a = b; return true; } return false; } template <typename T> constexpr bool ChMin(T &a, T const &b) { if (b < a) { a = b; return true; } return false; } void In(void) { return; } template <typename First, typename... Rest> void In(First &first, Rest &...rest) { cin >> first; In(rest...); return; } template <class T, typename I> void VectorIn(vector<T> &v, const I n) { v.resize(size_t(n)); rep(i, v.size()) cin >> v[i]; } void Out(void) { cout << "\n"; return; } template <typename First, typename... Rest> void Out(First first, Rest... rest) { cout << first << " "; Out(rest...); return; } constexpr auto yes(const bool c) { return c ? "yes" : "no"; } constexpr auto Yes(const bool c) { return c ? "Yes" : "No"; } constexpr auto YES(const bool c) { return c ? "YES" : "NO"; } #ifdef USE_STACK_TRACE_LOGGER #ifdef __clang__ #pragma clang diagnostic push #pragma clang diagnostic ignored "-Weverything" #include <glog/logging.h> #pragma clang diagnostic pop #endif //__clang__ #endif // USE_STACK_TRACE_LOGGER signed main(int argc, char *argv[]) { (void)argc; #ifdef USE_STACK_TRACE_LOGGER google::InitGoogleLogging(argv[0]); google::InstallFailureSignalHandler(); #else (void)argv; #endif // USE_STACK_TRACE_LOGGER int64_t N, T; In(N, T); vector<int64_t> A(N), B(N); rep(i, N) In(A[i], B[i]); { using P = pair<int64_t, int64_t>; vector<P> p(N); rep(i, N) p[i] = P(A[i], B[i]); Sort(p); rep(i, N) { A[i] = p[i].first; B[i] = p[i].second; } } vector<vector<int64_t>> dp(N + 1, vector<int64_t>(T + 1, 0)); rep(i, N) dp[0][i] = 0; rep(i, N) { rep(j, T + 1) { if (j < T) ChMax(dp[i + 1][min(T, j + A[i])], dp[i][j] + B[i]); ChMax(dp[i + 1][j], dp[i][j]); } } cout << dp[N][T] << endl; return EXIT_SUCCESS; }
#include <stdint.h> #include <stdlib.h> #include <algorithm> #include <iostream> #include <numeric> #include <vector> using namespace std; using default_counter_t = int64_t; // macro #define let auto const & #define overload4(a, b, c, d, name, ...) name #define rep1(n) \ for (default_counter_t i = 0, end_i = default_counter_t(n); i < end_i; ++i) #define rep2(i, n) \ for (default_counter_t i = 0, end_##i = default_counter_t(n); i < end_##i; \ ++i) #define rep3(i, a, b) \ for (default_counter_t i = default_counter_t(a), \ end_##i = default_counter_t(b); \ i < end_##i; ++i) #define rep4(i, a, b, c) \ for (default_counter_t i = default_counter_t(a), \ end_##i = default_counter_t(b); \ i < end_##i; i += default_counter_t(c)) #define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define rrep1(n) \ for (default_counter_t i = default_counter_t(n) - 1; i >= 0; --i) #define rrep2(i, n) \ for (default_counter_t i = default_counter_t(n) - 1; i >= 0; --i) #define rrep3(i, a, b) \ for (default_counter_t i = default_counter_t(b) - 1, \ begin_##i = default_counter_t(a); \ i >= begin_##i; --i) #define rrep4(i, a, b, c) \ for (default_counter_t \ i = (default_counter_t(b) - default_counter_t(a) - 1) / \ default_counter_t(c) * default_counter_t(c) + \ default_counter_t(a), \ begin_##i = default_counter_t(a); \ i >= begin_##i; i -= default_counter_t(c)) #define rrep(...) \ overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__) #define ALL(f, c, ...) \ (([&](decltype((c)) cccc) { \ return (f)(std::begin(cccc), std::end(cccc), ##__VA_ARGS__); \ })(c)) // function template <class C> constexpr C &Sort(C &a) { std::sort(std::begin(a), std::end(a)); return a; } template <class C> constexpr auto &Min(C const &a) { return *std::min_element(std::begin(a), std::end(a)); } template <class C> constexpr auto &Max(C const &a) { return *std::max_element(std::begin(a), std::end(a)); } template <class C> constexpr auto Total(C const &c) { return std::accumulate(std::begin(c), std::end(c), C(0)); } template <typename T> auto CumSum(std::vector<T> const &v) { std::vector<T> a(v.size() + 1, T(0)); for (std::size_t i = 0; i < a.size() - size_t(1); ++i) a[i + 1] = a[i] + v[i]; return a; } template <typename T> constexpr bool ChMax(T &a, T const &b) { if (a < b) { a = b; return true; } return false; } template <typename T> constexpr bool ChMin(T &a, T const &b) { if (b < a) { a = b; return true; } return false; } void In(void) { return; } template <typename First, typename... Rest> void In(First &first, Rest &...rest) { cin >> first; In(rest...); return; } template <class T, typename I> void VectorIn(vector<T> &v, const I n) { v.resize(size_t(n)); rep(i, v.size()) cin >> v[i]; } void Out(void) { cout << "\n"; return; } template <typename First, typename... Rest> void Out(First first, Rest... rest) { cout << first << " "; Out(rest...); return; } constexpr auto yes(const bool c) { return c ? "yes" : "no"; } constexpr auto Yes(const bool c) { return c ? "Yes" : "No"; } constexpr auto YES(const bool c) { return c ? "YES" : "NO"; } #ifdef USE_STACK_TRACE_LOGGER #ifdef __clang__ #pragma clang diagnostic push #pragma clang diagnostic ignored "-Weverything" #include <glog/logging.h> #pragma clang diagnostic pop #endif //__clang__ #endif // USE_STACK_TRACE_LOGGER signed main(int argc, char *argv[]) { (void)argc; #ifdef USE_STACK_TRACE_LOGGER google::InitGoogleLogging(argv[0]); google::InstallFailureSignalHandler(); #else (void)argv; #endif // USE_STACK_TRACE_LOGGER int64_t N, T; In(N, T); vector<int64_t> A(N), B(N); rep(i, N) In(A[i], B[i]); { using P = pair<int64_t, int64_t>; vector<P> p(N); rep(i, N) p[i] = P(A[i], B[i]); Sort(p); rep(i, N) { A[i] = p[i].first; B[i] = p[i].second; } } vector<vector<int64_t>> dp(N + 1, vector<int64_t>(T + 1, 0)); rep(i, N) { rep(j, T + 1) { if (j < T) ChMax(dp[i + 1][min(T, j + A[i])], dp[i][j] + B[i]); ChMax(dp[i + 1][j], dp[i][j]); } } cout << dp[N][T] << endl; return EXIT_SUCCESS; }
delete
152
154
152
152
0
p02863
C++
Runtime Error
// g++ -std=c++14 test.cpp -o test.out #include <algorithm> #include <bitset> #include <cassert> #include <functional> #include <iomanip> #include <iostream> #include <limits.h> #include <map> #include <math.h> #include <queue> #include <stdlib.h> #include <string.h> #include <utility> #include <vector> using namespace std; #define LL long long int const LL INF = (LL)(1e18) + 1; const LL mod = 1000000007ll; LL N, T; vector<pair<LL, LL>> dish; // pair<所要時間、満足度> LL ans = 0; bool compare_by_b(pair<int, int> a, pair<int, int> b) { if (a.second != b.second) { return a.first < b.first; } else { return a.second < b.second; } } void solve() { sort(dish.begin(), dish.end(), compare_by_b); // 昇順に並べ替え vector<vector<LL>> dp(3000, vector<LL>(3000, 0)); // i-1個目までの料理から、 // A(所要時間)の和がt以下となるよう選択した時の、 // Bの和(満足度)の最大値をdp[i][t]とする // ただしdp[0][t] = 0 for (int i = 0; i < N; i++) { // 食べる順番は考慮しなくても最大値を求められる // dp[i+1]をdp[i]から求める for (int t = 0; t < T; t++) { // i番目を食べない場合 // この場合 所要時間は変化しない dp[i + 1][t] = max(dp[i + 1][t], dp[i][t]); // i番目を食べる場合 // この場合所要時間にdish[i].firstを足す if (t + dish[i].first < T) { dp[i + 1][t + dish[i].first] = max(dp[i + 1][t], dp[i][t] + dish[i].second); } } ans = max(ans, dp[i][T - 1] + dish[i].second); } } int main() { cin >> N >> T; for (int i = 0; i < N; i++) { LL a, b; cin >> a >> b; dish.push_back(make_pair(a, b)); } solve(); cout << ans << endl; return 0; }
// g++ -std=c++14 test.cpp -o test.out #include <algorithm> #include <bitset> #include <cassert> #include <functional> #include <iomanip> #include <iostream> #include <limits.h> #include <map> #include <math.h> #include <queue> #include <stdlib.h> #include <string.h> #include <utility> #include <vector> using namespace std; #define LL long long int const LL INF = (LL)(1e18) + 1; const LL mod = 1000000007ll; LL N, T; vector<pair<LL, LL>> dish; // pair<所要時間、満足度> LL ans = 0; bool compare_by_b(pair<int, int> a, pair<int, int> b) { if (a.second != b.second) { return a.first < b.first; } else { return a.second < b.second; } } void solve() { sort(dish.begin(), dish.end(), compare_by_b); // 昇順に並べ替え vector<vector<LL>> dp(3001, vector<LL>(3001, 0)); // i-1個目までの料理から、 // A(所要時間)の和がt以下となるよう選択した時の、 // Bの和(満足度)の最大値をdp[i][t]とする // ただしdp[0][t] = 0 for (int i = 0; i < N; i++) { // 食べる順番は考慮しなくても最大値を求められる // dp[i+1]をdp[i]から求める for (int t = 0; t < T; t++) { // i番目を食べない場合 // この場合 所要時間は変化しない dp[i + 1][t] = max(dp[i + 1][t], dp[i][t]); // i番目を食べる場合 // この場合所要時間にdish[i].firstを足す if (t + dish[i].first < T) { dp[i + 1][t + dish[i].first] = max(dp[i + 1][t], dp[i][t] + dish[i].second); } } ans = max(ans, dp[i][T - 1] + dish[i].second); } } int main() { cin >> N >> T; for (int i = 0; i < N; i++) { LL a, b; cin >> a >> b; dish.push_back(make_pair(a, b)); } solve(); cout << ans << endl; return 0; }
replace
36
37
36
37
0
p02863
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; long long ans[3001][6001]; int main() { int n, t; cin >> n >> t; int a[n], b[n]; for (int i = 0; i < n; i++) cin >> a[i] >> b[i]; vector<pair<int, int>> x; for (int i = 0; i < n; i++) x.push_back(make_pair(a[i], b[i])); sort(x.begin(), x.end()); memset(ans, 0, sizeof(ans)); for (int i = 0; i < n; i++) { for (int j = 0; j <= t + 3000; j++) { if (0 <= j - x[i].first && j - x[i].first < t) ans[i + 1][j] = max(ans[i][j], ans[i][j - x[i].second] + x[i].second); else if (0 <= j - x[i].first) ans[i + 1][j] = max(ans[i + 1][j - 1], ans[i][j]); else ans[i + 1][j] = ans[i][j]; } } cout << ans[n][t + 3000] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long ans[3001][6001]; int main() { int n, t; cin >> n >> t; int a[n], b[n]; for (int i = 0; i < n; i++) cin >> a[i] >> b[i]; vector<pair<int, int>> x; for (int i = 0; i < n; i++) x.push_back(make_pair(a[i], b[i])); sort(x.begin(), x.end()); memset(ans, 0, sizeof(ans)); for (int i = 0; i < n; i++) { for (int j = 0; j <= t + 3000; j++) { if (0 <= j - x[i].first && j - x[i].first < t) ans[i + 1][j] = max(ans[i][j], ans[i][j - x[i].first] + x[i].second); else if (0 <= j - x[i].first) ans[i + 1][j] = max(ans[i + 1][j - 1], ans[i][j]); else ans[i + 1][j] = ans[i][j]; } } cout << ans[n][t + 3000] << endl; return 0; }
replace
18
19
18
19
-11
p02863
C++
Runtime Error
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < n; i++) #define FOR(i, m, n) for (int i = m; i < n; i++) #define ll long long int #define vl vector<ll> #define vi vector<int> const int INF = 2147483647; const ll MOD = 1000000007; using namespace std; int dp1[3001][3000] = {}; int dp2[3002][3000] = {}; int main() { int n, t; cin >> n >> t; vector<pair<int, int>> ab(n); REP(i, n) { int a, b; cin >> a >> b; ab[i + 1] = make_pair(a, b); } FOR(i, 1, n + 1) { FOR(j, 1, t) { if (j < ab[i].first) { dp1[i][j] = dp1[i - 1][j]; } else { dp1[i][j] = max(dp1[i - 1][j], dp1[i - 1][j - ab[i].first] + ab[i].second); } } } for (int i = n; i >= 0; i--) { FOR(j, 1, t) { if (j < ab[i].first) { dp2[i][j] = dp2[i + 1][j]; } else { dp2[i][j] = max(dp2[i + 1][j], dp2[i + 1][j - ab[i].first] + ab[i].second); } } } int ans = 0; FOR(i, 1, n + 1) { int tmp = 0; REP(j, t) { tmp = max(tmp, dp1[i - 1][j] + dp2[i + 1][t - j - 1]); } ans = max(ans, tmp + ab[i].second); } cout << ans << endl; }
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < n; i++) #define FOR(i, m, n) for (int i = m; i < n; i++) #define ll long long int #define vl vector<ll> #define vi vector<int> const int INF = 2147483647; const ll MOD = 1000000007; using namespace std; int dp1[3001][3000] = {}; int dp2[3002][3000] = {}; int main() { int n, t; cin >> n >> t; vector<pair<int, int>> ab(n + 1); REP(i, n) { int a, b; cin >> a >> b; ab[i + 1] = make_pair(a, b); } FOR(i, 1, n + 1) { FOR(j, 1, t) { if (j < ab[i].first) { dp1[i][j] = dp1[i - 1][j]; } else { dp1[i][j] = max(dp1[i - 1][j], dp1[i - 1][j - ab[i].first] + ab[i].second); } } } for (int i = n; i >= 0; i--) { FOR(j, 1, t) { if (j < ab[i].first) { dp2[i][j] = dp2[i + 1][j]; } else { dp2[i][j] = max(dp2[i + 1][j], dp2[i + 1][j - ab[i].first] + ab[i].second); } } } int ans = 0; FOR(i, 1, n + 1) { int tmp = 0; REP(j, t) { tmp = max(tmp, dp1[i - 1][j] + dp2[i + 1][t - j - 1]); } ans = max(ans, tmp + ab[i].second); } cout << ans << endl; }
replace
17
18
17
18
0
p02863
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int N, T; cin >> N >> T; vector<int> A(N); vector<int> B(N); for (int i = 0; i < N; i++) { cin >> A[i] >> B[i]; } vector<vector<int>> dp(N + 1, vector<int>(T, 0)); vector<vector<int>> ep(N + 1, vector<int>(T, 0)); int a, b; for (int i = 0; i < N; i++) { a = A[i]; b = B[i]; for (int j = 0; j < T; j++) { if (dp[i + 1][j] + ep[i + 1][j] < dp[i][j] + ep[i][j]) { dp[i + 1][a + j] = dp[i][j]; ep[i + 1][a + j] = ep[i][j]; } if (dp[i + 1][j] + ep[i + 1][j] < dp[i][j] + b) { dp[i + 1][j] = dp[i][j]; ep[i + 1][j] = b; } if (a + j < T) { dp[i + 1][a + j] = dp[i][j] + b; ep[i + 1][a + j] = ep[i][j]; } } } int ans = 0; for (int i = 0; i < T; i++) { ans = max(ans, dp.back()[i] + ep.back()[i]); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int N, T; cin >> N >> T; vector<int> A(N); vector<int> B(N); for (int i = 0; i < N; i++) { cin >> A[i] >> B[i]; } vector<vector<int>> dp(N + 1, vector<int>(T, 0)); vector<vector<int>> ep(N + 1, vector<int>(T, 0)); int a, b; for (int i = 0; i < N; i++) { a = A[i]; b = B[i]; for (int j = 0; j < T; j++) { if (dp[i + 1][j] + ep[i + 1][j] < dp[i][j] + ep[i][j]) { dp[i + 1][j] = dp[i][j]; ep[i + 1][j] = ep[i][j]; } if (dp[i + 1][j] + ep[i + 1][j] < dp[i][j] + b) { dp[i + 1][j] = dp[i][j]; ep[i + 1][j] = b; } if (a + j < T) { dp[i + 1][a + j] = dp[i][j] + b; ep[i + 1][a + j] = ep[i][j]; } } } int ans = 0; for (int i = 0; i < T; i++) { ans = max(ans, dp.back()[i] + ep.back()[i]); } cout << ans << endl; }
replace
21
23
21
23
-6
munmap_chunk(): invalid pointer
p02863
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int n, t; cin >> n >> t; vector<pair<int, int>> ab(n); for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; ab[i] = make_pair(a, b); } sort(ab.begin(), ab.end(), [](pair<int, int> lhs, pair<int, int> rhs) { return lhs.first < rhs.first; }); vector<vector<int>> dp(n, vector<int>(t + 1, 0)); dp[0][ab[0].first] = ab[0].second; for (int i = 0; i < n - 1; i++) { for (int j = 0; j < t; j++) { dp[i + 1][j] = max(dp[i][j], dp[i + 1][j]); if (j + ab[i + 1].first < t) { int aip1 = ab[i + 1].first; int bip1 = ab[i + 1].second; dp[i + 1][j + aip1] = max(dp[i][j] + bip1, dp[i + 1][j + aip1]); } } } int max_b = 0; for (int i = 0; i < n - 1; i++) { int max_dp = 0; for (int j = 0; j < t; j++) { max_dp = max(max_dp, dp[i][j]); } max_dp += ab[i + 1].second; max_b = max(max_b, max_dp); } cout << max_b << endl; return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int n, t; cin >> n >> t; vector<pair<int, int>> ab(n); for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; ab[i] = make_pair(a, b); } sort(ab.begin(), ab.end(), [](pair<int, int> lhs, pair<int, int> rhs) { return lhs.first < rhs.first; }); vector<vector<int>> dp(n, vector<int>(t + 10, 0)); dp[0][ab[0].first] = ab[0].second; for (int i = 0; i < n - 1; i++) { for (int j = 0; j < t; j++) { dp[i + 1][j] = max(dp[i][j], dp[i + 1][j]); if (j + ab[i + 1].first < t) { int aip1 = ab[i + 1].first; int bip1 = ab[i + 1].second; dp[i + 1][j + aip1] = max(dp[i][j] + bip1, dp[i + 1][j + aip1]); } } } int max_b = 0; for (int i = 0; i < n - 1; i++) { int max_dp = 0; for (int j = 0; j < t; j++) { max_dp = max(max_dp, dp[i][j]); } max_dp += ab[i + 1].second; max_b = max(max_b, max_dp); } cout << max_b << endl; return 0; }
replace
22
23
22
23
0
p02863
C++
Runtime Error
#pragma GCC optimize("O3") #include "bits/stdc++.h" using namespace std; using ll = long long int; #define debugos cout #define debug(v) \ { \ printf("L%d %s > ", __LINE__, #v); \ debugos << (v) << endl; \ } #define debugv(v) \ { \ printf("L%d %s > ", __LINE__, #v); \ for (auto e : (v)) { \ debugos << e << " "; \ } \ debugos << endl; \ } #define debuga(m, w) \ { \ printf("L%d %s > ", __LINE__, #m); \ for (int x = 0; x < (w); x++) { \ debugos << (m)[x] << " "; \ } \ debugos << endl; \ } #define debugaa(m, h, w) \ { \ printf("L%d %s >\n", __LINE__, #m); \ for (int y = 0; y < (h); y++) { \ for (int x = 0; x < (w); x++) { \ debugos << (m)[y][x] << " "; \ } \ debugos << endl; \ } \ } #define all(v) (v).begin(), (v).end() #define repeat(cnt, l) \ for (auto cnt = remove_reference<decltype(l)>::type(); (cnt) < (l); ++(cnt)) #define rrepeat(cnt, l) for (auto cnt = (l)-1; 0 <= (cnt); --(cnt)) #define iterate(cnt, b, e) for (auto cnt = (b); (cnt) != (e); ++(cnt)) #define diterate(cnt, b, e) for (auto cnt = (b); (cnt) != (e); --(cnt)) const ll MD = 1000000007ll; const long double PI = 3.1415926535897932384626433832795L; inline void assert_call(bool assertion, function<void()> f) { if (!assertion) { cerr << "assertion fault:" << endl; f(); abort(); } } template <typename T1, typename T2> inline ostream &operator<<(ostream &o, const pair<T1, T2> p) { o << '(' << p.first << ':' << p.second << ')'; return o; } template <typename Vec> inline ostream &_ostream_vecprint(ostream &os, const Vec &a) { os << '['; for (const auto &e : a) os << ' ' << e << ' '; os << ']'; return os; } template <typename T> inline ostream &operator<<(ostream &o, const vector<T> &v) { return _ostream_vecprint(o, v); } template <typename T, size_t S> inline ostream &operator<<(ostream &o, const array<T, S> &v) { return _ostream_vecprint(o, v); } template <typename T> inline T &chmax(T &to, const T &val) { return to = max(to, val); } template <typename T> inline T &chmin(T &to, const T &val) { return to = min(to, val); } void bye(string s, int code = 0) { cout << s << endl; exit(code); } mt19937_64 randdev(8901016); template <typename T, typename Random = decltype(randdev), typename enable_if<is_integral<T>::value>::type * = nullptr> inline T rand(T l, T h, Random &rand = randdev) { return uniform_int_distribution<T>(l, h)(rand); } template <typename T, typename Random = decltype(randdev), typename enable_if<is_floating_point<T>::value>::type * = nullptr> inline T rand(T l, T h, Random &rand = randdev) { return uniform_real_distribution<T>(l, h)(rand); } #if defined(_WIN32) || defined(_WIN64) #define getc_x _getc_nolock #define putc_x _putc_nolock #elif defined(__GNUC__) #define getc_x getc_unlocked #define putc_x putc_unlocked #else #define getc_x getc #define putc_x putc #endif #define isvisiblechar(c) (0x21 <= (c) && (c) <= 0x7E) class MaiScanner { FILE *fp_; public: inline MaiScanner(FILE *fp) : fp_(fp) {} template <typename T> void input_integer(T &var) noexcept { var = 0; T sign = 1; int cc = getc_x(fp_); for (; cc < '0' || '9' < cc; cc = getc_x(fp_)) if (cc == '-') sign = -1; for (; '0' <= cc && cc <= '9'; cc = getc_x(fp_)) var = (var << 3) + (var << 1) + cc - '0'; var = var * sign; } inline int c() noexcept { return getc_x(fp_); } inline MaiScanner &operator>>(int &var) noexcept { input_integer<int>(var); return *this; } inline MaiScanner &operator>>(long long &var) noexcept { input_integer<long long>(var); return *this; } inline MaiScanner &operator>>(string &var) { int cc = getc_x(fp_); for (; !isvisiblechar(cc); cc = getc_x(fp_)) ; for (; isvisiblechar(cc); cc = getc_x(fp_)) var.push_back(cc); return *this; } template <typename IT> inline void in(IT begin, IT end) { for (auto it = begin; it != end; ++it) *this >> *it; } }; class MaiPrinter { FILE *fp_; public: inline MaiPrinter(FILE *fp) : fp_(fp) {} template <typename T> void output_integer(T var) noexcept { if (var == 0) { putc_x('0', fp_); return; } if (var < 0) putc_x('-', fp_), var = -var; char stack[32]; int stack_p = 0; while (var) stack[stack_p++] = '0' + (var % 10), var /= 10; while (stack_p) putc_x(stack[--stack_p], fp_); } inline MaiPrinter &operator<<(char c) noexcept { putc_x(c, fp_); return *this; } inline MaiPrinter &operator<<(int var) noexcept { output_integer<int>(var); return *this; } inline MaiPrinter &operator<<(long long var) noexcept { output_integer<long long>(var); return *this; } inline MaiPrinter &operator<<(char *str_p) noexcept { while (*str_p) putc_x(*(str_p++), fp_); return *this; } inline MaiPrinter &operator<<(const string &str) { const char *p = str.c_str(); const char *l = p + str.size(); while (p < l) putc_x(*p++, fp_); return *this; } template <typename IT> void join(IT begin, IT end, char sep = ' ') { for (bool b = 0; begin != end; ++begin, b = 1) b ? *this << sep << *begin : *this << *begin; } }; MaiScanner scanner(stdin); MaiPrinter printer(stdout); // int N; int T; array<int, 2> AB[3030]; // int main() { scanner >> N >> T; repeat(i, N) { int a, b; scanner >> a >> b; AB[i] = {a, b}; } sort(AB, AB + N); int best = 0; vector<int> dp1; dp1.resize(T, -1); dp1[0] = 0; int tm = 1; repeat(i, N) { int a, b; a = AB[i][0]; b = AB[i][1]; rrepeat(t, tm) { if (T <= t + a) chmax(best, dp1[t] + b); else chmax(best, chmax(dp1[t + a], dp1[t] + b)); } tm += a; } cout << best << endl; return 0; }
#pragma GCC optimize("O3") #include "bits/stdc++.h" using namespace std; using ll = long long int; #define debugos cout #define debug(v) \ { \ printf("L%d %s > ", __LINE__, #v); \ debugos << (v) << endl; \ } #define debugv(v) \ { \ printf("L%d %s > ", __LINE__, #v); \ for (auto e : (v)) { \ debugos << e << " "; \ } \ debugos << endl; \ } #define debuga(m, w) \ { \ printf("L%d %s > ", __LINE__, #m); \ for (int x = 0; x < (w); x++) { \ debugos << (m)[x] << " "; \ } \ debugos << endl; \ } #define debugaa(m, h, w) \ { \ printf("L%d %s >\n", __LINE__, #m); \ for (int y = 0; y < (h); y++) { \ for (int x = 0; x < (w); x++) { \ debugos << (m)[y][x] << " "; \ } \ debugos << endl; \ } \ } #define all(v) (v).begin(), (v).end() #define repeat(cnt, l) \ for (auto cnt = remove_reference<decltype(l)>::type(); (cnt) < (l); ++(cnt)) #define rrepeat(cnt, l) for (auto cnt = (l)-1; 0 <= (cnt); --(cnt)) #define iterate(cnt, b, e) for (auto cnt = (b); (cnt) != (e); ++(cnt)) #define diterate(cnt, b, e) for (auto cnt = (b); (cnt) != (e); --(cnt)) const ll MD = 1000000007ll; const long double PI = 3.1415926535897932384626433832795L; inline void assert_call(bool assertion, function<void()> f) { if (!assertion) { cerr << "assertion fault:" << endl; f(); abort(); } } template <typename T1, typename T2> inline ostream &operator<<(ostream &o, const pair<T1, T2> p) { o << '(' << p.first << ':' << p.second << ')'; return o; } template <typename Vec> inline ostream &_ostream_vecprint(ostream &os, const Vec &a) { os << '['; for (const auto &e : a) os << ' ' << e << ' '; os << ']'; return os; } template <typename T> inline ostream &operator<<(ostream &o, const vector<T> &v) { return _ostream_vecprint(o, v); } template <typename T, size_t S> inline ostream &operator<<(ostream &o, const array<T, S> &v) { return _ostream_vecprint(o, v); } template <typename T> inline T &chmax(T &to, const T &val) { return to = max(to, val); } template <typename T> inline T &chmin(T &to, const T &val) { return to = min(to, val); } void bye(string s, int code = 0) { cout << s << endl; exit(code); } mt19937_64 randdev(8901016); template <typename T, typename Random = decltype(randdev), typename enable_if<is_integral<T>::value>::type * = nullptr> inline T rand(T l, T h, Random &rand = randdev) { return uniform_int_distribution<T>(l, h)(rand); } template <typename T, typename Random = decltype(randdev), typename enable_if<is_floating_point<T>::value>::type * = nullptr> inline T rand(T l, T h, Random &rand = randdev) { return uniform_real_distribution<T>(l, h)(rand); } #if defined(_WIN32) || defined(_WIN64) #define getc_x _getc_nolock #define putc_x _putc_nolock #elif defined(__GNUC__) #define getc_x getc_unlocked #define putc_x putc_unlocked #else #define getc_x getc #define putc_x putc #endif #define isvisiblechar(c) (0x21 <= (c) && (c) <= 0x7E) class MaiScanner { FILE *fp_; public: inline MaiScanner(FILE *fp) : fp_(fp) {} template <typename T> void input_integer(T &var) noexcept { var = 0; T sign = 1; int cc = getc_x(fp_); for (; cc < '0' || '9' < cc; cc = getc_x(fp_)) if (cc == '-') sign = -1; for (; '0' <= cc && cc <= '9'; cc = getc_x(fp_)) var = (var << 3) + (var << 1) + cc - '0'; var = var * sign; } inline int c() noexcept { return getc_x(fp_); } inline MaiScanner &operator>>(int &var) noexcept { input_integer<int>(var); return *this; } inline MaiScanner &operator>>(long long &var) noexcept { input_integer<long long>(var); return *this; } inline MaiScanner &operator>>(string &var) { int cc = getc_x(fp_); for (; !isvisiblechar(cc); cc = getc_x(fp_)) ; for (; isvisiblechar(cc); cc = getc_x(fp_)) var.push_back(cc); return *this; } template <typename IT> inline void in(IT begin, IT end) { for (auto it = begin; it != end; ++it) *this >> *it; } }; class MaiPrinter { FILE *fp_; public: inline MaiPrinter(FILE *fp) : fp_(fp) {} template <typename T> void output_integer(T var) noexcept { if (var == 0) { putc_x('0', fp_); return; } if (var < 0) putc_x('-', fp_), var = -var; char stack[32]; int stack_p = 0; while (var) stack[stack_p++] = '0' + (var % 10), var /= 10; while (stack_p) putc_x(stack[--stack_p], fp_); } inline MaiPrinter &operator<<(char c) noexcept { putc_x(c, fp_); return *this; } inline MaiPrinter &operator<<(int var) noexcept { output_integer<int>(var); return *this; } inline MaiPrinter &operator<<(long long var) noexcept { output_integer<long long>(var); return *this; } inline MaiPrinter &operator<<(char *str_p) noexcept { while (*str_p) putc_x(*(str_p++), fp_); return *this; } inline MaiPrinter &operator<<(const string &str) { const char *p = str.c_str(); const char *l = p + str.size(); while (p < l) putc_x(*p++, fp_); return *this; } template <typename IT> void join(IT begin, IT end, char sep = ' ') { for (bool b = 0; begin != end; ++begin, b = 1) b ? *this << sep << *begin : *this << *begin; } }; MaiScanner scanner(stdin); MaiPrinter printer(stdout); // int N; int T; array<int, 2> AB[3030]; // int main() { scanner >> N >> T; repeat(i, N) { int a, b; scanner >> a >> b; AB[i] = {a, b}; } sort(AB, AB + N); int best = 0; vector<int> dp1; dp1.resize(T, -1); dp1[0] = 0; int tm = 1; repeat(i, N) { int a, b; a = AB[i][0]; b = AB[i][1]; rrepeat(t, tm) { if (T <= t + a) chmax(best, dp1[t] + b); else chmax(best, chmax(dp1[t + a], dp1[t] + b)); } tm += a; if (tm > T) tm = T; } cout << best << endl; return 0; }
insert
231
231
231
233
0
p02863
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define mp make_pair #define pb push_back #define x first #define y second #define pii pair<int, int> #define pll pair<ll, ll> #define xmod (int)(1e9 + 7) using namespace std; int n, t, dp[3005], ans; pii a[3005]; bool used[3005]; int main() { scanf("%d%d", &n, &t); for (int i = 1; i <= n; i++) { scanf("%d%d", &a[i].x, &a[i].y); } sort(a + 1, a + 1 + n); used[0] = true; for (int i = 1; i <= n; i++) { for (int j = t; j >= 0; j--) { if (j + a[i].x > t && used[j] && j != t) { used[t] = true; dp[t] = max(dp[t], dp[j] + a[i].y); } else if (used[j]) { used[j + a[i].x] = true; dp[j + a[i].x] = max(dp[j + a[i].x], dp[j] + a[i].y); } } } for (int i = 1; i <= t; i++) { ans = max(ans, dp[i]); } printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> #define ll long long #define mp make_pair #define pb push_back #define x first #define y second #define pii pair<int, int> #define pll pair<ll, ll> #define xmod (int)(1e9 + 7) using namespace std; int n, t, dp[3005], ans; pii a[3005]; bool used[3005]; int main() { scanf("%d%d", &n, &t); for (int i = 1; i <= n; i++) { scanf("%d%d", &a[i].x, &a[i].y); } sort(a + 1, a + 1 + n); used[0] = true; for (int i = 1; i <= n; i++) { for (int j = t; j >= 0; j--) { if (j + a[i].x > t && used[j] && j != t) { used[t] = true; dp[t] = max(dp[t], dp[j] + a[i].y); } else if (j + a[i].x <= t && used[j]) { used[j + a[i].x] = true; dp[j + a[i].x] = max(dp[j + a[i].x], dp[j] + a[i].y); } } } for (int i = 1; i <= t; i++) { ans = max(ans, dp[i]); } printf("%d\n", ans); return 0; }
replace
26
27
26
27
0
p02864
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int n; const int N = 300; const int K = N; const int H = 1e9; const long long LLINF = N * 1LL * H + 7; long long dp[N + 7][K + 7]; int h[N + 7]; long long dpf(int i, int k) { long long &d = dp[i][k]; if (d != -1) { return d; } if (i > n) { return d = 0; } d = LLINF; for (int j = i; j <= i + k; j++) { d = min(d, max(0, h[j + 1] - h[i]) + dpf(j + 1, k - (j - i))); } return d; } int main() { memset(dp, -1, sizeof dp); int k; cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> h[i]; } cout << dpf(0, k) << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; int n; const int N = 300; const int K = N; const int H = 1e9; const long long LLINF = N * 1LL * H + 7; long long dp[N + 7][K + 7]; int h[N + 7]; long long dpf(int i, int k) { long long &d = dp[i][k]; if (d != -1) { return d; } if (i > n) { return d = 0; } d = LLINF; for (int j = i; j <= min(n, i + k); j++) { d = min(d, max(0, h[j + 1] - h[i]) + dpf(j + 1, k - (j - i))); } return d; } int main() { memset(dp, -1, sizeof dp); int k; cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> h[i]; } cout << dpf(0, k) << "\n"; return 0; }
replace
27
28
27
28
0
p02864
C++
Runtime Error
#include <bits/stdc++.h> #define all(x) x.begin(), x.end() using namespace std; using ll = long long; using pii = pair<int, int>; const int MN = 305 + 5; int N, K, a[MN]; ll dp[MN][MN]; int main() { ios_base::sync_with_stdio(0), cin.tie(0); memset(dp, 0x3f, sizeof(dp)); cin >> N >> K; for (int i = 1; i <= N; i++) cin >> a[i]; for (int k = 0; k <= K; k++) dp[0][k] = 0; for (int i = 1; i <= N + 1; i++) { for (int k = 0; k <= K; k++) { for (int j = 0; j <= k; j++) { dp[i][k] = min(dp[i][k], dp[i - 1 - j][k - j] + max(0, a[i] - a[i - 1 - j])); } } } cout << *min_element(dp[N + 1], dp[N + 1] + K + 1) << '\n'; return 0; }
#include <bits/stdc++.h> #define all(x) x.begin(), x.end() using namespace std; using ll = long long; using pii = pair<int, int>; const int MN = 305 + 5; int N, K, a[MN]; ll dp[MN][MN]; int main() { ios_base::sync_with_stdio(0), cin.tie(0); memset(dp, 0x3f, sizeof(dp)); cin >> N >> K; for (int i = 1; i <= N; i++) cin >> a[i]; for (int k = 0; k <= K; k++) dp[0][k] = 0; for (int i = 1; i <= N + 1; i++) { for (int k = 0; k <= min(i - 1, K); k++) { for (int j = 0; j <= k; j++) { dp[i][k] = min(dp[i][k], dp[i - 1 - j][k - j] + max(0, a[i] - a[i - 1 - j])); } } } cout << *min_element(dp[N + 1], dp[N + 1] + K + 1) << '\n'; return 0; }
replace
18
19
18
19
0
p02864
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll dp[505][505]; int n, k, h[305], b[305], he[305][305]; int main() { scanf("%d%d", &n, &k); memset(dp, 0x7f, sizeof(dp)); ll ans = dp[0][0]; int i, j; for (i = 1; i <= n; i++) { scanf("%d", &h[i]); for (j = 0; j <= k; j++) he[i][j] = h[i]; // b[i]=h[i]; } // sort(b+1,b+n+1); for (i = 0; i <= k; i++) dp[0][i] = 0; // for(i=1;i<=k;i++) dp[i][i]=0; /* for(i=1;i<=n;i++) for(j=0;j<=k;j++){ if(h[i]>he[i-1][j]) dp[i][j]=min(dp[i][j],dp[i-1][j]+h[i]-he[i-1][j]); else dp[i][j]=min(dp[i][j],dp[i-1][j]); if(dp[i][j]>dp[i-1][j-1]&&j>=1){ dp[i][j]=dp[i-1][j-1]; he[i][j]=he[i-1][j-1]; } } for(i=1;i<=n;i++){ for(j=0;j<=k;j++) printf("%d ",dp[i][j]); printf("\n"); } cout<<dp[n][k];*/ for (i = 1; i <= n + 1; i++) for (j = 0; j <= k; j++) for (int ii = 0; ii <= j; ii++) if (h[i] > h[i - ii - 1]) dp[i][j] = min(dp[i][j], dp[i - ii - 1][j - ii] + h[i] - h[i - ii - 1]); else dp[i][j] = min(dp[i][j], dp[i - ii - 1][j - ii]); for (i = 0; i <= k; i++) ans = min(ans, dp[n + 1][i]); cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll dp[505][505]; int n, k, h[305], b[305], he[305][305]; int main() { scanf("%d%d", &n, &k); memset(dp, 0x7f, sizeof(dp)); ll ans = dp[0][0]; int i, j; for (i = 1; i <= n; i++) { scanf("%d", &h[i]); for (j = 0; j <= k; j++) he[i][j] = h[i]; // b[i]=h[i]; } // sort(b+1,b+n+1); for (i = 0; i <= k; i++) dp[0][i] = 0; // for(i=1;i<=k;i++) dp[i][i]=0; /* for(i=1;i<=n;i++) for(j=0;j<=k;j++){ if(h[i]>he[i-1][j]) dp[i][j]=min(dp[i][j],dp[i-1][j]+h[i]-he[i-1][j]); else dp[i][j]=min(dp[i][j],dp[i-1][j]); if(dp[i][j]>dp[i-1][j-1]&&j>=1){ dp[i][j]=dp[i-1][j-1]; he[i][j]=he[i-1][j-1]; } } for(i=1;i<=n;i++){ for(j=0;j<=k;j++) printf("%d ",dp[i][j]); printf("\n"); } cout<<dp[n][k];*/ for (i = 1; i <= n + 1; i++) for (j = 0; j <= min(k, i - 1); j++) for (int ii = 0; ii <= j; ii++) if (h[i] > h[i - ii - 1]) dp[i][j] = min(dp[i][j], dp[i - ii - 1][j - ii] + h[i] - h[i - ii - 1]); else dp[i][j] = min(dp[i][j], dp[i - ii - 1][j - ii]); for (i = 0; i <= k; i++) ans = min(ans, dp[n + 1][i]); cout << ans; return 0; }
replace
36
37
36
37
0
p02864
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long using namespace std; const int MAXN = 301; ll dp[MAXN][MAXN], h[MAXN]; int main() { int n, k; scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) scanf("%lld", &h[i]); n++; for (int i = 1; i <= n; i++) for (int j = 0; j <= k; j++) { dp[i][j] = 1e16; for (int x = 0; x < i; x++) if (j - (i - x - 1) >= 0) dp[i][j] = min(dp[i][j], dp[x][j - (i - x - 1)] + max(0ll, h[i] - h[x])); } ll ans = 1e16; for (int i = 0; i <= k; i++) ans = min(ans, dp[n][i]); printf("%lld\n", ans); return 0; }
#include <bits/stdc++.h> #define ll long long using namespace std; const int MAXN = 302; ll dp[MAXN][MAXN], h[MAXN]; int main() { int n, k; scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) scanf("%lld", &h[i]); n++; for (int i = 1; i <= n; i++) for (int j = 0; j <= k; j++) { dp[i][j] = 1e16; for (int x = 0; x < i; x++) if (j - (i - x - 1) >= 0) dp[i][j] = min(dp[i][j], dp[x][j - (i - x - 1)] + max(0ll, h[i] - h[x])); } ll ans = 1e16; for (int i = 0; i <= k; i++) ans = min(ans, dp[n][i]); printf("%lld\n", ans); return 0; }
replace
3
4
3
4
0
p02864
C++
Runtime Error
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (int)(n); ++i) using namespace std; using lint = long long; template <class T = int> using V = vector<T>; template <class T = int> using VV = V<V<T>>; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n, k; cin >> n >> k; k = n - k; VV<lint> dp(k + 1, V<lint>(n, 1e18)); V<> h(n); REP(i, n) { cin >> h[i]; for (int x = k; x >= 2; --x) REP(j, i) { dp[x][i] = min(dp[x][i], dp[x - 1][j] + (h[j] < h[i] ? h[i] - h[j] : 0)); } dp[1][i] = h[i]; } cout << *min_element(begin(dp[k]), end(dp[k])) << endl; }
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (int)(n); ++i) using namespace std; using lint = long long; template <class T = int> using V = vector<T>; template <class T = int> using VV = V<V<T>>; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n, k; cin >> n >> k; k = n - k; if (k == 0) return cout << 0 << endl, 0; VV<lint> dp(k + 2, V<lint>(n, 1e18)); V<> h(n); REP(i, n) { cin >> h[i]; for (int x = k; x >= 2; --x) REP(j, i) { dp[x][i] = min(dp[x][i], dp[x - 1][j] + (h[j] < h[i] ? h[i] - h[j] : 0)); } dp[1][i] = h[i]; } cout << *min_element(begin(dp[k]), end(dp[k])) << endl; }
replace
14
15
14
17
0
p02864
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; #ifndef orewabaka2 #define f first #define s second #define minna(a) a.begin(), a.end() #define ovse(a) a.rbegin(), a.rend() #define pb push_back #define endl aut '\n' #define ar vector #define var auto #define rand() (((rand() << 15) + rand()) & INT32_MAX) #define ain cin, #define aut cout, #define $(a) (i64) a.size() #define in insert #define fusrodah return #define mp make_pair #define rep(i, n) for (i64 i = 0; i < (n); i++) #define rep1(i, n) for (i64 i = 1; i < (n); i++) #define fro2(i, a, b) for (i64 i = (a); i < (b); i++) #define elif else if typedef int i32; typedef unsigned u32; typedef long long i64; typedef unsigned long long u64; typedef pair<i32, i32> p32; typedef pair<i64, i64> p64; typedef double lf; typedef long double ld; typedef tree<p64, null_type, less<p64>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; namespace shyutsuryoku { const i32 STRSZ = 3e6 + 13; char inbufer[STRSZ]; inline void cscan(string &i) { i32 c = getc(stdin), cnt = 0; while (c <= 32) c = getc(stdin); while (c > 32) *(inbufer + cnt) = c, c = getc(stdin), ++cnt; *(cnt + inbufer) = 0; i = inbufer; } inline void cprint(string &i) { char *s = new char[i.size() + 1]; strcpy(s, i.c_str()); while (*s) putchar(*s), s++; } inline void cprint(const char *&i) { i32 cnt = 0; while (*(i + cnt)) putc(*(i + cnt), stdout), cnt++; } inline void cscan(i32 &i) { i = 0; i32 c = getc(stdin), b = 0; while (c <= 32) c = getc(stdin); if (c == '-') b = 1, c = getc(stdin); while (c >= '0' && c <= '9') i *= 10, i += c - '0', c = getc(stdin); if (b) i = -i; } inline void cprint(i32 i) { if (i < 0) putc('-', stdout), i = -i; i32 sz = 0; char ans[13]; while (i || !sz) ans[sz++] = i % 10 + '0', i /= 10; while (sz--) putc(ans[sz], stdout); } inline void cscan(i64 &i) { i = 0; i32 c = getc(stdin), b = 1; while (c <= 32) c = getc(stdin); if (c == '-') b = -1, c = getc(stdin); while (c >= '0' && c <= '9') i *= 10, i += c - '0', c = getc(stdin); i *= b; } inline void cprint(i64 i) { if (i < 0) putc('-', stdout), i = -i; i32 sz = 0; char ans[21]; while (i || !sz) ans[sz++] = i % 10 + '0', i /= 10; while (sz--) putc(ans[sz], stdout); } inline void cscan(u32 &i) { i = 0; i32 c = getc(stdin); while (c <= 32) c = getc(stdin); while (c >= '0' && c <= '9') i *= 10, i += c - '0', c = getc(stdin); } inline void cprint(u32 i) { i32 sz = 0; char ans[12]; while (i || !sz) ans[sz++] = i % 10 + '0', i /= 10; while (sz--) putc(ans[sz], stdout); } inline void cscan(u64 &i) { i = 0; i32 c = getc(stdin); while (c <= 32) c = getc(stdin); while (c >= '0' && c <= '9') i *= 10, i += c - '0', c = getc(stdin); } inline void cprint(u64 i) { i32 sz = 0; char ans[21]; while (i || !sz) ans[sz++] = i % 10 + '0', i /= 10; while (sz--) putc(ans[sz], stdout); } inline void cscan(lf &i) { scanf("%lf", &i); } inline void cprint(lf i) { printf("%.20lf", i); } inline void cprint(char i) { putc(i, stdout); } inline void cscan(char &i) { i32 b = getc(stdin); while (b <= 32) b = getc(stdin); i = b; } template <class T, class K> inline void cscan(pair<T, K> &i) { cscan(i.f); cscan(i.s); } template <class T, class K> inline void cprint(pair<T, K> &i) { cprint('('); cprint(i.f); cprint(' '); cprint(i.s); cprint(')'); } template <class T> inline void cscan(ar<T> &i) { for (var &j : i) cscan(j); } template <class T> inline void cprint(ar<T> &i) { for (var &j : i) cprint(j), cprint(' '); } template <class T> inline void cprint(ar<ar<T>> &i) { for (var &j : i) cprint(j), cprint('\n'); } } // namespace shyutsuryoku template <class T> ostream &operator,(ostream &os, T v) { shyutsuryoku::cprint(v); // cprint(' '); return os; } template <class T> istream &operator,(istream &is, T &v) { shyutsuryoku::cscan(v); return is; } #endif void misaka_mikoto_is_the_best_girl_ever(); i32 main() { #ifdef tavan_sersyv freopen(".in", "r", stdin); freopen(".out", "w", stdout); #endif srand(time(0)); misaka_mikoto_is_the_best_girl_ever(); #ifdef tavan_sersyv aut '\n', (lf)clock() / CLOCKS_PER_SEC, " 秒\n"; #endif fusrodah 0; } ar<i64> sp[300][301]; void misaka_mikoto_is_the_best_girl_ever() { i64 n, k, ans = 1e18; ain n, k; ar<i64> in(n); ain in; rep(i, n) { rep(j, k + 1) sp[i][j].resize(min(j + 1, i + 2), 1e18); } sp[0][0][0] = in[0]; if (k) sp[0][1][1] = 0; rep1(i, n) { rep(j, k) { rep(z, $(sp[i][j + 1])) { if (i == z) sp[i][j + 1][z + 1] = 0; else if (sp[i - 1][j][z] != 1e18) sp[i][j + 1][z + 1] = min(sp[i - 1][j][z], sp[i][j + 1][z + 1]); } } rep(j, k + 1) { rep(z, $(sp[i][j])) { if (i == z) sp[i][j][0] = min(in[i], sp[i][j][0]); else if (sp[i - 1][j][z] != 1e18) sp[i][j][0] = min(sp[i - 1][j][z] + in[i] - min(in[i], in[i - 1 - z]), sp[i][j][0]); } } } rep(i, k + 1) { for (i64 j : sp[n - 1][i]) if (j != 1e18) ans = min(ans, j); } aut 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; #ifndef orewabaka2 #define f first #define s second #define minna(a) a.begin(), a.end() #define ovse(a) a.rbegin(), a.rend() #define pb push_back #define endl aut '\n' #define ar vector #define var auto #define rand() (((rand() << 15) + rand()) & INT32_MAX) #define ain cin, #define aut cout, #define $(a) (i64) a.size() #define in insert #define fusrodah return #define mp make_pair #define rep(i, n) for (i64 i = 0; i < (n); i++) #define rep1(i, n) for (i64 i = 1; i < (n); i++) #define fro2(i, a, b) for (i64 i = (a); i < (b); i++) #define elif else if typedef int i32; typedef unsigned u32; typedef long long i64; typedef unsigned long long u64; typedef pair<i32, i32> p32; typedef pair<i64, i64> p64; typedef double lf; typedef long double ld; typedef tree<p64, null_type, less<p64>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; namespace shyutsuryoku { const i32 STRSZ = 3e6 + 13; char inbufer[STRSZ]; inline void cscan(string &i) { i32 c = getc(stdin), cnt = 0; while (c <= 32) c = getc(stdin); while (c > 32) *(inbufer + cnt) = c, c = getc(stdin), ++cnt; *(cnt + inbufer) = 0; i = inbufer; } inline void cprint(string &i) { char *s = new char[i.size() + 1]; strcpy(s, i.c_str()); while (*s) putchar(*s), s++; } inline void cprint(const char *&i) { i32 cnt = 0; while (*(i + cnt)) putc(*(i + cnt), stdout), cnt++; } inline void cscan(i32 &i) { i = 0; i32 c = getc(stdin), b = 0; while (c <= 32) c = getc(stdin); if (c == '-') b = 1, c = getc(stdin); while (c >= '0' && c <= '9') i *= 10, i += c - '0', c = getc(stdin); if (b) i = -i; } inline void cprint(i32 i) { if (i < 0) putc('-', stdout), i = -i; i32 sz = 0; char ans[13]; while (i || !sz) ans[sz++] = i % 10 + '0', i /= 10; while (sz--) putc(ans[sz], stdout); } inline void cscan(i64 &i) { i = 0; i32 c = getc(stdin), b = 1; while (c <= 32) c = getc(stdin); if (c == '-') b = -1, c = getc(stdin); while (c >= '0' && c <= '9') i *= 10, i += c - '0', c = getc(stdin); i *= b; } inline void cprint(i64 i) { if (i < 0) putc('-', stdout), i = -i; i32 sz = 0; char ans[21]; while (i || !sz) ans[sz++] = i % 10 + '0', i /= 10; while (sz--) putc(ans[sz], stdout); } inline void cscan(u32 &i) { i = 0; i32 c = getc(stdin); while (c <= 32) c = getc(stdin); while (c >= '0' && c <= '9') i *= 10, i += c - '0', c = getc(stdin); } inline void cprint(u32 i) { i32 sz = 0; char ans[12]; while (i || !sz) ans[sz++] = i % 10 + '0', i /= 10; while (sz--) putc(ans[sz], stdout); } inline void cscan(u64 &i) { i = 0; i32 c = getc(stdin); while (c <= 32) c = getc(stdin); while (c >= '0' && c <= '9') i *= 10, i += c - '0', c = getc(stdin); } inline void cprint(u64 i) { i32 sz = 0; char ans[21]; while (i || !sz) ans[sz++] = i % 10 + '0', i /= 10; while (sz--) putc(ans[sz], stdout); } inline void cscan(lf &i) { scanf("%lf", &i); } inline void cprint(lf i) { printf("%.20lf", i); } inline void cprint(char i) { putc(i, stdout); } inline void cscan(char &i) { i32 b = getc(stdin); while (b <= 32) b = getc(stdin); i = b; } template <class T, class K> inline void cscan(pair<T, K> &i) { cscan(i.f); cscan(i.s); } template <class T, class K> inline void cprint(pair<T, K> &i) { cprint('('); cprint(i.f); cprint(' '); cprint(i.s); cprint(')'); } template <class T> inline void cscan(ar<T> &i) { for (var &j : i) cscan(j); } template <class T> inline void cprint(ar<T> &i) { for (var &j : i) cprint(j), cprint(' '); } template <class T> inline void cprint(ar<ar<T>> &i) { for (var &j : i) cprint(j), cprint('\n'); } } // namespace shyutsuryoku template <class T> ostream &operator,(ostream &os, T v) { shyutsuryoku::cprint(v); // cprint(' '); return os; } template <class T> istream &operator,(istream &is, T &v) { shyutsuryoku::cscan(v); return is; } #endif void misaka_mikoto_is_the_best_girl_ever(); i32 main() { #ifdef tavan_sersyv freopen(".in", "r", stdin); freopen(".out", "w", stdout); #endif srand(time(0)); misaka_mikoto_is_the_best_girl_ever(); #ifdef tavan_sersyv aut '\n', (lf)clock() / CLOCKS_PER_SEC, " 秒\n"; #endif fusrodah 0; } ar<i64> sp[300][301]; void misaka_mikoto_is_the_best_girl_ever() { i64 n, k, ans = 1e18; ain n, k; ar<i64> in(n); ain in; rep(i, n) { rep(j, k + 1) sp[i][j].resize(min(j + 1, i + 2), 1e18); } sp[0][0][0] = in[0]; if (k) sp[0][1][1] = 0; rep1(i, n) { rep(j, k) { rep(z, $(sp[i][j + 1]) - 1) { if (i == z) sp[i][j + 1][z + 1] = 0; else if (sp[i - 1][j][z] != 1e18) sp[i][j + 1][z + 1] = min(sp[i - 1][j][z], sp[i][j + 1][z + 1]); } } rep(j, k + 1) { rep(z, $(sp[i][j])) { if (i == z) sp[i][j][0] = min(in[i], sp[i][j][0]); else if (sp[i - 1][j][z] != 1e18) sp[i][j][0] = min(sp[i - 1][j][z] + in[i] - min(in[i], in[i - 1 - z]), sp[i][j][0]); } } } rep(i, k + 1) { for (i64 j : sp[n - 1][i]) if (j != 1e18) ans = min(ans, j); } aut ans; }
replace
207
208
207
208
0
p02864
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAX_N = 3e2 + 10; const ll INF = 1e18 + 10; ll dp[MAX_N][MAX_N][MAX_N], H[MAX_N]; int N, K, M; int main() { for (int i = 0; i < MAX_N; i++) for (int j = 0; j < MAX_N; j++) for (int k = 0; k < MAX_N; k++) dp[i][j][k] = INF; scanf("%d%d", &N, &K); M = N; for (int i = 0; i < N; i++) scanf("%d", &H[i]); bool flag = false; for (int i = 0; i < N; i++) if (H[i] == 0) flag = true; if (!flag) H[M++] = 0; // for(int i=0;i<M;i++) printf("%d ",H[i]); printf("\n"); dp[0][0][0] = H[0]; if (K > 0) for (int i = 1; i <= N; i++) dp[0][1][i] = H[i]; for (int i = 1; i < N; i++) { dp[i][0][i] = min(dp[i][0][i], dp[i - 1][0][i - 1] + (H[i] > H[i - 1] ? H[i] - H[i - 1] : 0)); for (int j = 1; j <= K; j++) { for (int k = 0; k < M; k++) { // cout<<dp[i][j][k]<<endl; if (H[i] == H[k]) { for (int fr = 0; fr < M; fr++) dp[i][j][k] = min(dp[i][j][k], dp[i - 1][j][fr] + (H[i] > H[fr] ? H[i] - H[fr] : 0)); } else { dp[i][j][k] = min(dp[i][j][k], dp[i - 1][j - 1][k]); // dp[i][j][k]=min(dp[i][j][k],dp[i-1][j-1][i-1]); } // cout<<dp[i][j][k]<<endl; } } } ll ans = INF; for (int i = 0; i <= K; i++) for (int j = 0; j < M; j++) ans = min(ans, dp[N - 1][i][j]); printf("%lld\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAX_N = 3e2 + 10; const ll INF = 1e18 + 10; ll dp[MAX_N][MAX_N][MAX_N], H[MAX_N]; int N, K, M; int main() { for (int i = 0; i < MAX_N; i++) for (int j = 0; j < MAX_N; j++) for (int k = 0; k < MAX_N; k++) dp[i][j][k] = INF; scanf("%d%d", &N, &K); M = N; for (int i = 0; i < N; i++) scanf("%d", &H[i]); bool flag = false; for (int i = 0; i < N; i++) if (H[i] == 0) flag = true; if (!flag) H[M++] = 0; // for(int i=0;i<M;i++) printf("%d ",H[i]); printf("\n"); dp[0][0][0] = H[0]; if (K > 0) for (int i = 1; i <= N; i++) dp[0][1][i] = H[i]; for (int i = 1; i < N; i++) { dp[i][0][i] = min(dp[i][0][i], dp[i - 1][0][i - 1] + (H[i] > H[i - 1] ? H[i] - H[i - 1] : 0)); for (int j = 1; j <= K; j++) { for (int k = 0; k < M; k++) { // cout<<dp[i][j][k]<<endl; if (i == k) { for (int fr = 0; fr < M; fr++) dp[i][j][k] = min(dp[i][j][k], dp[i - 1][j][fr] + (H[i] > H[fr] ? H[i] - H[fr] : 0)); } else { dp[i][j][k] = min(dp[i][j][k], dp[i - 1][j - 1][k]); // dp[i][j][k]=min(dp[i][j][k],dp[i-1][j-1][i-1]); } // cout<<dp[i][j][k]<<endl; } } } ll ans = INF; for (int i = 0; i <= K; i++) for (int j = 0; j < M; j++) ans = min(ans, dp[N - 1][i][j]); printf("%lld\n", ans); return 0; }
replace
37
38
37
38
TLE
p02864
C++
Runtime Error
#include <algorithm> #include <complex> #include <cstdio> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <memory> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define REP(i, m, n) for (int i = int(m); i < int(n); i++) #define RREP(i, m, n) for (int i = int(n) - 1; i >= int(m); --i) #define EACH(i, c) for (auto &(i) : c) #define all(c) begin(c), end(c) #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort(begin(c), end(c)) #define pb emplace_back #define MP make_pair #define SZ(a) int((a).size()) #ifdef LOCAL #define DEBUG(s) cout << (s) << endl #define dump(x) cerr << #x << " = " << (x) << endl #define BR cout << endl; #else #define DEBUG(s) \ do { \ } while (0) #define dump(x) \ do { \ } while (0) #define BR #endif using namespace std; using UI = unsigned int; using UL = unsigned long; using LL = long long int; using ULL = unsigned long long; using VI = vector<int>; using VVI = vector<VI>; using VLL = vector<LL>; using VVLL = vector<VLL>; using VS = vector<string>; using PII = pair<int, int>; using VP = vector<PII>; constexpr LL INF = 1LL << 60; void solve() { int n, K; cin >> n >> K; VLL h(n); REP(i, 0, n) cin >> h[i]; VVLL dp(n + 1, VLL(n + 1, INF)); dp[0][0] = 0; REP(i, 0, n) { REP(j, 0, i + 1) { REP(k, 1, n + 1) { dp[i + 1][k] = min(dp[i + 1][k], dp[j][k - 1] + max(0LL, h[i] - (j > 0 ? h[j - 1] : 0))); } } } LL ans = INF; REP(i, 0, n + 1) ans = min(ans, dp[i + 1][n - K]); cout << ans << endl; } int main() { solve(); return 0; }
#include <algorithm> #include <complex> #include <cstdio> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <memory> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define REP(i, m, n) for (int i = int(m); i < int(n); i++) #define RREP(i, m, n) for (int i = int(n) - 1; i >= int(m); --i) #define EACH(i, c) for (auto &(i) : c) #define all(c) begin(c), end(c) #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort(begin(c), end(c)) #define pb emplace_back #define MP make_pair #define SZ(a) int((a).size()) #ifdef LOCAL #define DEBUG(s) cout << (s) << endl #define dump(x) cerr << #x << " = " << (x) << endl #define BR cout << endl; #else #define DEBUG(s) \ do { \ } while (0) #define dump(x) \ do { \ } while (0) #define BR #endif using namespace std; using UI = unsigned int; using UL = unsigned long; using LL = long long int; using ULL = unsigned long long; using VI = vector<int>; using VVI = vector<VI>; using VLL = vector<LL>; using VVLL = vector<VLL>; using VS = vector<string>; using PII = pair<int, int>; using VP = vector<PII>; constexpr LL INF = 1LL << 60; void solve() { int n, K; cin >> n >> K; VLL h(n); REP(i, 0, n) cin >> h[i]; VVLL dp(n + 1, VLL(n + 1, INF)); dp[0][0] = 0; REP(i, 0, n) { REP(j, 0, i + 1) { REP(k, 1, n + 1) { dp[i + 1][k] = min(dp[i + 1][k], dp[j][k - 1] + max(0LL, h[i] - (j > 0 ? h[j - 1] : 0))); } } } LL ans = INF; REP(i, 0, n + 1) ans = min(ans, dp[i][n - K]); cout << ans << endl; } int main() { solve(); return 0; }
replace
73
74
73
74
-11
p02864
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define fs first #define fst first #define sc second #define snd second #define sz(X) (int)(X).size() #define forn(i, n) for (int i = 0; i < int(n); i++) #define fornr(i, n) for (int i = int(n) - 1; i >= 0; i--) #define forab(i, a, b) for (int i = int(a); i < int(b); i++) #define forabr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--) #define all(X) X.begin(), X.end() typedef long long ll; typedef pair<int, int> pii; typedef unsigned int uint; typedef unsigned long long ull; typedef double dbl; typedef long double ld; typedef vector<int> vi; const int N = 301; const ll INF = 1e18; ll dp[N][N][N]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; n++; vector<ll> a(n + 1); forab(i, 1, n) cin >> a[i]; forn(i, N) forn(j, N) forn(l, N) dp[i][j][l] = INF; dp[1][0][0] = 0; forab(i, 1, n) { forn(j, k + 1) { forn(l, i) { dp[i + 1][j][i] = min(dp[i + 1][j][i], dp[i][j][l] + max(a[i] - a[l], 0ll)); if (j < k) { dp[i + 1][j + 1][l] = min(dp[i + 1][j + 1][l], dp[i][j][l]); } } } } ll ans = INF; forn(j, k + 1) forn(l, n) ans = min(ans, dp[n][j][l]); cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define fs first #define fst first #define sc second #define snd second #define sz(X) (int)(X).size() #define forn(i, n) for (int i = 0; i < int(n); i++) #define fornr(i, n) for (int i = int(n) - 1; i >= 0; i--) #define forab(i, a, b) for (int i = int(a); i < int(b); i++) #define forabr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--) #define all(X) X.begin(), X.end() typedef long long ll; typedef pair<int, int> pii; typedef unsigned int uint; typedef unsigned long long ull; typedef double dbl; typedef long double ld; typedef vector<int> vi; const int N = 310; const ll INF = 1e18; ll dp[N][N][N]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; n++; vector<ll> a(n + 1); forab(i, 1, n) cin >> a[i]; forn(i, N) forn(j, N) forn(l, N) dp[i][j][l] = INF; dp[1][0][0] = 0; forab(i, 1, n) { forn(j, k + 1) { forn(l, i) { dp[i + 1][j][i] = min(dp[i + 1][j][i], dp[i][j][l] + max(a[i] - a[l], 0ll)); if (j < k) { dp[i + 1][j + 1][l] = min(dp[i + 1][j + 1][l], dp[i][j][l]); } } } } ll ans = INF; forn(j, k + 1) forn(l, n) ans = min(ans, dp[n][j][l]); cout << ans << "\n"; return 0; }
replace
25
26
25
26
-11
p02864
C++
Time Limit Exceeded
#include <bits/stdc++.h> using std::min; int n, m, a[205]; long long dp[305][305][305], ans; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); memset(dp, 0x3f, sizeof(dp)); dp[0][0][0] = 0; for (int i = 0; i < n; i++) for (int j = 0; j <= i; j++) for (int k = 0; k <= m; k++) { if (dp[i][j][k] > 10000000000000) continue; dp[i + 1][j][k + 1] = min(dp[i + 1][j][k + 1], dp[i][j][k]); dp[i + 1][i + 1][k] = min(dp[i + 1][i + 1][k], dp[i][j][k] + std::max(0, a[i + 1] - a[j])); } ans = 1000000000000; for (int i = 0; i <= m; i++) for (int j = 0; j <= n; j++) ans = min(ans, dp[n][j][i]); printf("%lld\n", ans); }
#include <bits/stdc++.h> using std::min; int n, m, a[305]; long long dp[305][305][305], ans; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); memset(dp, 0x3f, sizeof(dp)); dp[0][0][0] = 0; for (int i = 0; i < n; i++) for (int j = 0; j <= i; j++) for (int k = 0; k <= m; k++) { if (dp[i][j][k] > 10000000000000) continue; dp[i + 1][j][k + 1] = min(dp[i + 1][j][k + 1], dp[i][j][k]); dp[i + 1][i + 1][k] = min(dp[i + 1][i + 1][k], dp[i][j][k] + std::max(0, a[i + 1] - a[j])); } ans = 1000000000000; for (int i = 0; i <= m; i++) for (int j = 0; j <= n; j++) ans = min(ans, dp[n][j][i]); printf("%lld\n", ans); }
replace
2
3
2
3
TLE
p02864
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <vector> #define pb push_back #define mk make_pair #define eb emplace_back #define eps 1e-8 #define fi first #define se second #define all(x) (x).begin(), (x).end() // #define int long long using namespace std; typedef long double ld; typedef unsigned int ui; typedef pair<int, int> pii; typedef tuple<int, int, int> tiii; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<pii> vii; typedef vector<long double> vd; const int inf = 1e9; const long long INF = 1e18; const int M = 1e9 + 7; //__int128 long long dp[305][305][305]; signed main() { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); #endif // freopen("in.txt", "r", stdin); ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout.setf(ios::fixed); cout.precision(10); int n, K; cin >> n >> K; vi a(n + 1), b(n + 1); memset(dp, 0x3f, sizeof(dp)); dp[0][0][0] = 0; for (int i = 1; i <= n; ++i) { cin >> a[i]; b[i] = a[i]; } sort(all(b)); vi p(n + 1); for (int i = 1; i <= n; ++i) { p[i] = lower_bound(all(b), a[i]) - b.begin(); } for (int i = 0; i < n; ++i) { for (int j = 0; j <= n; ++j) { for (int k = 0; k <= K; ++k) { if (b[j] != a[i + 1]) { dp[i + 1][j][k + 1] = min(dp[i + 1][j][k + 1], dp[i][j][k]); if (b[j] >= a[i + 1]) { dp[i + 1][p[i + 1]][k] = min(dp[i + 1][p[i + 1]][k], dp[i][j][k]); } else { dp[i + 1][p[i + 1]][k] = min(dp[i + 1][p[i + 1]][k], dp[i][j][k] + a[i + 1] - b[j]); } } else { dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j][k]); } } } } long long res = INF; for (int i = 0; i <= n; ++i) { res = min(dp[n][i][K], res); } cout << res << endl; return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <vector> #define pb push_back #define mk make_pair #define eb emplace_back #define eps 1e-8 #define fi first #define se second #define all(x) (x).begin(), (x).end() // #define int long long using namespace std; typedef long double ld; typedef unsigned int ui; typedef pair<int, int> pii; typedef tuple<int, int, int> tiii; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<pii> vii; typedef vector<long double> vd; const int inf = 1e9; const long long INF = 1e18; const int M = 1e9 + 7; //__int128 long long dp[305][305][305]; signed main() { // freopen("in.txt", "r", stdin); ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout.setf(ios::fixed); cout.precision(10); int n, K; cin >> n >> K; vi a(n + 1), b(n + 1); memset(dp, 0x3f, sizeof(dp)); dp[0][0][0] = 0; for (int i = 1; i <= n; ++i) { cin >> a[i]; b[i] = a[i]; } sort(all(b)); vi p(n + 1); for (int i = 1; i <= n; ++i) { p[i] = lower_bound(all(b), a[i]) - b.begin(); } for (int i = 0; i < n; ++i) { for (int j = 0; j <= n; ++j) { for (int k = 0; k <= K; ++k) { if (b[j] != a[i + 1]) { dp[i + 1][j][k + 1] = min(dp[i + 1][j][k + 1], dp[i][j][k]); if (b[j] >= a[i + 1]) { dp[i + 1][p[i + 1]][k] = min(dp[i + 1][p[i + 1]][k], dp[i][j][k]); } else { dp[i + 1][p[i + 1]][k] = min(dp[i + 1][p[i + 1]][k], dp[i][j][k] + a[i + 1] - b[j]); } } else { dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j][k]); } } } } long long res = INF; for (int i = 0; i <= n; ++i) { res = min(dp[n][i][K], res); } cout << res << endl; return 0; }
delete
37
42
37
37
-11
p02864
C++
Runtime Error
// 解説PDFの通り実装 // K個の列を選んで長さを変えるのと、K個の列をなくすのとが同じことにまず気付く必要があった #include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i) #define ALL(c) (c).begin(), (c).end() #define SIZE(v) ((int)v.size()) #define pb push_back #define mp make_pair #define mt make_tuple int main(void) { cin.sync_with_stdio(false); int N, K; cin >> N >> K; vector<int> Hs(N); REP(n, N) cin >> Hs[n]; // dp[x][y] // x: もっとも右の項の番号 // y: サイズ vector<vector<ll>> dp(N + 1, vector<ll>(N + 1)); REP(x, N) { dp[x][1] = Hs[x]; } FOR(y, 2, N + 1) { FOR(x, y - 1, N) { ll tmp = 1e18; // REP(i, x - 1) { FOR(i, y - 2, x) { auto prev = Hs[i]; auto curr = Hs[x]; tmp = min(tmp, dp[i][y - 1] + max(0, curr - prev)); } dp[x][y] = tmp; } // REP(y1, N + 1) { // cout << "y " << y1 << ": "; // REP(x1, N) { // cout << dp[x1][y1] << " "; // } // cout << endl; // } // cout << endl; } ll ans = 1e18; ll y = N - K; FOR(x, y - 1, N) { ans = min(ans, dp[x][y]); } cout << ans << endl; return 0; }
// 解説PDFの通り実装 // K個の列を選んで長さを変えるのと、K個の列をなくすのとが同じことにまず気付く必要があった #include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i) #define ALL(c) (c).begin(), (c).end() #define SIZE(v) ((int)v.size()) #define pb push_back #define mp make_pair #define mt make_tuple int main(void) { cin.sync_with_stdio(false); int N, K; cin >> N >> K; vector<int> Hs(N); REP(n, N) cin >> Hs[n]; // dp[x][y] // x: もっとも右の項の番号 // y: サイズ vector<vector<ll>> dp(N + 1, vector<ll>(N + 1)); REP(x, N) { dp[x][1] = Hs[x]; } FOR(y, 2, N + 1) { FOR(x, y - 1, N) { ll tmp = 1e18; // REP(i, x - 1) { FOR(i, y - 2, x) { auto prev = Hs[i]; auto curr = Hs[x]; tmp = min(tmp, dp[i][y - 1] + max(0, curr - prev)); } dp[x][y] = tmp; } // REP(y1, N + 1) { // cout << "y " << y1 << ": "; // REP(x1, N) { // cout << dp[x1][y1] << " "; // } // cout << endl; // } // cout << endl; } // コーナーケース!! // N = Kの場合は明らかにゼロ if (N == K) { cout << 0 << endl; } else { ll ans = 1e18; ll y = N - K; FOR(x, y - 1, N) { ans = min(ans, dp[x][y]); } cout << ans << endl; } return 0; }
replace
72
76
72
82
0
p02864
C++
Runtime Error
#define _USE_MATH_DEFINES #ifdef __GNUC__ #include <bits/stdc++.h> #else #define _CRT_SECURE_NO_WARNINGS #include <__msvc_all_public_headers.hpp> #undef min #undef max #endif #define rep(i, n) for (int i = 0; i < (n); ++i) #define FOR(i, m, n) for (int i = (m); i < (n); ++i) #define rrep(i, n) for (int i = (n)-1; i >= 0; --i) #define rfor(i, m, n) for (int i = (m); i >= (n); --i) #define sz(x) ((int)(x).size()) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define range_it(a, l, r) (a).begin() + (l), (a).begin() + (r) using namespace std; using ll = long long; using ld = long double; using VB = vector<bool>; using VVB = vector<VB>; using VI = vector<int>; using VVI = vector<VI>; using VL = vector<ll>; using VVL = vector<VL>; using VS = vector<string>; using VD = vector<ld>; using PII = pair<int, int>; using VP = vector<PII>; using PLL = pair<ll, ll>; using VPL = vector<PLL>; template <class T> using PQ = priority_queue<T>; template <class T> using PQS = priority_queue<T, vector<T>, greater<T>>; constexpr int inf = (int)1e9; constexpr ll inf_ll = (ll)1e18, MOD = 1000000007; constexpr ld PI = M_PI, EPS = 1e-12; // --- input --- // #ifdef _WIN32 #define getchar_unlocked _getchar_nolock #define putchar_unlocked _putchar_nolock #define fwrite_unlocked _fwrite_nolock #define fflush_unlocked _fflush_nolock #endif inline int gc() { return getchar_unlocked(); } template <class T> inline void InputF(T &v) { cin >> v; } inline void InputF(char &v) { while (isspace(v = gc())) ; } inline void InputF(bool &v) { char c; InputF(c); v = c != '0'; } inline void InputF(string &v) { char c; for (InputF(c); !isspace(c); c = gc()) v += c; } inline void InputF(int &v) { bool neg = false; v = 0; char c; InputF(c); if (c == '-') { neg = true; c = gc(); } for (; isdigit(c); c = gc()) v = v * 10 + (c - '0'); if (neg) v = -v; } inline void InputF(long long &v) { bool neg = false; v = 0; char c; InputF(c); if (c == '-') { neg = true; c = gc(); } for (; isdigit(c); c = gc()) v = v * 10 + (c - '0'); if (neg) v = -v; } inline void InputF(double &v) { double dp = 1; bool neg = false, adp = false; v = 0; char c; InputF(c); if (c == '-') { neg = true; c = gc(); } for (; isdigit(c) || c == '.'; c = gc()) { if (c == '.') adp = true; else if (adp) v += (c - '0') * (dp *= 0.1); else v = v * 10 + (c - '0'); } if (neg) v = -v; } template <class T, class U> inline void InputF(pair<T, U> &v) { InputF(v.first); InputF(v.second); } template <class T> inline void InputF(vector<T> &v) { for (auto &e : v) InputF(e); } template <class T> inline T InputF() { T v; InputF(v); return v; } inline string GetLine() { string v; char c; for (InputF(c); c != '\n' && c != '\0'; c = gc()) v += c; return v; } struct InputV { int n, m; InputV(int N) : n(N), m(0) {} InputV(pair<int, int> N) : n(N.first), m(N.second) {} template <class T> operator vector<T>() { vector<T> v(n); InputF(v); return v; } template <class T> operator vector<vector<T>>() { vector<vector<T>> v(n, vector<T>(m)); InputF(v); return v; } }; struct Input { template <class T> operator T() { return InputF<T>(); } int operator--(int) { int v; InputF(v); v--; return v; } InputV operator[](int n) { return InputV(n); } InputV operator[](pair<int, int> n) { return InputV(n); } void operator()() {} template <class H, class... T> void operator()(H &&h, T &&...t) { InputF(h); operator()(forward<T>(t)...); } template <class T> Input &operator,(T &&v) { InputF(v); return *this; } } in; #define input(T) InputF<T>() #define ini input(int) #define inl input(ll) #define ins input(string) #define inputs(T, ...) \ T __VA_ARGS__; \ in(__VA_ARGS__) #define INT(...) inputs(int, __VA_ARGS__) #define LL(...) inputs(ll, __VA_ARGS__) #define STR(...) inputs(string, __VA_ARGS__) // --- output --- // struct BoolStr { const char *t, *f; BoolStr(const char *_t, const char *_f) : t(_t), f(_f) {} } Yes("Yes", "No"), yes("yes", "no"), YES("YES", "NO"), Int("1", "0"); struct DivStr { const char *d, *l; DivStr(const char *_d, const char *_l) : d(_d), l(_l) {} } spc(" ", "\n"), no_spc("", "\n"), end_line("\n", "\n"), comma(",", "\n"), no_endl(" ", ""); class Output { BoolStr B{Yes}; DivStr D{spc}; void p(int v) { if (v < 0) putchar_unlocked('-'), v = -v; char b[10]; int i = 0; while (v) b[i++] = '0' + v % 10, v /= 10; if (!i) b[i++] = '0'; while (i--) putchar_unlocked(b[i]); } void p(ll v) { if (v < 0) putchar_unlocked('-'), v = -v; char b[20]; int i = 0; while (v) b[i++] = '0' + v % 10, v /= 10; if (!i) b[i++] = '0'; while (i--) putchar_unlocked(b[i]); } void p(bool v) { p(v ? B.t : B.f); } void p(char v) { putchar_unlocked(v); } void p(const char *v) { fwrite_unlocked(v, 1, strlen(v), stdout); } void p(double v) { printf("%.20f", v); } void p(ld v) { printf("%.20Lf", v); } template <class T> void p(const T &v) { cout << v; } template <class T, class U> void p(const pair<T, U> &v) { p(v.first); p(D.d); p(v.second); } template <class T> void p(const vector<T> &v) { rep(i, sz(v)) { if (i) p(D.d); p(v[i]); } } template <class T> void p(const vector<vector<T>> &v) { rep(i, sz(v)) { if (i) p(D.l); p(v[i]); } } public: Output &operator()() { p(D.l); return *this; } template <class H> Output &operator()(H &&h) { p(h); p(D.l); return *this; } template <class H, class... T> Output &operator()(H &&h, T &&...t) { p(h); p(D.d); return operator()(forward<T>(t)...); } template <class... T> void exit(T &&...t) { operator()(forward<T>(t)...); std::exit(EXIT_SUCCESS); } Output &flush() { fflush_unlocked(stdout); return *this; } Output &set(const BoolStr &b) { B = b; return *this; } Output &set(const DivStr &d) { D = d; return *this; } Output &set(const char *t, const char *f) { B = BoolStr(t, f); return *this; } } out; // --- step --- // template <class T> struct Step { class It { T a, b, c; public: constexpr It() : a(T()), b(T()), c(T()) {} constexpr It(T _b, T _c, T _s) : a(_b), b(_c), c(_s) {} constexpr It &operator++() { --b; a += c; return *this; } constexpr It operator++(int) { It tmp = *this; --b; a += c; return tmp; } constexpr const T &operator*() const { return a; } constexpr const T *operator->() const { return &a; } constexpr bool operator==(const It &i) const { return b == i.b; } constexpr bool operator!=(const It &i) const { return !(b == i.b); } constexpr T start() const { return a; } constexpr T count() const { return b; } constexpr T step() const { return c; } }; constexpr Step(T b, T c, T s) : be(b, c, s) {} constexpr It begin() const { return be; } constexpr It end() const { return en; } constexpr T start() const { return be.start(); } constexpr T count() const { return be.count(); } constexpr T step() const { return be.step(); } constexpr T sum() const { return start() * count() + step() * (count() * (count() - 1) / 2); } operator vector<T>() const { return as_vector(); } vector<T> as_vector() const { vector<T> res; res.reserve(count()); each([&](T i) { res.push_back(i); }); return res; } template <class F> void each(const F &f) const { for (T i : *this) f(i); } template <class F> auto map(const F &f) const { vector<decay_t<result_of_t<F(T)>>> res; res.reserve(count()); each([&](T i) { res.push_back(f(i)); }); return res; } template <class F> int count_if(const F &f) const { int res = 0; each([&](T i) { res += static_cast<bool>(f(i)); }); return res; } template <class F> vector<T> select(const F &f) const { vector<T> res; each([&](T i) { if (f(i)) res.push_back(i); }); return res; } template <class F> auto max(const F &f) const { auto v = map(f); return *max_element(v.begin(), v.end()); } template <class F> auto min(const F &f) const { auto v = map(f); return *min_element(v.begin(), v.end()); } template <class F, class U = decay_t<result_of_t<F(T)>>> auto sum(const F &f) const { U res = 0; each([&](T i) { res += static_cast<U>(f(i)); }); return res; } using value_type = T; using iterator = It; private: It be, en; }; template <class T> inline constexpr auto step(T a) { return Step<T>(0, a, 1); } template <class T> inline constexpr auto step(T a, T b) { return Step<T>(a, b - a, 1); } template <class T> inline constexpr auto step(T a, T b, T c) { return Step<T>(a, a < b ? (b - a - 1) / c + 1 : 0, c); } // --- functions --- // inline namespace { template <class T> inline void Sort(T &a) { sort(all(a)); } template <class T> inline void RSort(T &a) { sort(rall(a)); } template <class T> inline T Sorted(T a) { Sort(a); return a; } template <class T> inline T RSorted(T a) { RSort(a); return a; } template <class T, class F> inline void Sort(T &a, const F &f) { sort(all(a), [&](const auto &x, const auto &y) { return f(x) < f(y); }); } template <class T, class F> inline void RSort(T &a, const F &f) { sort(rall(a), [&](const auto &x, const auto &y) { return f(x) < f(y); }); } template <class T> inline void Reverse(T &a) { reverse(all(a)); } template <class T> inline void Unique(T &a) { a.erase(unique(all(a)), a.end()); } template <class T> inline void Rotate(T &a, int left) { rotate(a.begin(), a.begin() + left, a.end()); } template <class T> inline T Reversed(T a) { Reverse(a); return a; } template <class T> inline T Uniqued(T a) { Unique(a); return a; } template <class T> inline T Rotated(T a, int left) { Rotate(a, left); return a; } template <class T> inline auto Max(const T &a) { return *max_element(all(a)); } template <class T> inline auto Min(const T &a) { return *min_element(all(a)); } template <class T> inline int MaxPos(const T &a) { return max_element(all(a)) - a.begin(); } template <class T> inline int MinPos(const T &a) { return min_element(all(a)) - a.begin(); } template <class T, class F> inline auto Max(const T &a, const F &f) { return *max_element( all(a), [&](const auto &x, const auto &y) { return f(x) < f(y); }); } template <class T, class F> inline auto Min(const T &a, const F &f) { return *min_element( all(a), [&](const auto &x, const auto &y) { return f(x) < f(y); }); } template <class T, class U> inline int Count(const T &a, const U &v) { return count(all(a), v); } template <class T, class F> inline int CountIf(const T &a, const F &f) { return count_if(all(a), f); } template <class T, class U> inline int Find(const T &a, const U &v) { return find(all(a), v) - a.begin(); } template <class T, class F> inline int FindIf(const T &a, const F &f) { return find_if(all(a), f) - a.begin(); } template <class T, class U = typename T::value_type> inline U Sum(const T &a) { return accumulate(all(a), U()); } template <class T, class F> inline auto Sum(const T &v, const F &f) { return accumulate(next(v.begin()), v.end(), f(v.front()), [&](auto a, auto b) { return a + f(b); }); } template <class T, class U> inline int Lower(const T &a, const U &v) { return lower_bound(all(a), v) - a.begin(); } template <class T, class U> inline int Upper(const T &a, const U &v) { return upper_bound(all(a), v) - a.begin(); } template <class T, class F> inline void RemoveIf(T &a, const F &f) { a.erase(remove_if(all(a), f), a.end()); } template <class F> inline auto Vector(size_t size, const F &f) { vector<decay_t<result_of_t<F(size_t)>>> res(size); for (size_t i = 0; i < size; ++i) res[i] = f(i); return res; } template <class T> inline auto Grid(size_t h, size_t w, const T &v = T()) { return vector<vector<T>>(h, vector<T>(w, v)); } template <class T> inline auto Slice(const T &v, size_t i, size_t len) { return i < v.size() ? T(v.begin() + i, v.begin() + min(i + len, v.size())) : T(); } template <class T, class F> inline auto Each(const T &v, const F &f) { for (auto &i : v) f(i); } template <class T, class F> inline auto Select(const T &v, const F &f) { T res; for (const auto &e : v) if (f(e)) res.push_back(e); return res; } template <class T, class F> inline auto Map(const T &v, const F &f) { vector<decay_t<result_of_t<F(typename T::value_type)>>> res(v.size()); size_t i = 0; for (const auto &e : v) res[i++] = f(e); return res; } template <class T, class F> inline auto MapIndex(const T &v, const F &f) { vector<decay_t<result_of_t<F(size_t, typename T::value_type)>>> res(v.size()); size_t i = 0; for (auto it = v.begin(); it != v.end(); ++it, ++i) res[i] = f(i, *it); return res; } template <class T, class F> inline auto TrueIndex(const T &v, const F &f) { vector<size_t> res; for (size_t i = 0; i < v.size(); ++i) if (f(v[i])) res.push_back(i); return res; } inline string operator*(string s, size_t n) { string ret; for (size_t i = 0; i < n; ++i) ret += s; return ret; } template <class T> inline T Ceil(T n, T m) { return (n + m - 1) / m; } template <class T> inline T Ceil2(T n, T m) { return Ceil(n, m) * m; } template <class T> inline T Tri(T n) { return (n & 1) ? (n + 1) / 2 * n : n / 2 * (n + 1); } template <class T> inline T nC2(T n) { return (n & 1) ? (n - 1) / 2 * n : n / 2 * (n - 1); } template <class T> inline T Mid(const T &l, const T &r) { return l + (r - l) / 2; } inline int pop_count(int n) { return bitset<32>(n).count(); } inline int pop_count(ll n) { return bitset<64>(n).count(); } template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool inRange(const T &v, const T &min, const T &max) { return min <= v && v < max; } template <class T = ll> inline T BIT(int b) { return T(1) << b; } template <class T> inline T Gcd(T n, T m) { return m ? Gcd(m, n % m) : n; } template <class T> inline T Lcm(T n, T m) { return n / Gcd(n, m) * m; } template <class T, class U = typename T::value_type> inline U Gcdv(const T &v) { return accumulate(next(v.begin()), v.end(), U(*v.begin()), Gcd<U>); } template <class T, class U = typename T::value_type> inline U Lcmv(const T &v) { return accumulate(next(v.begin()), v.end(), U(*v.begin()), Lcm<U>); } template <class T> inline T Pow(T a, T n) { T r = 1; while (n > 0) { if (n & 1) r *= a; a *= a; n /= 2; } return r; } template <class T> inline T Powmod(T a, T n, T m = MOD) { T r = 1; while (n > 0) { if (n & 1) r = r * a % m, n--; else a = a * a % m, n /= 2; } return r; } } // namespace // --- dump --- // #if __has_include("dump.hpp") #include "dump.hpp" #else #define dump(...) ((void)0) #endif // ---------------------------------------------------------------- // int main() { INT(n, k); VL a = in[n]; auto dp = Grid<ll>(n, n, inf_ll); dp[0] = a; FOR(i, 1, n) FOR(j, i, n) { rep(k, j) { chmin(dp[i][j], dp[i - 1][k] + max(0ll, a[j] - a[k])); } } dump(dp); out(Min(dp[n - k - 1])); }
#define _USE_MATH_DEFINES #ifdef __GNUC__ #include <bits/stdc++.h> #else #define _CRT_SECURE_NO_WARNINGS #include <__msvc_all_public_headers.hpp> #undef min #undef max #endif #define rep(i, n) for (int i = 0; i < (n); ++i) #define FOR(i, m, n) for (int i = (m); i < (n); ++i) #define rrep(i, n) for (int i = (n)-1; i >= 0; --i) #define rfor(i, m, n) for (int i = (m); i >= (n); --i) #define sz(x) ((int)(x).size()) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define range_it(a, l, r) (a).begin() + (l), (a).begin() + (r) using namespace std; using ll = long long; using ld = long double; using VB = vector<bool>; using VVB = vector<VB>; using VI = vector<int>; using VVI = vector<VI>; using VL = vector<ll>; using VVL = vector<VL>; using VS = vector<string>; using VD = vector<ld>; using PII = pair<int, int>; using VP = vector<PII>; using PLL = pair<ll, ll>; using VPL = vector<PLL>; template <class T> using PQ = priority_queue<T>; template <class T> using PQS = priority_queue<T, vector<T>, greater<T>>; constexpr int inf = (int)1e9; constexpr ll inf_ll = (ll)1e18, MOD = 1000000007; constexpr ld PI = M_PI, EPS = 1e-12; // --- input --- // #ifdef _WIN32 #define getchar_unlocked _getchar_nolock #define putchar_unlocked _putchar_nolock #define fwrite_unlocked _fwrite_nolock #define fflush_unlocked _fflush_nolock #endif inline int gc() { return getchar_unlocked(); } template <class T> inline void InputF(T &v) { cin >> v; } inline void InputF(char &v) { while (isspace(v = gc())) ; } inline void InputF(bool &v) { char c; InputF(c); v = c != '0'; } inline void InputF(string &v) { char c; for (InputF(c); !isspace(c); c = gc()) v += c; } inline void InputF(int &v) { bool neg = false; v = 0; char c; InputF(c); if (c == '-') { neg = true; c = gc(); } for (; isdigit(c); c = gc()) v = v * 10 + (c - '0'); if (neg) v = -v; } inline void InputF(long long &v) { bool neg = false; v = 0; char c; InputF(c); if (c == '-') { neg = true; c = gc(); } for (; isdigit(c); c = gc()) v = v * 10 + (c - '0'); if (neg) v = -v; } inline void InputF(double &v) { double dp = 1; bool neg = false, adp = false; v = 0; char c; InputF(c); if (c == '-') { neg = true; c = gc(); } for (; isdigit(c) || c == '.'; c = gc()) { if (c == '.') adp = true; else if (adp) v += (c - '0') * (dp *= 0.1); else v = v * 10 + (c - '0'); } if (neg) v = -v; } template <class T, class U> inline void InputF(pair<T, U> &v) { InputF(v.first); InputF(v.second); } template <class T> inline void InputF(vector<T> &v) { for (auto &e : v) InputF(e); } template <class T> inline T InputF() { T v; InputF(v); return v; } inline string GetLine() { string v; char c; for (InputF(c); c != '\n' && c != '\0'; c = gc()) v += c; return v; } struct InputV { int n, m; InputV(int N) : n(N), m(0) {} InputV(pair<int, int> N) : n(N.first), m(N.second) {} template <class T> operator vector<T>() { vector<T> v(n); InputF(v); return v; } template <class T> operator vector<vector<T>>() { vector<vector<T>> v(n, vector<T>(m)); InputF(v); return v; } }; struct Input { template <class T> operator T() { return InputF<T>(); } int operator--(int) { int v; InputF(v); v--; return v; } InputV operator[](int n) { return InputV(n); } InputV operator[](pair<int, int> n) { return InputV(n); } void operator()() {} template <class H, class... T> void operator()(H &&h, T &&...t) { InputF(h); operator()(forward<T>(t)...); } template <class T> Input &operator,(T &&v) { InputF(v); return *this; } } in; #define input(T) InputF<T>() #define ini input(int) #define inl input(ll) #define ins input(string) #define inputs(T, ...) \ T __VA_ARGS__; \ in(__VA_ARGS__) #define INT(...) inputs(int, __VA_ARGS__) #define LL(...) inputs(ll, __VA_ARGS__) #define STR(...) inputs(string, __VA_ARGS__) // --- output --- // struct BoolStr { const char *t, *f; BoolStr(const char *_t, const char *_f) : t(_t), f(_f) {} } Yes("Yes", "No"), yes("yes", "no"), YES("YES", "NO"), Int("1", "0"); struct DivStr { const char *d, *l; DivStr(const char *_d, const char *_l) : d(_d), l(_l) {} } spc(" ", "\n"), no_spc("", "\n"), end_line("\n", "\n"), comma(",", "\n"), no_endl(" ", ""); class Output { BoolStr B{Yes}; DivStr D{spc}; void p(int v) { if (v < 0) putchar_unlocked('-'), v = -v; char b[10]; int i = 0; while (v) b[i++] = '0' + v % 10, v /= 10; if (!i) b[i++] = '0'; while (i--) putchar_unlocked(b[i]); } void p(ll v) { if (v < 0) putchar_unlocked('-'), v = -v; char b[20]; int i = 0; while (v) b[i++] = '0' + v % 10, v /= 10; if (!i) b[i++] = '0'; while (i--) putchar_unlocked(b[i]); } void p(bool v) { p(v ? B.t : B.f); } void p(char v) { putchar_unlocked(v); } void p(const char *v) { fwrite_unlocked(v, 1, strlen(v), stdout); } void p(double v) { printf("%.20f", v); } void p(ld v) { printf("%.20Lf", v); } template <class T> void p(const T &v) { cout << v; } template <class T, class U> void p(const pair<T, U> &v) { p(v.first); p(D.d); p(v.second); } template <class T> void p(const vector<T> &v) { rep(i, sz(v)) { if (i) p(D.d); p(v[i]); } } template <class T> void p(const vector<vector<T>> &v) { rep(i, sz(v)) { if (i) p(D.l); p(v[i]); } } public: Output &operator()() { p(D.l); return *this; } template <class H> Output &operator()(H &&h) { p(h); p(D.l); return *this; } template <class H, class... T> Output &operator()(H &&h, T &&...t) { p(h); p(D.d); return operator()(forward<T>(t)...); } template <class... T> void exit(T &&...t) { operator()(forward<T>(t)...); std::exit(EXIT_SUCCESS); } Output &flush() { fflush_unlocked(stdout); return *this; } Output &set(const BoolStr &b) { B = b; return *this; } Output &set(const DivStr &d) { D = d; return *this; } Output &set(const char *t, const char *f) { B = BoolStr(t, f); return *this; } } out; // --- step --- // template <class T> struct Step { class It { T a, b, c; public: constexpr It() : a(T()), b(T()), c(T()) {} constexpr It(T _b, T _c, T _s) : a(_b), b(_c), c(_s) {} constexpr It &operator++() { --b; a += c; return *this; } constexpr It operator++(int) { It tmp = *this; --b; a += c; return tmp; } constexpr const T &operator*() const { return a; } constexpr const T *operator->() const { return &a; } constexpr bool operator==(const It &i) const { return b == i.b; } constexpr bool operator!=(const It &i) const { return !(b == i.b); } constexpr T start() const { return a; } constexpr T count() const { return b; } constexpr T step() const { return c; } }; constexpr Step(T b, T c, T s) : be(b, c, s) {} constexpr It begin() const { return be; } constexpr It end() const { return en; } constexpr T start() const { return be.start(); } constexpr T count() const { return be.count(); } constexpr T step() const { return be.step(); } constexpr T sum() const { return start() * count() + step() * (count() * (count() - 1) / 2); } operator vector<T>() const { return as_vector(); } vector<T> as_vector() const { vector<T> res; res.reserve(count()); each([&](T i) { res.push_back(i); }); return res; } template <class F> void each(const F &f) const { for (T i : *this) f(i); } template <class F> auto map(const F &f) const { vector<decay_t<result_of_t<F(T)>>> res; res.reserve(count()); each([&](T i) { res.push_back(f(i)); }); return res; } template <class F> int count_if(const F &f) const { int res = 0; each([&](T i) { res += static_cast<bool>(f(i)); }); return res; } template <class F> vector<T> select(const F &f) const { vector<T> res; each([&](T i) { if (f(i)) res.push_back(i); }); return res; } template <class F> auto max(const F &f) const { auto v = map(f); return *max_element(v.begin(), v.end()); } template <class F> auto min(const F &f) const { auto v = map(f); return *min_element(v.begin(), v.end()); } template <class F, class U = decay_t<result_of_t<F(T)>>> auto sum(const F &f) const { U res = 0; each([&](T i) { res += static_cast<U>(f(i)); }); return res; } using value_type = T; using iterator = It; private: It be, en; }; template <class T> inline constexpr auto step(T a) { return Step<T>(0, a, 1); } template <class T> inline constexpr auto step(T a, T b) { return Step<T>(a, b - a, 1); } template <class T> inline constexpr auto step(T a, T b, T c) { return Step<T>(a, a < b ? (b - a - 1) / c + 1 : 0, c); } // --- functions --- // inline namespace { template <class T> inline void Sort(T &a) { sort(all(a)); } template <class T> inline void RSort(T &a) { sort(rall(a)); } template <class T> inline T Sorted(T a) { Sort(a); return a; } template <class T> inline T RSorted(T a) { RSort(a); return a; } template <class T, class F> inline void Sort(T &a, const F &f) { sort(all(a), [&](const auto &x, const auto &y) { return f(x) < f(y); }); } template <class T, class F> inline void RSort(T &a, const F &f) { sort(rall(a), [&](const auto &x, const auto &y) { return f(x) < f(y); }); } template <class T> inline void Reverse(T &a) { reverse(all(a)); } template <class T> inline void Unique(T &a) { a.erase(unique(all(a)), a.end()); } template <class T> inline void Rotate(T &a, int left) { rotate(a.begin(), a.begin() + left, a.end()); } template <class T> inline T Reversed(T a) { Reverse(a); return a; } template <class T> inline T Uniqued(T a) { Unique(a); return a; } template <class T> inline T Rotated(T a, int left) { Rotate(a, left); return a; } template <class T> inline auto Max(const T &a) { return *max_element(all(a)); } template <class T> inline auto Min(const T &a) { return *min_element(all(a)); } template <class T> inline int MaxPos(const T &a) { return max_element(all(a)) - a.begin(); } template <class T> inline int MinPos(const T &a) { return min_element(all(a)) - a.begin(); } template <class T, class F> inline auto Max(const T &a, const F &f) { return *max_element( all(a), [&](const auto &x, const auto &y) { return f(x) < f(y); }); } template <class T, class F> inline auto Min(const T &a, const F &f) { return *min_element( all(a), [&](const auto &x, const auto &y) { return f(x) < f(y); }); } template <class T, class U> inline int Count(const T &a, const U &v) { return count(all(a), v); } template <class T, class F> inline int CountIf(const T &a, const F &f) { return count_if(all(a), f); } template <class T, class U> inline int Find(const T &a, const U &v) { return find(all(a), v) - a.begin(); } template <class T, class F> inline int FindIf(const T &a, const F &f) { return find_if(all(a), f) - a.begin(); } template <class T, class U = typename T::value_type> inline U Sum(const T &a) { return accumulate(all(a), U()); } template <class T, class F> inline auto Sum(const T &v, const F &f) { return accumulate(next(v.begin()), v.end(), f(v.front()), [&](auto a, auto b) { return a + f(b); }); } template <class T, class U> inline int Lower(const T &a, const U &v) { return lower_bound(all(a), v) - a.begin(); } template <class T, class U> inline int Upper(const T &a, const U &v) { return upper_bound(all(a), v) - a.begin(); } template <class T, class F> inline void RemoveIf(T &a, const F &f) { a.erase(remove_if(all(a), f), a.end()); } template <class F> inline auto Vector(size_t size, const F &f) { vector<decay_t<result_of_t<F(size_t)>>> res(size); for (size_t i = 0; i < size; ++i) res[i] = f(i); return res; } template <class T> inline auto Grid(size_t h, size_t w, const T &v = T()) { return vector<vector<T>>(h, vector<T>(w, v)); } template <class T> inline auto Slice(const T &v, size_t i, size_t len) { return i < v.size() ? T(v.begin() + i, v.begin() + min(i + len, v.size())) : T(); } template <class T, class F> inline auto Each(const T &v, const F &f) { for (auto &i : v) f(i); } template <class T, class F> inline auto Select(const T &v, const F &f) { T res; for (const auto &e : v) if (f(e)) res.push_back(e); return res; } template <class T, class F> inline auto Map(const T &v, const F &f) { vector<decay_t<result_of_t<F(typename T::value_type)>>> res(v.size()); size_t i = 0; for (const auto &e : v) res[i++] = f(e); return res; } template <class T, class F> inline auto MapIndex(const T &v, const F &f) { vector<decay_t<result_of_t<F(size_t, typename T::value_type)>>> res(v.size()); size_t i = 0; for (auto it = v.begin(); it != v.end(); ++it, ++i) res[i] = f(i, *it); return res; } template <class T, class F> inline auto TrueIndex(const T &v, const F &f) { vector<size_t> res; for (size_t i = 0; i < v.size(); ++i) if (f(v[i])) res.push_back(i); return res; } inline string operator*(string s, size_t n) { string ret; for (size_t i = 0; i < n; ++i) ret += s; return ret; } template <class T> inline T Ceil(T n, T m) { return (n + m - 1) / m; } template <class T> inline T Ceil2(T n, T m) { return Ceil(n, m) * m; } template <class T> inline T Tri(T n) { return (n & 1) ? (n + 1) / 2 * n : n / 2 * (n + 1); } template <class T> inline T nC2(T n) { return (n & 1) ? (n - 1) / 2 * n : n / 2 * (n - 1); } template <class T> inline T Mid(const T &l, const T &r) { return l + (r - l) / 2; } inline int pop_count(int n) { return bitset<32>(n).count(); } inline int pop_count(ll n) { return bitset<64>(n).count(); } template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool inRange(const T &v, const T &min, const T &max) { return min <= v && v < max; } template <class T = ll> inline T BIT(int b) { return T(1) << b; } template <class T> inline T Gcd(T n, T m) { return m ? Gcd(m, n % m) : n; } template <class T> inline T Lcm(T n, T m) { return n / Gcd(n, m) * m; } template <class T, class U = typename T::value_type> inline U Gcdv(const T &v) { return accumulate(next(v.begin()), v.end(), U(*v.begin()), Gcd<U>); } template <class T, class U = typename T::value_type> inline U Lcmv(const T &v) { return accumulate(next(v.begin()), v.end(), U(*v.begin()), Lcm<U>); } template <class T> inline T Pow(T a, T n) { T r = 1; while (n > 0) { if (n & 1) r *= a; a *= a; n /= 2; } return r; } template <class T> inline T Powmod(T a, T n, T m = MOD) { T r = 1; while (n > 0) { if (n & 1) r = r * a % m, n--; else a = a * a % m, n /= 2; } return r; } } // namespace // --- dump --- // #if __has_include("dump.hpp") #include "dump.hpp" #else #define dump(...) ((void)0) #endif // ---------------------------------------------------------------- // int main() { INT(n, k); VL a = in[n]; if (n == k) { out.exit(0); } auto dp = Grid<ll>(n, n, inf_ll); dp[0] = a; FOR(i, 1, n) FOR(j, i, n) { rep(k, j) { chmin(dp[i][j], dp[i - 1][k] + max(0ll, a[j] - a[k])); } } dump(dp); out(Min(dp[n - k - 1])); }
insert
577
577
577
581
0
p02864
C++
Runtime Error
#include "bits/stdc++.h" #include <random> using namespace std; typedef long long int lint; typedef pair<lint, lint> plint; typedef pair<double long, double long> pld; #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((lint)(x).size()) #define POW2(n) (1LL << (n)) #define FOR(i, begin, end) \ for (lint i = (begin), i##_end_ = (end); i < i##_end_; i++) #define IFOR(i, begin, end) \ for (lint i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--) #define REP(i, n) FOR(i, 0, n) #define IREP(i, n) IFOR(i, 0, n) template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); } template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); } const lint MOD = 1e9 + 7, INF = 1e18; lint N, K, H[302]; lint dp[303][301]; // dp[見た地点][使ったKの数] = 必要な手数の最小値 int main() { cin >> N >> K; H[0] = 0; H[N + 1] = 0; REP(i, N) { cin >> H[i + 1]; } REP(i, N + 2) { REP(j, K + 1) { dp[i][j] = INF; } } dp[0][0] = 0; REP(i, N + 2) { REP(j, K + 1) { // 使うKの数 REP(k, K - j + 1) { if (H[i] > H[i + k + 1]) chmin(dp[i + k + 1][j + k], dp[i][j]); else chmin(dp[i + k + 1][j + k], dp[i][j] + (H[i + k + 1] - H[i])); } } } lint ans = INF; REP(i, K + 1) { chmin(ans, dp[N + 1][i]); } cout << ans << endl; }
#include "bits/stdc++.h" #include <random> using namespace std; typedef long long int lint; typedef pair<lint, lint> plint; typedef pair<double long, double long> pld; #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((lint)(x).size()) #define POW2(n) (1LL << (n)) #define FOR(i, begin, end) \ for (lint i = (begin), i##_end_ = (end); i < i##_end_; i++) #define IFOR(i, begin, end) \ for (lint i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--) #define REP(i, n) FOR(i, 0, n) #define IREP(i, n) IFOR(i, 0, n) template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); } template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); } const lint MOD = 1e9 + 7, INF = 1e18; lint N, K, H[302]; lint dp[303][301]; // dp[見た地点][使ったKの数] = 必要な手数の最小値 int main() { cin >> N >> K; H[0] = 0; H[N + 1] = 0; REP(i, N) { cin >> H[i + 1]; } REP(i, N + 2) { REP(j, K + 1) { dp[i][j] = INF; } } dp[0][0] = 0; REP(i, N + 2) { REP(j, K + 1) { // 使うKの数 REP(k, K - j + 1) { if (i + k + 1 > N + 1) continue; if (H[i] > H[i + k + 1]) chmin(dp[i + k + 1][j + k], dp[i][j]); else chmin(dp[i + k + 1][j + k], dp[i][j] + (H[i + k + 1] - H[i])); } } } lint ans = INF; REP(i, K + 1) { chmin(ans, dp[N + 1][i]); } cout << ans << endl; }
insert
55
55
55
57
0
p02864
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define REP(i, n) FOR(i, 0, n) #define ALL(v) begin(v), end(v) #define fi first #define se second template <typename A, typename B> inline bool chmax(A &a, B b) { if (a < b) { a = b; return 1; } return 0; } template <typename A, typename B> inline bool chmin(A &a, B b) { if (a > b) { a = b; return 1; } return 0; } using ll = long long; using pii = pair<int, int>; constexpr ll INF = 1ll << 30; constexpr ll longINF = 1ll << 60; constexpr ll MOD = 1000000007; constexpr bool debug = 0; //---------------------------------// int N, K; int H[300]; ll dp[301][301]; int main() { cin >> N >> K; REP(i, N) scanf("%d", H + i + 1); fill(dp[0], dp[N + 1], longINF); dp[0][0] = 0; REP(i, N) REP(j, N - K + 1) { if (dp[i][j] == longINF) continue; FOR(k, i + 1, N + 1) { chmin(dp[k][j + 1], dp[i][j] + max(0, H[k] - H[i])); } } ll ans = longINF; REP(i, N + 1) chmin(ans, dp[i][N - K]); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define REP(i, n) FOR(i, 0, n) #define ALL(v) begin(v), end(v) #define fi first #define se second template <typename A, typename B> inline bool chmax(A &a, B b) { if (a < b) { a = b; return 1; } return 0; } template <typename A, typename B> inline bool chmin(A &a, B b) { if (a > b) { a = b; return 1; } return 0; } using ll = long long; using pii = pair<int, int>; constexpr ll INF = 1ll << 30; constexpr ll longINF = 1ll << 60; constexpr ll MOD = 1000000007; constexpr bool debug = 0; //---------------------------------// int N, K; int H[301]; ll dp[301][301]; int main() { cin >> N >> K; REP(i, N) scanf("%d", H + i + 1); fill(dp[0], dp[N + 1], longINF); dp[0][0] = 0; REP(i, N) REP(j, N - K + 1) { if (dp[i][j] == longINF) continue; FOR(k, i + 1, N + 1) { chmin(dp[k][j + 1], dp[i][j] + max(0, H[k] - H[i])); } } ll ans = longINF; REP(i, N + 1) chmin(ans, dp[i][N - K]); cout << ans << endl; return 0; }
replace
30
31
30
31
0
p02864
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define per(i, n) for (int i = (n)-1; i >= 0; --i) #define repa(i, n) for (int i = 1; i < (n); ++i) #define foreach(i, n) for (auto &i : (n)) #define pii pair<int, int> #define pll pair<long long, long long> #define all(x) (x).begin(), (x).end() #define bit(x) (1ll << (x)) using namespace std; using ll = long long; const ll MOD = (ll)1e9 + 7; // const ll MOD = 998244353; // const ll MOD = 924844033; const int INF = (ll)1e9 + 7; const ll INFLL = (ll)1e18; using namespace std; template <class t> using vvector = vector<vector<t>>; template <class t> using vvvector = vector<vector<vector<t>>>; template <class t> using priority_queuer = priority_queue<t, vector<t>, greater<t>>; template <class t, class u> bool chmax(t &a, u b) { if (a < b) { a = b; return true; } return false; } template <class t, class u> bool chmin(t &a, u b) { if (a > b) { a = b; return true; } return false; } #ifdef DEBUG #define OUTPUT(x) (output(x), outendl()) #else #define OUTPUT(x) (void)0 #endif ll modpow(ll x, ll b) { ll res = 1; while (b) { if (b & 1) res = res * x % MOD; x = x * x % MOD; b >>= 1; } return res; } ll modinv(ll x) { return modpow(x, MOD - 2); } bool was_output = false; template <class t> void output(t a) { if (was_output) cout << " "; cout << a; was_output = true; } void outendl() { was_output = false; cout << endl; } // below here int n; int k; vector<int> line(n); vvvector<ll> dp; ll func(int place, int select, int remain) { if (place == n + 1) { return line[select]; } if (dp[place][select][remain] >= 0) { return dp[place][select][remain]; } ll res = INFLL; rep(i, place) { if (!remain) { break; } if (line[select] < line[i]) { continue; } chmin(res, line[select] - line[i] + func(place + 1, i, remain - 1)); } if (line[select] > line[place]) { chmin(res, line[select] - line[place] + func(place + 1, place, remain)); } else { chmin(res, func(place + 1, place, remain)); } return dp[place][select][remain] = res; } int main() { cin >> n >> k; line.push_back(0); rep(i, n) { int d; cin >> d; line.push_back(d); } dp.resize(n + 1, vvector<ll>(n + 1, vector<ll>(k + 1, -1))); cout << func(1, 0, k) << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define per(i, n) for (int i = (n)-1; i >= 0; --i) #define repa(i, n) for (int i = 1; i < (n); ++i) #define foreach(i, n) for (auto &i : (n)) #define pii pair<int, int> #define pll pair<long long, long long> #define all(x) (x).begin(), (x).end() #define bit(x) (1ll << (x)) using namespace std; using ll = long long; const ll MOD = (ll)1e9 + 7; // const ll MOD = 998244353; // const ll MOD = 924844033; const int INF = (ll)1e9 + 7; const ll INFLL = (ll)1e18; using namespace std; template <class t> using vvector = vector<vector<t>>; template <class t> using vvvector = vector<vector<vector<t>>>; template <class t> using priority_queuer = priority_queue<t, vector<t>, greater<t>>; template <class t, class u> bool chmax(t &a, u b) { if (a < b) { a = b; return true; } return false; } template <class t, class u> bool chmin(t &a, u b) { if (a > b) { a = b; return true; } return false; } #ifdef DEBUG #define OUTPUT(x) (output(x), outendl()) #else #define OUTPUT(x) (void)0 #endif ll modpow(ll x, ll b) { ll res = 1; while (b) { if (b & 1) res = res * x % MOD; x = x * x % MOD; b >>= 1; } return res; } ll modinv(ll x) { return modpow(x, MOD - 2); } bool was_output = false; template <class t> void output(t a) { if (was_output) cout << " "; cout << a; was_output = true; } void outendl() { was_output = false; cout << endl; } // below here int n; int k; vector<int> line(n); vvvector<ll> dp; ll func(int place, int select, int remain) { if (place == n + 1) { return line[select]; } if (dp[place][select][remain] >= 0) { return dp[place][select][remain]; } ll res = INFLL; if (remain) { chmin(res, func(place + 1, select, remain - 1)); chmin(res, line[select] + func(place + 1, 0, remain - 1)); } if (line[select] > line[place]) { chmin(res, line[select] - line[place] + func(place + 1, place, remain)); } else { chmin(res, func(place + 1, place, remain)); } return dp[place][select][remain] = res; } int main() { cin >> n >> k; line.push_back(0); rep(i, n) { int d; cin >> d; line.push_back(d); } dp.resize(n + 1, vvector<ll>(n + 1, vector<ll>(k + 1, -1))); cout << func(1, 0, k) << endl; return 0; }
replace
84
92
84
87
TLE
p02864
C++
Runtime Error
#define _USE_MATH_DEFINES #include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <vector> using namespace std; #define MOD 1000000007 #define rep(i, m, n) for (int(i) = (int)(m); i < (int)(n); i++) #define REP(i, n) rep(i, 0, n) #define FOR(i, c) \ for (decltype((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ll long long #define ull unsigned long long #define all(hoge) (hoge).begin(), (hoge).end() typedef pair<ll, ll> P; const long long INF = 1LL << 60; typedef vector<ll> Array; typedef vector<Array> Matrix; string operator*(const string &s, int k) { if (k == 0) return ""; string p = (s + s) * (k / 2); if (k % 2 == 1) p += s; return p; } 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; } struct Edge { // グラフ ll to, cap, rev; Edge(ll _to, ll _cap, ll _rev) { to = _to; cap = _cap; rev = _rev; } }; typedef vector<Edge> Edges; typedef vector<Edges> Graph; void add_edge(Graph &G, ll from, ll to, ll cap, bool revFlag, ll revCap) { // 最大フロー求める Ford-fulkerson G[from].push_back(Edge(to, cap, (ll)G[to].size())); if (revFlag) G[to].push_back(Edge( from, revCap, (ll)G[from].size() - 1)); // 最小カットの場合逆辺は0にする } ll max_flow_dfs(Graph &G, ll v, ll t, ll f, vector<bool> &used) { if (v == t) return f; used[v] = true; for (int i = 0; i < G[v].size(); ++i) { Edge &e = G[v][i]; if (!used[e.to] && e.cap > 0) { ll d = max_flow_dfs(G, e.to, t, min(f, e.cap), used); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } // 二分グラフの最大マッチングを求めたりも出来る また二部グラフの最大独立集合は頂点数-最大マッチングのサイズ ll max_flow(Graph &G, ll s, ll t) // O(V(V+E)) { ll flow = 0; for (;;) { vector<bool> used(G.size()); REP(i, used.size()) used[i] = false; ll f = max_flow_dfs(G, s, t, INF, used); if (f == 0) { return flow; } flow += f; } } void BellmanFord(Graph &G, ll s, Array &d, Array &negative) { // O(|E||V|) d.resize(G.size()); negative.resize(G.size()); REP(i, d.size()) d[i] = INF; REP(i, d.size()) negative[i] = false; d[s] = 0; REP(k, G.size() - 1) { REP(i, G.size()) { REP(j, G[i].size()) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; } } } } REP(k, G.size() - 1) { REP(i, G.size()) { REP(j, G[i].size()) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; negative[G[i][j].to] = true; } if (negative[i] == true) negative[G[i][j].to] = true; } } } } void Dijkstra(Graph &G, ll s, Array &d) { // O(|E|log|V|) d.resize(G.size()); REP(i, d.size()) d[i] = INF; d[s] = 0; priority_queue<P, vector<P>, greater<P>> q; q.push(make_pair(0, s)); while (!q.empty()) { P a = q.top(); q.pop(); if (d[a.second] < a.first) continue; REP(i, G[a.second].size()) { Edge e = G[a.second][i]; if (d[e.to] > d[a.second] + e.cap) { d[e.to] = d[a.second] + e.cap; q.push(make_pair(d[e.to], e.to)); } } } } void WarshallFloyd(Graph &G, Matrix &d) { // O(V^3) d.resize(G.size()); REP(i, d.size()) d[i].resize(G.size()); REP(i, d.size()) { REP(j, d[i].size()) { d[i][j] = INF; } } REP(i, G.size()) { REP(j, G[i].size()) { d[i][G[i][j].to] = G[i][j].cap; } } REP(i, G.size()) { REP(j, G.size()) { REP(k, G.size()) { chmin(d[j][k], d[j][i] + d[i][k]); } } } } bool tsort(Graph &graph, vector<int> &order) { // トポロジカルソートO(E+V) int n = graph.size(), k = 0; Array in(n); for (auto &es : graph) for (auto &e : es) in[e.to]++; priority_queue<ll, Array, greater<ll>> que; REP(i, n) if (in[i] == 0) que.push(i); while (que.size()) { int v = que.top(); que.pop(); order.push_back(v); for (auto &e : graph[v]) if (--in[e.to] == 0) que.push(e.to); } if (order.size() != n) return false; else return true; } class lca { public: const int n = 0; const int log2_n = 0; std::vector<std::vector<int>> parent; std::vector<int> depth; lca() {} lca(const Graph &g, int root) : n(g.size()), log2_n(log2(n) + 1), parent(log2_n, std::vector<int>(n)), depth(n) { dfs(g, root, -1, 0); for (int k = 0; k + 1 < log2_n; k++) { for (int v = 0; v < (int)g.size(); v++) { if (parent[k][v] < 0) parent[k + 1][v] = -1; else parent[k + 1][v] = parent[k][parent[k][v]]; } } } void dfs(const Graph &g, int v, int p, int d) { parent[0][v] = p; depth[v] = d; for (auto &e : g[v]) { if (e.to != p) dfs(g, e.to, v, d + 1); } } int get(int u, int v) { if (depth[u] > depth[v]) std::swap(u, v); for (int k = 0; k < log2_n; k++) { if ((depth[v] - depth[u]) >> k & 1) { v = parent[k][v]; } } if (u == v) return u; for (int k = log2_n - 1; k >= 0; k--) { if (parent[k][u] != parent[k][v]) { u = parent[k][u]; v = parent[k][v]; } } return parent[0][u]; } }; void visit(const Graph &g, int v, Matrix &scc, stack<ll> &S, Array &inS, Array &low, Array &num, int &time) { low[v] = num[v] = ++time; S.push(v); inS[v] = true; FOR(e, g[v]) { int w = e->to; if (num[w] == 0) { visit(g, w, scc, S, inS, low, num, time); low[v] = min(low[v], low[w]); } else if (inS[w]) low[v] = min(low[v], num[w]); } if (low[v] == num[v]) { scc.push_back(Array()); while (1) { int w = S.top(); S.pop(); inS[w] = false; scc.back().push_back(w); if (v == w) break; } } } void stronglyConnectedComponents(const Graph &g, Matrix &scc) { // 強連結成分分解 O(E+V) const int n = g.size(); Array num(n), low(n); stack<ll> S; Array inS(n); int time = 0; REP(u, n) if (num[u] == 0) visit(g, u, scc, S, inS, low, num, time); } class UnionFind { vector<int> data; ll num; public: UnionFind(int size) : data(size, -1), num(size) {} bool unite(int x, int y) { // xとyの集合を統合する x = root(x); y = root(y); if (x != y) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } num -= (x != y); return x != y; } bool findSet(int x, int y) { // xとyが同じ集合か返す return root(x) == root(y); } int root(int x) { // xのルートを返す return data[x] < 0 ? x : data[x] = root(data[x]); } int size(int x) { // xの集合のサイズを返す return -data[root(x)]; } int numSet() { // 集合の数を返す return num; } }; class SumSegTree { private: ll _sum(ll a, ll b, ll k, ll l, ll r) { if (r <= a || b <= l) return 0; // 交差しない if (a <= l && r <= b) return dat[k]; // a,l,r,bの順で完全に含まれる else { ll s1 = _sum(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子 ll s2 = _sum(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子 return s1 + s2; } } public: ll n, height; vector<ll> dat; // 初期化(_nは最大要素数) SumSegTree(ll _n) { n = 1; height = 1; while (n < _n) { n *= 2; height++; } dat = vector<ll>(2 * n - 1, 0); } // 場所i(0-indexed)にxを足す void add(ll i, ll x) { i += n - 1; // i番目の葉ノードへ dat[i] += x; while (i > 0) { // 下から上がっていく i = (i - 1) / 2; dat[i] += x; } } // 区間[a,b)の総和。ノードk=[l,r)に着目している。 ll sum(ll a, ll b) { return _sum(a, b, 0, 0, n); } }; class RmqTree { private: ll _find(ll a, ll b, ll k, ll l, ll r) { if (r <= a || b <= l) return INF; // 交差しない if (a <= l && r <= b) return dat[k]; // a,l,r,bの順で完全に含まれる else { ll s1 = _find(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子 ll s2 = _find(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子 return min(s1, s2); } } public: ll n, height; vector<ll> dat; // 初期化(_nは最大要素数) RmqTree(ll _n) { n = 1; height = 1; while (n < _n) { n *= 2; height++; } dat = vector<ll>(2 * n - 1, INF); } // 場所i(0-indexed)をxにする void update(ll i, ll x) { i += n - 1; // i番目の葉ノードへ dat[i] = x; while (i > 0) { // 下から上がっていく i = (i - 1) / 2; dat[i] = min(dat[i * 2 + 1], dat[i * 2 + 2]); } } // 区間[a,b)の最小値。ノードk=[l,r)に着目している。 ll find(ll a, ll b) { return _find(a, b, 0, 0, n); } }; // 約数求める //約数 void divisor(ll n, vector<ll> &ret) { for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(ret.begin(), ret.end()); } vector<ll> lis_fast(const vector<ll> &a) { // 最長部分増加列 const ll n = a.size(); vector<ll> A(n, INT_MAX); vector<ll> id(n); for (int i = 0; i < n; ++i) { id[i] = distance(A.begin(), lower_bound(A.begin(), A.end(), a[i])); A[id[i]] = a[i]; } ll m = *max_element(id.begin(), id.end()); vector<ll> b(m + 1); for (int i = n - 1; i >= 0; --i) if (id[i] == m) b[m--] = a[i]; return b; } bool z_algorithm(string &str, vector<int> &z, ll s) { // s&tを渡してtにsが含まれるかを返す const int L = str.size(); z.resize(str.size()); for (int i = 1, left = 0, right = 0; i < L; i++) { if (i > right) { left = right = i; for (; right < L && str[right - left] == str[right]; right++) ; z[i] = right - left; right--; } else { int k = i - left; if (z[k] < right - i + 1) { z[i] = z[k]; } else { left = i; for (; right < L && str[right - left] == str[right]; right++) ; z[i] = right - left; right--; } } if (z[i] == s) return true; } return false; } bool z_algorithm(string &str, vector<int> &z) { // z[i]==|s|のときstr[i]からsが含まれる const int L = str.size(); z.resize(str.size()); for (int i = 1, left = 0, right = 0; i < L; i++) { if (i > right) { left = right = i; for (; right < L && str[right - left] == str[right]; right++) ; z[i] = right - left; right--; } else { int k = i - left; if (z[k] < right - i + 1) { z[i] = z[k]; } else { left = i; for (; right < L && str[right - left] == str[right]; right++) ; z[i] = right - left; right--; } } } return true; } // ローリングハッシュ // 二分探索で LCP を求める機能つき struct RollingHash { static const int base1 = 1007, base2 = 2009; static const int mod1 = 1000000007, mod2 = 1000000009; vector<long long> hash1, hash2, power1, power2; // construct RollingHash(const string &S) { int n = (int)S.size(); hash1.assign(n + 1, 0); hash2.assign(n + 1, 0); power1.assign(n + 1, 1); power2.assign(n + 1, 1); for (int i = 0; i < n; ++i) { hash1[i + 1] = (hash1[i] * base1 + S[i]) % mod1; hash2[i + 1] = (hash2[i] * base2 + S[i]) % mod2; power1[i + 1] = (power1[i] * base1) % mod1; power2[i + 1] = (power2[i] * base2) % mod2; } } // get hash of S[left:right] inline pair<long long, long long> get(int l, int r) const { long long res1 = hash1[r] - hash1[l] * power1[r - l] % mod1; if (res1 < 0) res1 += mod1; long long res2 = hash2[r] - hash2[l] * power2[r - l] % mod2; if (res2 < 0) res2 += mod2; return {res1, res2}; } // get lcp of S[a:] and T[b:] inline int getLCP(int a, int b) const { int len = min((int)hash1.size() - a, (int)hash1.size() - b); int low = 0, high = len; while (high - low > 1) { int mid = (low + high) >> 1; if (get(a, a + mid) != get(b, b + mid)) high = mid; else low = mid; } return low; } }; ll mod_pow(ll x, ll n, ll mod) { ll res = 1LL; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } ll mod_inv(ll x, ll mod) { return mod_pow(x, mod - 2, mod); } // nCrとか class Combination { public: Array fact; Array inv; ll mod; ll mod_inv(ll x) { ll n = mod - 2LL; ll res = 1LL; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } ll nCr(ll n, ll r) { return ((fact[n] * inv[r] % mod) * inv[n - r]) % mod; } ll nPr(ll n, ll r) { return (fact[n] * inv[n - r]) % mod; } ll nHr(ll n, ll r) { return nCr(r + n - 1, r); } Combination(ll n, ll _mod) { mod = _mod; fact.resize(n + 1); fact[0] = 1; REP(i, n) { fact[i + 1] = (fact[i] * (i + 1LL)) % mod; } inv.resize(n + 1); inv[n] = mod_inv(fact[n]); for (int i = n; i > 0; i--) { inv[i - 1] = inv[i] * i % mod; } } }; ll gcd(ll m, ll n) { if (n == 0) return m; return gcd(n, m % n); } // gcd ll lcm(ll m, ll n) { return m / gcd(m, n) * n; } Matrix mIdentity(ll n) { Matrix A(n, Array(n)); for (int i = 0; i < n; ++i) A[i][i] = 1; return A; } Matrix mMul(const Matrix &A, const Matrix &B) { Matrix C(A.size(), Array(B[0].size())); for (int i = 0; i < C.size(); ++i) for (int j = 0; j < C[i].size(); ++j) for (int k = 0; k < A[i].size(); ++k) (C[i][j] += (A[i][k] % MOD) * (B[k][j] % MOD)) %= MOD; return C; } // O( n^3 log e ) Matrix mPow(const Matrix &A, ll e) { return e == 0 ? mIdentity(A.size()) : e % 2 == 0 ? mPow(mMul(A, A), e / 2) : mMul(A, mPow(A, e - 1)); } template <class T> class RectangleSum { public: vector<vector<T>> sum; T GetSum(int left, int right, int top, int bottom) { //[left, right], [top, bottom] T res = sum[bottom][right]; if (left > 0) res -= sum[bottom][left - 1]; if (top > 0) res -= sum[top - 1][right]; if (left > 0 && top > 0) res += sum[top - 1][left - 1]; return res; } RectangleSum(const vector<vector<T>> &s, int h, int w) { sum.resize(h); for (int i = 0; i < h; i++) sum[i].resize(w, 0); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { sum[y][x] = s[y][x]; if (y > 0) sum[y][x] += sum[y - 1][x]; if (x > 0) sum[y][x] += sum[y][x - 1]; if (y > 0 && x > 0) sum[y][x] -= sum[y - 1][x - 1]; } } } }; // NTT ll _garner(Array &xs, Array &mods) { int M = xs.size(); Array coeffs(M, 1), constants(M, 0); for (int i = 0; i < M - 1; ++i) { ll mod_i = mods[i]; // coffs[i] * v + constants[i] == mr[i].val (mod mr[i].first) を解く ll v = (xs[i] - constants[i] + mod_i) % mod_i; v = (v * mod_pow(coeffs[i], mod_i - 2, mod_i)) % mod_i; for (int j = i + 1; j < M; j++) { ll mod_j = mods[j]; constants[j] = (constants[j] + coeffs[j] * v) % mod_j; coeffs[j] = (coeffs[j] * mod_i) % mod_j; } } return constants.back(); } template <typename T> inline void bit_reverse(vector<T> &a) { int n = a.size(); int i = 0; for (int j = 1; j < n - 1; ++j) { for (int k = n >> 1; k > (i ^= k); k >>= 1) ; if (j < i) swap(a[i], a[j]); } } template <long long mod, long long primitive_root> class NTT { public: long long get_mod() { return mod; } void _ntt(vector<long long> &a, int sign) { const int n = a.size(); assert((n ^ (n & -n)) == 0); // n = 2^k const long long g = primitive_root; // g is primitive root of mod long long tmp = (mod - 1) * mod_pow(n, mod - 2, mod) % mod; // -1/n long long h = mod_pow(g, tmp, mod); // ^n√g if (sign == -1) h = mod_pow(h, mod - 2, mod); bit_reverse(a); for (int m = 1; m < n; m <<= 1) { const int m2 = 2 * m; long long _base = mod_pow(h, n / m2, mod); long long _w = 1; for (int x = 0; x < m; ++x) { for (int s = x; s < n; s += m2) { long long u = a[s]; long long d = (a[s + m] * _w) % mod; a[s] = (u + d) % mod; a[s + m] = (u - d + mod) % mod; } _w = (_w * _base) % mod; } } } void ntt(vector<long long> &input) { _ntt(input, 1); } void intt(vector<long long> &input) { _ntt(input, -1); const long long n_inv = mod_pow(input.size(), mod - 2, mod); for (auto &x : input) x = (x * n_inv) % mod; } // 畳み込み演算を行う vector<long long> convolution(const vector<long long> &a, const vector<long long> &b) { int result_size = a.size() + b.size() - 1; int n = 1; while (n < result_size) n <<= 1; vector<long long> _a = a, _b = b; _a.resize(n, 0); _b.resize(n, 0); ntt(_a); ntt(_b); for (int i = 0; i < n; ++i) _a[i] = (_a[i] * _b[i]) % mod; intt(_a); _a.resize(result_size); return _a; } }; vector<long long> convolution_ntt(vector<long long> &a, vector<long long> &b, long long mod = 1224736769LL) { for (auto &x : a) x %= mod; for (auto &x : b) x %= mod; ll maxval = max(a.size(), b.size()) * *max_element(a.begin(), a.end()) * *max_element(b.begin(), b.end()); if (maxval < 1224736769) { NTT<1224736769, 3> ntt3; return ntt3.convolution(a, b); } NTT<167772161, 3> ntt1; NTT<469762049, 3> ntt2; NTT<1224736769, 3> ntt3; vector<long long> x1 = ntt1.convolution(a, b); vector<long long> x2 = ntt2.convolution(a, b); vector<long long> x3 = ntt3.convolution(a, b); vector<long long> ret(x1.size()); vector<long long> mods{167772161, 469762049, 1224736769, mod}; for (int i = 0; i < x1.size(); ++i) { vector<long long> xs{x1[i], x2[i], x3[i], 0}; ret[i] = _garner(xs, mods); } return ret; } int popcount3(int x) { x = (x & 0x55555555) + (x >> 1 & 0x55555555); x = (x & 0x33333333) + (x >> 2 & 0x33333333); x = (x & 0x0F0F0F0F) + (x >> 4 & 0x0F0F0F0F); x = (x & 0x00FF00FF) + (x >> 8 & 0x00FF00FF); x = (x & 0x0000FFFF) + (x >> 16 & 0x0000FFFF); return x; } ll dp[610][610]; ll n, k; Array h; ll find(ll x, ll y) { if (y > x + 1) return INF; if (y == 1) return h[x]; if (dp[x][y] != 0) return dp[x][y]; ll ret = INF; REP(i, x) { chmin(ret, find(i, y - 1) + max(0LL, h[x] - h[i])); } return dp[x][y] = ret; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> k; h.resize(n); REP(i, n) cin >> h[i]; ll ans = INF; REP(i, n) chmin(ans, find(i, n - k)); cout << ans << endl; return 0; }
#define _USE_MATH_DEFINES #include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <vector> using namespace std; #define MOD 1000000007 #define rep(i, m, n) for (int(i) = (int)(m); i < (int)(n); i++) #define REP(i, n) rep(i, 0, n) #define FOR(i, c) \ for (decltype((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ll long long #define ull unsigned long long #define all(hoge) (hoge).begin(), (hoge).end() typedef pair<ll, ll> P; const long long INF = 1LL << 60; typedef vector<ll> Array; typedef vector<Array> Matrix; string operator*(const string &s, int k) { if (k == 0) return ""; string p = (s + s) * (k / 2); if (k % 2 == 1) p += s; return p; } 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; } struct Edge { // グラフ ll to, cap, rev; Edge(ll _to, ll _cap, ll _rev) { to = _to; cap = _cap; rev = _rev; } }; typedef vector<Edge> Edges; typedef vector<Edges> Graph; void add_edge(Graph &G, ll from, ll to, ll cap, bool revFlag, ll revCap) { // 最大フロー求める Ford-fulkerson G[from].push_back(Edge(to, cap, (ll)G[to].size())); if (revFlag) G[to].push_back(Edge( from, revCap, (ll)G[from].size() - 1)); // 最小カットの場合逆辺は0にする } ll max_flow_dfs(Graph &G, ll v, ll t, ll f, vector<bool> &used) { if (v == t) return f; used[v] = true; for (int i = 0; i < G[v].size(); ++i) { Edge &e = G[v][i]; if (!used[e.to] && e.cap > 0) { ll d = max_flow_dfs(G, e.to, t, min(f, e.cap), used); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } // 二分グラフの最大マッチングを求めたりも出来る また二部グラフの最大独立集合は頂点数-最大マッチングのサイズ ll max_flow(Graph &G, ll s, ll t) // O(V(V+E)) { ll flow = 0; for (;;) { vector<bool> used(G.size()); REP(i, used.size()) used[i] = false; ll f = max_flow_dfs(G, s, t, INF, used); if (f == 0) { return flow; } flow += f; } } void BellmanFord(Graph &G, ll s, Array &d, Array &negative) { // O(|E||V|) d.resize(G.size()); negative.resize(G.size()); REP(i, d.size()) d[i] = INF; REP(i, d.size()) negative[i] = false; d[s] = 0; REP(k, G.size() - 1) { REP(i, G.size()) { REP(j, G[i].size()) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; } } } } REP(k, G.size() - 1) { REP(i, G.size()) { REP(j, G[i].size()) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; negative[G[i][j].to] = true; } if (negative[i] == true) negative[G[i][j].to] = true; } } } } void Dijkstra(Graph &G, ll s, Array &d) { // O(|E|log|V|) d.resize(G.size()); REP(i, d.size()) d[i] = INF; d[s] = 0; priority_queue<P, vector<P>, greater<P>> q; q.push(make_pair(0, s)); while (!q.empty()) { P a = q.top(); q.pop(); if (d[a.second] < a.first) continue; REP(i, G[a.second].size()) { Edge e = G[a.second][i]; if (d[e.to] > d[a.second] + e.cap) { d[e.to] = d[a.second] + e.cap; q.push(make_pair(d[e.to], e.to)); } } } } void WarshallFloyd(Graph &G, Matrix &d) { // O(V^3) d.resize(G.size()); REP(i, d.size()) d[i].resize(G.size()); REP(i, d.size()) { REP(j, d[i].size()) { d[i][j] = INF; } } REP(i, G.size()) { REP(j, G[i].size()) { d[i][G[i][j].to] = G[i][j].cap; } } REP(i, G.size()) { REP(j, G.size()) { REP(k, G.size()) { chmin(d[j][k], d[j][i] + d[i][k]); } } } } bool tsort(Graph &graph, vector<int> &order) { // トポロジカルソートO(E+V) int n = graph.size(), k = 0; Array in(n); for (auto &es : graph) for (auto &e : es) in[e.to]++; priority_queue<ll, Array, greater<ll>> que; REP(i, n) if (in[i] == 0) que.push(i); while (que.size()) { int v = que.top(); que.pop(); order.push_back(v); for (auto &e : graph[v]) if (--in[e.to] == 0) que.push(e.to); } if (order.size() != n) return false; else return true; } class lca { public: const int n = 0; const int log2_n = 0; std::vector<std::vector<int>> parent; std::vector<int> depth; lca() {} lca(const Graph &g, int root) : n(g.size()), log2_n(log2(n) + 1), parent(log2_n, std::vector<int>(n)), depth(n) { dfs(g, root, -1, 0); for (int k = 0; k + 1 < log2_n; k++) { for (int v = 0; v < (int)g.size(); v++) { if (parent[k][v] < 0) parent[k + 1][v] = -1; else parent[k + 1][v] = parent[k][parent[k][v]]; } } } void dfs(const Graph &g, int v, int p, int d) { parent[0][v] = p; depth[v] = d; for (auto &e : g[v]) { if (e.to != p) dfs(g, e.to, v, d + 1); } } int get(int u, int v) { if (depth[u] > depth[v]) std::swap(u, v); for (int k = 0; k < log2_n; k++) { if ((depth[v] - depth[u]) >> k & 1) { v = parent[k][v]; } } if (u == v) return u; for (int k = log2_n - 1; k >= 0; k--) { if (parent[k][u] != parent[k][v]) { u = parent[k][u]; v = parent[k][v]; } } return parent[0][u]; } }; void visit(const Graph &g, int v, Matrix &scc, stack<ll> &S, Array &inS, Array &low, Array &num, int &time) { low[v] = num[v] = ++time; S.push(v); inS[v] = true; FOR(e, g[v]) { int w = e->to; if (num[w] == 0) { visit(g, w, scc, S, inS, low, num, time); low[v] = min(low[v], low[w]); } else if (inS[w]) low[v] = min(low[v], num[w]); } if (low[v] == num[v]) { scc.push_back(Array()); while (1) { int w = S.top(); S.pop(); inS[w] = false; scc.back().push_back(w); if (v == w) break; } } } void stronglyConnectedComponents(const Graph &g, Matrix &scc) { // 強連結成分分解 O(E+V) const int n = g.size(); Array num(n), low(n); stack<ll> S; Array inS(n); int time = 0; REP(u, n) if (num[u] == 0) visit(g, u, scc, S, inS, low, num, time); } class UnionFind { vector<int> data; ll num; public: UnionFind(int size) : data(size, -1), num(size) {} bool unite(int x, int y) { // xとyの集合を統合する x = root(x); y = root(y); if (x != y) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } num -= (x != y); return x != y; } bool findSet(int x, int y) { // xとyが同じ集合か返す return root(x) == root(y); } int root(int x) { // xのルートを返す return data[x] < 0 ? x : data[x] = root(data[x]); } int size(int x) { // xの集合のサイズを返す return -data[root(x)]; } int numSet() { // 集合の数を返す return num; } }; class SumSegTree { private: ll _sum(ll a, ll b, ll k, ll l, ll r) { if (r <= a || b <= l) return 0; // 交差しない if (a <= l && r <= b) return dat[k]; // a,l,r,bの順で完全に含まれる else { ll s1 = _sum(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子 ll s2 = _sum(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子 return s1 + s2; } } public: ll n, height; vector<ll> dat; // 初期化(_nは最大要素数) SumSegTree(ll _n) { n = 1; height = 1; while (n < _n) { n *= 2; height++; } dat = vector<ll>(2 * n - 1, 0); } // 場所i(0-indexed)にxを足す void add(ll i, ll x) { i += n - 1; // i番目の葉ノードへ dat[i] += x; while (i > 0) { // 下から上がっていく i = (i - 1) / 2; dat[i] += x; } } // 区間[a,b)の総和。ノードk=[l,r)に着目している。 ll sum(ll a, ll b) { return _sum(a, b, 0, 0, n); } }; class RmqTree { private: ll _find(ll a, ll b, ll k, ll l, ll r) { if (r <= a || b <= l) return INF; // 交差しない if (a <= l && r <= b) return dat[k]; // a,l,r,bの順で完全に含まれる else { ll s1 = _find(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子 ll s2 = _find(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子 return min(s1, s2); } } public: ll n, height; vector<ll> dat; // 初期化(_nは最大要素数) RmqTree(ll _n) { n = 1; height = 1; while (n < _n) { n *= 2; height++; } dat = vector<ll>(2 * n - 1, INF); } // 場所i(0-indexed)をxにする void update(ll i, ll x) { i += n - 1; // i番目の葉ノードへ dat[i] = x; while (i > 0) { // 下から上がっていく i = (i - 1) / 2; dat[i] = min(dat[i * 2 + 1], dat[i * 2 + 2]); } } // 区間[a,b)の最小値。ノードk=[l,r)に着目している。 ll find(ll a, ll b) { return _find(a, b, 0, 0, n); } }; // 約数求める //約数 void divisor(ll n, vector<ll> &ret) { for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(ret.begin(), ret.end()); } vector<ll> lis_fast(const vector<ll> &a) { // 最長部分増加列 const ll n = a.size(); vector<ll> A(n, INT_MAX); vector<ll> id(n); for (int i = 0; i < n; ++i) { id[i] = distance(A.begin(), lower_bound(A.begin(), A.end(), a[i])); A[id[i]] = a[i]; } ll m = *max_element(id.begin(), id.end()); vector<ll> b(m + 1); for (int i = n - 1; i >= 0; --i) if (id[i] == m) b[m--] = a[i]; return b; } bool z_algorithm(string &str, vector<int> &z, ll s) { // s&tを渡してtにsが含まれるかを返す const int L = str.size(); z.resize(str.size()); for (int i = 1, left = 0, right = 0; i < L; i++) { if (i > right) { left = right = i; for (; right < L && str[right - left] == str[right]; right++) ; z[i] = right - left; right--; } else { int k = i - left; if (z[k] < right - i + 1) { z[i] = z[k]; } else { left = i; for (; right < L && str[right - left] == str[right]; right++) ; z[i] = right - left; right--; } } if (z[i] == s) return true; } return false; } bool z_algorithm(string &str, vector<int> &z) { // z[i]==|s|のときstr[i]からsが含まれる const int L = str.size(); z.resize(str.size()); for (int i = 1, left = 0, right = 0; i < L; i++) { if (i > right) { left = right = i; for (; right < L && str[right - left] == str[right]; right++) ; z[i] = right - left; right--; } else { int k = i - left; if (z[k] < right - i + 1) { z[i] = z[k]; } else { left = i; for (; right < L && str[right - left] == str[right]; right++) ; z[i] = right - left; right--; } } } return true; } // ローリングハッシュ // 二分探索で LCP を求める機能つき struct RollingHash { static const int base1 = 1007, base2 = 2009; static const int mod1 = 1000000007, mod2 = 1000000009; vector<long long> hash1, hash2, power1, power2; // construct RollingHash(const string &S) { int n = (int)S.size(); hash1.assign(n + 1, 0); hash2.assign(n + 1, 0); power1.assign(n + 1, 1); power2.assign(n + 1, 1); for (int i = 0; i < n; ++i) { hash1[i + 1] = (hash1[i] * base1 + S[i]) % mod1; hash2[i + 1] = (hash2[i] * base2 + S[i]) % mod2; power1[i + 1] = (power1[i] * base1) % mod1; power2[i + 1] = (power2[i] * base2) % mod2; } } // get hash of S[left:right] inline pair<long long, long long> get(int l, int r) const { long long res1 = hash1[r] - hash1[l] * power1[r - l] % mod1; if (res1 < 0) res1 += mod1; long long res2 = hash2[r] - hash2[l] * power2[r - l] % mod2; if (res2 < 0) res2 += mod2; return {res1, res2}; } // get lcp of S[a:] and T[b:] inline int getLCP(int a, int b) const { int len = min((int)hash1.size() - a, (int)hash1.size() - b); int low = 0, high = len; while (high - low > 1) { int mid = (low + high) >> 1; if (get(a, a + mid) != get(b, b + mid)) high = mid; else low = mid; } return low; } }; ll mod_pow(ll x, ll n, ll mod) { ll res = 1LL; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } ll mod_inv(ll x, ll mod) { return mod_pow(x, mod - 2, mod); } // nCrとか class Combination { public: Array fact; Array inv; ll mod; ll mod_inv(ll x) { ll n = mod - 2LL; ll res = 1LL; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } ll nCr(ll n, ll r) { return ((fact[n] * inv[r] % mod) * inv[n - r]) % mod; } ll nPr(ll n, ll r) { return (fact[n] * inv[n - r]) % mod; } ll nHr(ll n, ll r) { return nCr(r + n - 1, r); } Combination(ll n, ll _mod) { mod = _mod; fact.resize(n + 1); fact[0] = 1; REP(i, n) { fact[i + 1] = (fact[i] * (i + 1LL)) % mod; } inv.resize(n + 1); inv[n] = mod_inv(fact[n]); for (int i = n; i > 0; i--) { inv[i - 1] = inv[i] * i % mod; } } }; ll gcd(ll m, ll n) { if (n == 0) return m; return gcd(n, m % n); } // gcd ll lcm(ll m, ll n) { return m / gcd(m, n) * n; } Matrix mIdentity(ll n) { Matrix A(n, Array(n)); for (int i = 0; i < n; ++i) A[i][i] = 1; return A; } Matrix mMul(const Matrix &A, const Matrix &B) { Matrix C(A.size(), Array(B[0].size())); for (int i = 0; i < C.size(); ++i) for (int j = 0; j < C[i].size(); ++j) for (int k = 0; k < A[i].size(); ++k) (C[i][j] += (A[i][k] % MOD) * (B[k][j] % MOD)) %= MOD; return C; } // O( n^3 log e ) Matrix mPow(const Matrix &A, ll e) { return e == 0 ? mIdentity(A.size()) : e % 2 == 0 ? mPow(mMul(A, A), e / 2) : mMul(A, mPow(A, e - 1)); } template <class T> class RectangleSum { public: vector<vector<T>> sum; T GetSum(int left, int right, int top, int bottom) { //[left, right], [top, bottom] T res = sum[bottom][right]; if (left > 0) res -= sum[bottom][left - 1]; if (top > 0) res -= sum[top - 1][right]; if (left > 0 && top > 0) res += sum[top - 1][left - 1]; return res; } RectangleSum(const vector<vector<T>> &s, int h, int w) { sum.resize(h); for (int i = 0; i < h; i++) sum[i].resize(w, 0); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { sum[y][x] = s[y][x]; if (y > 0) sum[y][x] += sum[y - 1][x]; if (x > 0) sum[y][x] += sum[y][x - 1]; if (y > 0 && x > 0) sum[y][x] -= sum[y - 1][x - 1]; } } } }; // NTT ll _garner(Array &xs, Array &mods) { int M = xs.size(); Array coeffs(M, 1), constants(M, 0); for (int i = 0; i < M - 1; ++i) { ll mod_i = mods[i]; // coffs[i] * v + constants[i] == mr[i].val (mod mr[i].first) を解く ll v = (xs[i] - constants[i] + mod_i) % mod_i; v = (v * mod_pow(coeffs[i], mod_i - 2, mod_i)) % mod_i; for (int j = i + 1; j < M; j++) { ll mod_j = mods[j]; constants[j] = (constants[j] + coeffs[j] * v) % mod_j; coeffs[j] = (coeffs[j] * mod_i) % mod_j; } } return constants.back(); } template <typename T> inline void bit_reverse(vector<T> &a) { int n = a.size(); int i = 0; for (int j = 1; j < n - 1; ++j) { for (int k = n >> 1; k > (i ^= k); k >>= 1) ; if (j < i) swap(a[i], a[j]); } } template <long long mod, long long primitive_root> class NTT { public: long long get_mod() { return mod; } void _ntt(vector<long long> &a, int sign) { const int n = a.size(); assert((n ^ (n & -n)) == 0); // n = 2^k const long long g = primitive_root; // g is primitive root of mod long long tmp = (mod - 1) * mod_pow(n, mod - 2, mod) % mod; // -1/n long long h = mod_pow(g, tmp, mod); // ^n√g if (sign == -1) h = mod_pow(h, mod - 2, mod); bit_reverse(a); for (int m = 1; m < n; m <<= 1) { const int m2 = 2 * m; long long _base = mod_pow(h, n / m2, mod); long long _w = 1; for (int x = 0; x < m; ++x) { for (int s = x; s < n; s += m2) { long long u = a[s]; long long d = (a[s + m] * _w) % mod; a[s] = (u + d) % mod; a[s + m] = (u - d + mod) % mod; } _w = (_w * _base) % mod; } } } void ntt(vector<long long> &input) { _ntt(input, 1); } void intt(vector<long long> &input) { _ntt(input, -1); const long long n_inv = mod_pow(input.size(), mod - 2, mod); for (auto &x : input) x = (x * n_inv) % mod; } // 畳み込み演算を行う vector<long long> convolution(const vector<long long> &a, const vector<long long> &b) { int result_size = a.size() + b.size() - 1; int n = 1; while (n < result_size) n <<= 1; vector<long long> _a = a, _b = b; _a.resize(n, 0); _b.resize(n, 0); ntt(_a); ntt(_b); for (int i = 0; i < n; ++i) _a[i] = (_a[i] * _b[i]) % mod; intt(_a); _a.resize(result_size); return _a; } }; vector<long long> convolution_ntt(vector<long long> &a, vector<long long> &b, long long mod = 1224736769LL) { for (auto &x : a) x %= mod; for (auto &x : b) x %= mod; ll maxval = max(a.size(), b.size()) * *max_element(a.begin(), a.end()) * *max_element(b.begin(), b.end()); if (maxval < 1224736769) { NTT<1224736769, 3> ntt3; return ntt3.convolution(a, b); } NTT<167772161, 3> ntt1; NTT<469762049, 3> ntt2; NTT<1224736769, 3> ntt3; vector<long long> x1 = ntt1.convolution(a, b); vector<long long> x2 = ntt2.convolution(a, b); vector<long long> x3 = ntt3.convolution(a, b); vector<long long> ret(x1.size()); vector<long long> mods{167772161, 469762049, 1224736769, mod}; for (int i = 0; i < x1.size(); ++i) { vector<long long> xs{x1[i], x2[i], x3[i], 0}; ret[i] = _garner(xs, mods); } return ret; } int popcount3(int x) { x = (x & 0x55555555) + (x >> 1 & 0x55555555); x = (x & 0x33333333) + (x >> 2 & 0x33333333); x = (x & 0x0F0F0F0F) + (x >> 4 & 0x0F0F0F0F); x = (x & 0x00FF00FF) + (x >> 8 & 0x00FF00FF); x = (x & 0x0000FFFF) + (x >> 16 & 0x0000FFFF); return x; } ll dp[610][610]; ll n, k; Array h; ll find(ll x, ll y) { if (y == 0) return 0; if (y > x + 1) return INF; if (y == 1) return h[x]; if (dp[x][y] != 0) return dp[x][y]; ll ret = INF; REP(i, x) { chmin(ret, find(i, y - 1) + max(0LL, h[x] - h[i])); } return dp[x][y] = ret; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> k; h.resize(n); REP(i, n) cin >> h[i]; ll ans = INF; REP(i, n) chmin(ans, find(i, n - k)); cout << ans << endl; return 0; }
replace
771
772
771
773
0
p02864
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; long long D[301][301], H[302]; int main() { int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> H[i]; } H[n + 1] = 0; for (int i = 0; i <= k; i++) { D[0][i] = 0; } for (int i = 1; i <= n + 1; i++) { D[i][0] = D[i - 1][0] + max(0ll, H[i] - H[i - 1]); for (int y = 1; y <= k; y++) { D[i][y] = D[i - 1][y] + max(0ll, H[i] - H[i - 1]); for (int u = 1; u <= y; u++) { if (i - u >= 0) { D[i][y] = min(D[i][y], D[i - u - 1][y - u] + max(0ll, H[i] - H[i - u - 1])); } } } } cout << D[n + 1][k]; return 0; }
#include <bits/stdc++.h> using namespace std; long long D[302][301], H[302]; int main() { int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> H[i]; } H[n + 1] = 0; for (int i = 0; i <= k; i++) { D[0][i] = 0; } for (int i = 1; i <= n + 1; i++) { D[i][0] = D[i - 1][0] + max(0ll, H[i] - H[i - 1]); for (int y = 1; y <= k; y++) { D[i][y] = D[i - 1][y] + max(0ll, H[i] - H[i - 1]); for (int u = 1; u <= y; u++) { if (i - u >= 0) { D[i][y] = min(D[i][y], D[i - u - 1][y - u] + max(0ll, H[i] - H[i - u - 1])); } } } } cout << D[n + 1][k]; return 0; }
replace
4
5
4
5
0