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
|
---|---|---|---|---|---|---|---|---|---|---|---|
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
#define maxn 100
using namespace std;
long long num[maxn];
long long dp[maxn][maxn];
int n;
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> num[i];
for (int i = n; i >= 1; --i) {
for (int j = i; j <= n; ++j) {
dp[i][j] = max(num[i] - dp[i + 1][j], num[j] - dp[i][j - 1]);
}
}
cout << dp[1][n];
return 0;
} | #include <bits/stdc++.h>
#define maxn 3005
using namespace std;
long long num[maxn];
long long dp[maxn][maxn];
int n;
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> num[i];
for (int i = n; i >= 1; --i) {
for (int j = i; j <= n; ++j) {
dp[i][j] = max(num[i] - dp[i + 1][j], num[j] - dp[i][j - 1]);
}
}
cout << dp[1][n];
return 0;
} | replace | 1 | 2 | 1 | 2 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ull unsigned long long int
#define Endl endl
#define mod 1000000007
#define mkp make_pair
#define pb push_back
#define cin(n) scanf("%d", &n)
#define cout(n) printf("%d ", n)
#define sf(n) scanf("%d", &n)
#define pf(n) printf("%d ", n)
#define bitget(n) __builtin_popcountll(n)
#define maxi 500111
ll dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
ll dy[] = {0, -1, -1, -1, 0, 1, 1, 1};
ll dxx[] = {1, -1, 0, 0};
ll dyy[] = {0, 0, -1, 1};
ll modexpo(ll x, ll y) {
if (y == 0)
return 1;
if (y % 2) {
ll viky = modexpo(x, y / 2);
return (((x * viky) % mod) * viky) % mod;
} else {
ll viky = modexpo(x, y / 2);
return (viky * viky) % mod;
}
}
ll intpoint(ll x1, ll y1, ll x2, ll y2) {
return (__gcd(abs(x1 - x2), abs(y1 - y2)) + 1);
}
bool isvalid(ll x, ll y, ll n, ll m) {
return (x >= 0 && x < n && y >= 0 && y < m);
}
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// int pp = uniform_int_distribution<int>(0, kk-1)(rng);
// ---------------------///////////////---------------------------------
// ---------------------///////////////----------------------------------
// -------------/////////////////---------------------------------///////
struct gg {
ll first;
ll second;
};
gg dp[3011][3011];
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin >> n;
ll ap[n + 11];
for (ll i = 0; i < n; i++) {
cin >> ap[i];
}
for (ll i = 0; i < n; i++) {
dp[i][i].first = ap[i];
dp[i][i].second = 0;
}
for (ll i = 1; i < n; i++) {
for (ll j = i - 1; j >= 0; j--) {
ll sum = dp[j][i - 1].first + dp[j][i - 1].second + ap[i];
dp[j][i].first =
max(dp[j][i - 1].second + ap[i], dp[j + 1][i].second + ap[j]);
dp[j][i].second = sum - dp[j][i].first;
}
}
for (ll i = 0; i < n; i++) {
for (ll j = 0; j < n; j++) {
// cout<<dp[i][j].first<<" "<<dp[i][j].second<<" ";
}
// cout<<"\n";
}
cout << (dp[0][n - 1].first - dp[0][n - 1].second);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ull unsigned long long int
#define Endl endl
#define mod 1000000007
#define mkp make_pair
#define pb push_back
#define cin(n) scanf("%d", &n)
#define cout(n) printf("%d ", n)
#define sf(n) scanf("%d", &n)
#define pf(n) printf("%d ", n)
#define bitget(n) __builtin_popcountll(n)
#define maxi 500111
ll dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
ll dy[] = {0, -1, -1, -1, 0, 1, 1, 1};
ll dxx[] = {1, -1, 0, 0};
ll dyy[] = {0, 0, -1, 1};
ll modexpo(ll x, ll y) {
if (y == 0)
return 1;
if (y % 2) {
ll viky = modexpo(x, y / 2);
return (((x * viky) % mod) * viky) % mod;
} else {
ll viky = modexpo(x, y / 2);
return (viky * viky) % mod;
}
}
ll intpoint(ll x1, ll y1, ll x2, ll y2) {
return (__gcd(abs(x1 - x2), abs(y1 - y2)) + 1);
}
bool isvalid(ll x, ll y, ll n, ll m) {
return (x >= 0 && x < n && y >= 0 && y < m);
}
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// int pp = uniform_int_distribution<int>(0, kk-1)(rng);
// ---------------------///////////////---------------------------------
// ---------------------///////////////----------------------------------
// -------------/////////////////---------------------------------///////
struct gg {
ll first;
ll second;
};
gg dp[3011][3011];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin >> n;
ll ap[n + 11];
for (ll i = 0; i < n; i++) {
cin >> ap[i];
}
for (ll i = 0; i < n; i++) {
dp[i][i].first = ap[i];
dp[i][i].second = 0;
}
for (ll i = 1; i < n; i++) {
for (ll j = i - 1; j >= 0; j--) {
ll sum = dp[j][i - 1].first + dp[j][i - 1].second + ap[i];
dp[j][i].first =
max(dp[j][i - 1].second + ap[i], dp[j + 1][i].second + ap[j]);
dp[j][i].second = sum - dp[j][i].first;
}
}
for (ll i = 0; i < n; i++) {
for (ll j = 0; j < n; j++) {
// cout<<dp[i][j].first<<" "<<dp[i][j].second<<" ";
}
// cout<<"\n";
}
cout << (dp[0][n - 1].first - dp[0][n - 1].second);
return 0;
} | replace | 50 | 54 | 50 | 51 | -11 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
#define FASTIO
#define endl \
"\n" // Since endl forces a buffer flush which slows down large I/O
// operations.
#define ff first
#define ss second
#define pb push_back
#define input(c, n) \
for (int i = 0; i < (n); i++) \
cin >> (c)[i];
#define exit_message(s) return 0 * printf(s)
#define show(c) \
for (auto i : (c)) \
cout << i << " "; \
cout << "\n";
#define display(c, n) \
for (int i = 0; i < (n); i++) \
cout << (c)[i] << " "; \
cout << "\n";
#define forl(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define dugbe(k) cout << "-\t> " << #k << " = " << k << "\n";
/* TIPS FOR DEBUGGING
* Check if values are not overflowing (use long long where required)
* Check if ranges are inclusive or not.
* Check properly if greedy will work or not before using it.
* Check EDGE CASES!! Seriously, even in 2B, saving 8 points by being
* 2 minutes faster will bite you in the ass if you don't make sure
* it works on edge cases.
* Keep adding to this list.
*/
using namespace std;
typedef long long ll;
typedef pair<int, int> pr;
typedef pair<ll, ll> prll;
typedef vector<int> vi;
typedef vector<ll> vill;
typedef vector<pr> vpr;
ifstream in("input.txt");
const ll mod = 1e9 + 7;
ll mod_pow(ll a, ll b) {
if (b == 0)
return 1LL;
ll res = mod_pow(a, b >> 1);
res = (res * res) % mod;
if (b & 1LL)
res = (res * a) % mod;
return res;
}
const int N = 100 + 5;
const int SZ = 3000 + 5;
int n, k;
int a[N];
ll dp[N][N];
ll solve(int l, int r) {
if (l > r)
return 0;
ll &memo = dp[l][r];
if (memo != -1)
return memo;
memo = max(a[l] - solve(l + 1, r), a[r] - solve(l, r - 1));
return memo;
}
int32_t main() {
#ifdef FASTIO
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#endif
// freopen("in.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
memset(dp, -1, sizeof dp);
cin >> n;
forl(i, 0, n) { cin >> a[i]; }
ll ans = solve(0, n - 1);
cout << ans << "\n";
return 0;
} | #include <bits/stdc++.h>
#define FASTIO
#define endl \
"\n" // Since endl forces a buffer flush which slows down large I/O
// operations.
#define ff first
#define ss second
#define pb push_back
#define input(c, n) \
for (int i = 0; i < (n); i++) \
cin >> (c)[i];
#define exit_message(s) return 0 * printf(s)
#define show(c) \
for (auto i : (c)) \
cout << i << " "; \
cout << "\n";
#define display(c, n) \
for (int i = 0; i < (n); i++) \
cout << (c)[i] << " "; \
cout << "\n";
#define forl(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define dugbe(k) cout << "-\t> " << #k << " = " << k << "\n";
/* TIPS FOR DEBUGGING
* Check if values are not overflowing (use long long where required)
* Check if ranges are inclusive or not.
* Check properly if greedy will work or not before using it.
* Check EDGE CASES!! Seriously, even in 2B, saving 8 points by being
* 2 minutes faster will bite you in the ass if you don't make sure
* it works on edge cases.
* Keep adding to this list.
*/
using namespace std;
typedef long long ll;
typedef pair<int, int> pr;
typedef pair<ll, ll> prll;
typedef vector<int> vi;
typedef vector<ll> vill;
typedef vector<pr> vpr;
ifstream in("input.txt");
const ll mod = 1e9 + 7;
ll mod_pow(ll a, ll b) {
if (b == 0)
return 1LL;
ll res = mod_pow(a, b >> 1);
res = (res * res) % mod;
if (b & 1LL)
res = (res * a) % mod;
return res;
}
const int N = 3000 + 5;
int n, k;
int a[N];
ll dp[N][N];
ll solve(int l, int r) {
if (l > r)
return 0;
ll &memo = dp[l][r];
if (memo != -1)
return memo;
memo = max(a[l] - solve(l + 1, r), a[r] - solve(l, r - 1));
return memo;
}
int32_t main() {
#ifdef FASTIO
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#endif
// freopen("in.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
memset(dp, -1, sizeof dp);
cin >> n;
forl(i, 0, n) { cin >> a[i]; }
ll ans = solve(0, n - 1);
cout << ans << "\n";
return 0;
} | replace | 55 | 57 | 55 | 56 | 0 | |
p03171 | C++ | Runtime Error |
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll dp[3000][3000];
ll topdown(ll *arr, ll l, ll r) {
if (l > r) {
dp[l][r] = 0;
return 0;
}
if (dp[l][r] != -1) {
return dp[l][r];
}
ll op1 = arr[l] + min(topdown(arr, l + 1, r - 1), topdown(arr, l + 2, r));
ll op2 = arr[r] + min(topdown(arr, l, r - 2), topdown(arr, l + 1, r - 1));
dp[l][r] = max(op1, op2);
return dp[l][r];
}
int main() {
ll n = 0, sum = 0;
cin >> n;
ll *arr = new ll[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
sum += arr[i];
}
memset(dp, -1, sizeof dp);
cout << 2 * topdown(arr, 0, n - 1) - sum;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll dp[4000][4000];
ll topdown(ll *arr, ll l, ll r) {
if (l > r) {
dp[l][r] = 0;
return 0;
}
if (dp[l][r] != -1) {
return dp[l][r];
}
ll op1 = arr[l] + min(topdown(arr, l + 1, r - 1), topdown(arr, l + 2, r));
ll op2 = arr[r] + min(topdown(arr, l, r - 2), topdown(arr, l + 1, r - 1));
dp[l][r] = max(op1, op2);
return dp[l][r];
}
int main() {
ll n = 0, sum = 0;
cin >> n;
ll *arr = new ll[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
sum += arr[i];
}
memset(dp, -1, sizeof dp);
cout << 2 * topdown(arr, 0, n - 1) - sum;
return 0;
} | replace | 5 | 6 | 5 | 6 | 0 | |
p03171 | C++ | Runtime Error | /* Abhijith */
#include <bits/stdc++.h>
#include <cmath>
#include <iomanip>
#define lld long long int
#define ld long double
#define mod 1000000007
#define all(v) (v).begin(), (v).end()
#define rep(i, a, b) for (lld i = a; i <= b; i++)
#define repr(i, a, b) for (lld i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define endl '\n'
#define ios \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
using namespace std;
vector<lld> v(3001);
lld dp[3001][3001][2];
lld ans = 0, n;
lld rec(lld l, lld r, lld pla) {
if (dp[l][r][pla] != -1)
return dp[l][r][pla];
if (l > r) {
return dp[l][r][pla] = 0;
}
if (pla == 0) {
ans = max(v[l] + rec(l + 1, r, 1), v[r] + rec(l, r - 1, 1));
} else {
ans = min(rec(l + 1, r, 0) - v[l], rec(l, r - 1, 0) - v[r]);
}
return dp[l][r][pla] = ans;
}
int main() {
ios;
cin >> n;
rep(i, 1, n) { cin >> v[i]; }
rep(i, 0, 3000) rep(j, 0, 3000) rep(k, 0, 1) dp[i][j][k] = -1;
lld ans = rec(1, n, 0);
cout << ans << endl;
return 0;
}
| /* Abhijith */
#include <bits/stdc++.h>
#include <cmath>
#include <iomanip>
#define lld long long int
#define ld long double
#define mod 1000000007
#define all(v) (v).begin(), (v).end()
#define rep(i, a, b) for (lld i = a; i <= b; i++)
#define repr(i, a, b) for (lld i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define endl '\n'
#define ios \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
using namespace std;
vector<lld> v(3001);
lld dp[3001][3001][2];
lld ans = 0, n;
lld rec(lld l, lld r, lld pla) {
if (dp[l][r][pla] != -1)
return dp[l][r][pla];
if (l > r) {
return dp[l][r][pla] = 0;
}
if (pla == 0) {
ans = max(v[l] + rec(l + 1, r, 1), v[r] + rec(l, r - 1, 1));
} else {
ans = min(rec(l + 1, r, 0) - v[l], rec(l, r - 1, 0) - v[r]);
}
return dp[l][r][pla] = ans;
}
int main() {
ios;
cin >> n;
rep(i, 1, n) { cin >> v[i]; }
rep(l, 1, n) {
rep(i, 1, n - l + 1) {
lld j = i + l - 1;
if (l == 1) {
dp[i][j][0] = v[i];
dp[i][j][1] = -v[i];
} else {
dp[i][j][0] = max(dp[i + 1][j][1] + v[i], dp[i][j - 1][1] + v[j]);
dp[i][j][1] = min(dp[i + 1][j][0] - v[i], dp[i][j - 1][0] - v[j]);
}
}
}
// lld ans= rec(1,n,0);
cout << dp[1][n][0] << endl;
return 0;
}
| replace | 38 | 41 | 38 | 52 | -11 | |
p03171 | C++ | Time Limit Exceeded | #include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> a(N);
for (int &t : a)
cin >> t;
long long inv = 10000000000000LL;
vector<vector<long long>> T(N, vector<long long>(N, inv));
function<long long(int, int)> f = [&](int p, int q) -> long long {
if (p > q)
return 0;
if (T[p][q] != inv)
return T[p][q];
return max(a[p] - f(p + 1, q), a[q] - f(p, q - 1));
};
cout << f(0, N - 1) << endl;
}
| #include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> a(N);
for (int &t : a)
cin >> t;
long long inv = 10000000000000LL;
vector<vector<long long>> T(N, vector<long long>(N, inv));
function<long long(int, int)> f = [&](int p, int q) -> long long {
if (p > q)
return 0;
if (T[p][q] != inv)
return T[p][q];
return T[p][q] = max(a[p] - f(p + 1, q), a[q] - f(p, q - 1));
};
cout << f(0, N - 1) << endl;
}
| replace | 22 | 23 | 22 | 23 | TLE | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long
#define endl "\n"
#define vi vector<int>
#define vvi vector<vector<int>>
#define FASTIO \
ios_base::sync_with_stdio(NULL); \
cin.tie(NULL);
#define FOR(i, n) for (int i = 0; i < n; i++)
#define FORE(i, a, b) for (int i = a; i <= b; i++)
#define pb push_back
#define MOD 1000000007
using namespace std;
void init() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
// code from below
ll dp[3001][3001];
void solve() {
ll n;
cin >> n;
vector<ll> arr(n);
FOR(i, n)
cin >> arr[i];
// dp[i][j] stores the x-y value if both players play optimally, considering
// subarr from i to j of arr thus we have n^2 sub array thus dp is of n^2 size
for (int L = n - 1; L >= 0; L--) {
for (int R = L; R < n; R++) {
if (L == R)
dp[L][R] = arr[R];
else {
dp[L][R] = max(arr[L] - dp[L + 1][R], arr[R] - dp[L][R - 1]);
}
}
}
cout << dp[0][n - 1] << endl;
}
int main() {
int t = 1;
cin >> t;
// cin.ignore(numeric_limits<streamsize>::max(),'\n');
while (t--)
solve();
} | #include <bits/stdc++.h>
#define ll long long
#define endl "\n"
#define vi vector<int>
#define vvi vector<vector<int>>
#define FASTIO \
ios_base::sync_with_stdio(NULL); \
cin.tie(NULL);
#define FOR(i, n) for (int i = 0; i < n; i++)
#define FORE(i, a, b) for (int i = a; i <= b; i++)
#define pb push_back
#define MOD 1000000007
using namespace std;
void init() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
// code from below
ll dp[3001][3001];
void solve() {
ll n;
cin >> n;
vector<ll> arr(n);
FOR(i, n)
cin >> arr[i];
// dp[i][j] stores the x-y value if both players play optimally, considering
// subarr from i to j of arr thus we have n^2 sub array thus dp is of n^2 size
for (int L = n - 1; L >= 0; L--) {
for (int R = L; R < n; R++) {
if (L == R)
dp[L][R] = arr[R];
else {
dp[L][R] = max(arr[L] - dp[L + 1][R], arr[R] - dp[L][R - 1]);
}
}
}
cout << dp[0][n - 1] << endl;
}
int main() {
int t = 1;
// cin>>t;
// cin.ignore(numeric_limits<streamsize>::max(),'\n');
while (t--)
solve();
} | replace | 47 | 49 | 47 | 49 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0)
using namespace std;
const int maxi = 3005;
ll dp[maxi][maxi] = {0};
/**
dp[l][r] = max( arr[l] + dp[l-1][r] , arr[r] -dp[l][r-1] )
maximum of difference of if we choose left and the opponents choice or if we
choose right and opponents choice
dp[i][j] is storing the maximum difference for every j in arr.
so the answer will be dp[0][n-1]
**/
int main() {
fast;
int n;
cin >> n;
vector<int> arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
for (int l = n - 1; l >= 0; l--) {
for (int r = l; r < n; r++) {
if (l == r) {
dp[l][r] = arr[l];
} else {
dp[l][r] = max(arr[l] - dp[l - 1][r], arr[r] - dp[l][r - 1]);
}
}
}
cout << dp[0][n - 1];
}
| #include <bits/stdc++.h>
#define ll long long
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0)
using namespace std;
const int maxi = 3005;
ll dp[maxi][maxi] = {0};
/**
dp[l][r] = max( arr[l] + dp[l-1][r] , arr[r] -dp[l][r-1] )
maximum of difference of if we choose left and the opponents choice or if we
choose right and opponents choice
dp[i][j] is storing the maximum difference for every j in arr.
so the answer will be dp[0][n-1]
**/
int main() {
fast;
int n;
cin >> n;
vector<int> arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
for (int l = n - 1; l >= 0; l--) {
for (int r = l; r < n; r++) {
if (l == r) {
dp[l][r] = arr[l];
} else {
dp[l][r] = max(arr[l] - dp[l + 1][r], arr[r] - dp[l][r - 1]);
}
}
}
cout << dp[0][n - 1];
}
| replace | 37 | 38 | 37 | 38 | -11 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define n_l '\n'
template <typename T, size_t N> int SIZE(const T (&t)[N]) { return N; }
template <typename T> int SIZE(const T &t) { return t.size(); }
string to_string(const string s, int x1 = 0, int x2 = 1e9) {
return '"' + ((x1 < s.size()) ? s.substr(x1, x2 - x1 + 1) : "") + '"';
}
string to_string(const char *s) { return to_string((string)s); }
string to_string(const bool b) { return (b ? "true" : "false"); }
string to_string(const char c) { return string({c}); }
template <size_t N>
string to_string(const bitset<N> &b, int x1 = 0, int x2 = 1e9) {
string t = "";
for (int __iii__ = min(x1, SIZE(b)), __jjj__ = min(x2, SIZE(b) - 1);
__iii__ <= __jjj__; ++__iii__) {
t += b[__iii__] + '0';
}
return '"' + t + '"';
}
template <typename A, typename... C>
string to_string(const A(&v), int x1 = 0, int x2 = 1e9, C... coords);
int l_v_l_v_l = 0, t_a_b_s = 0;
template <typename A, typename B> string to_string(const pair<A, B> &p) {
l_v_l_v_l++;
string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
l_v_l_v_l--;
return res;
}
template <typename A, typename... C>
string to_string(const A(&v), int x1, int x2, C... coords) {
int rnk = rank<A>::value;
string tab(t_a_b_s, ' ');
string res = "";
bool first = true;
if (l_v_l_v_l == 0)
res += n_l;
res += tab + "[";
x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v));
auto l = begin(v);
advance(l, x1);
auto r = l;
advance(r, (x2 - x1) + (x2 < SIZE(v)));
for (auto e = l; e != r; e = next(e)) {
if (!first) {
res += ", ";
}
first = false;
l_v_l_v_l++;
if (e != l) {
if (rnk > 1) {
res += n_l;
t_a_b_s = l_v_l_v_l;
};
} else {
t_a_b_s = 0;
}
res += to_string(*e, coords...);
l_v_l_v_l--;
}
res += "]";
if (l_v_l_v_l == 0)
res += n_l;
return res;
}
void dbgm() { ; }
template <typename Heads, typename... Tails> void dbgm(Heads H, Tails... T) {
cout << to_string(H) << " | ";
dbgm(T...);
}
#define dbg(...) \
cout << "[" << #__VA_ARGS__ << "]: "; \
dbgm(__VA_ARGS__); \
cout << endl
int32_t main() {
#ifndef ONLINE_JUDGE
freopen("inp.txt", "r", stdin);
freopen("outp.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, sum = 0;
cin >> n;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++)
cin >> a[i];
vector<vector<int>> dp(n + 1, vector<int>(n + 1));
for (int i = 1; i <= n; i++) {
dp[i][i] = a[i];
sum += a[i];
}
for (int len = 2; len <= n; len++) {
for (int i = 1; i <= n - len + 1; i++) {
int j = i + len - 1;
dp[i][j] = max(a[i] - dp[i + 1][j], a[j] - dp[i][j - 1]);
}
}
cout << dp[1][n] << '\n';
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define n_l '\n'
template <typename T, size_t N> int SIZE(const T (&t)[N]) { return N; }
template <typename T> int SIZE(const T &t) { return t.size(); }
string to_string(const string s, int x1 = 0, int x2 = 1e9) {
return '"' + ((x1 < s.size()) ? s.substr(x1, x2 - x1 + 1) : "") + '"';
}
string to_string(const char *s) { return to_string((string)s); }
string to_string(const bool b) { return (b ? "true" : "false"); }
string to_string(const char c) { return string({c}); }
template <size_t N>
string to_string(const bitset<N> &b, int x1 = 0, int x2 = 1e9) {
string t = "";
for (int __iii__ = min(x1, SIZE(b)), __jjj__ = min(x2, SIZE(b) - 1);
__iii__ <= __jjj__; ++__iii__) {
t += b[__iii__] + '0';
}
return '"' + t + '"';
}
template <typename A, typename... C>
string to_string(const A(&v), int x1 = 0, int x2 = 1e9, C... coords);
int l_v_l_v_l = 0, t_a_b_s = 0;
template <typename A, typename B> string to_string(const pair<A, B> &p) {
l_v_l_v_l++;
string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
l_v_l_v_l--;
return res;
}
template <typename A, typename... C>
string to_string(const A(&v), int x1, int x2, C... coords) {
int rnk = rank<A>::value;
string tab(t_a_b_s, ' ');
string res = "";
bool first = true;
if (l_v_l_v_l == 0)
res += n_l;
res += tab + "[";
x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v));
auto l = begin(v);
advance(l, x1);
auto r = l;
advance(r, (x2 - x1) + (x2 < SIZE(v)));
for (auto e = l; e != r; e = next(e)) {
if (!first) {
res += ", ";
}
first = false;
l_v_l_v_l++;
if (e != l) {
if (rnk > 1) {
res += n_l;
t_a_b_s = l_v_l_v_l;
};
} else {
t_a_b_s = 0;
}
res += to_string(*e, coords...);
l_v_l_v_l--;
}
res += "]";
if (l_v_l_v_l == 0)
res += n_l;
return res;
}
void dbgm() { ; }
template <typename Heads, typename... Tails> void dbgm(Heads H, Tails... T) {
cout << to_string(H) << " | ";
dbgm(T...);
}
#define dbg(...) \
cout << "[" << #__VA_ARGS__ << "]: "; \
dbgm(__VA_ARGS__); \
cout << endl
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, sum = 0;
cin >> n;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++)
cin >> a[i];
vector<vector<int>> dp(n + 1, vector<int>(n + 1));
for (int i = 1; i <= n; i++) {
dp[i][i] = a[i];
sum += a[i];
}
for (int len = 2; len <= n; len++) {
for (int i = 1; i <= n - len + 1; i++) {
int j = i + len - 1;
dp[i][j] = max(a[i] - dp[i + 1][j], a[j] - dp[i][j - 1]);
}
}
cout << dp[1][n] << '\n';
return 0;
} | delete | 81 | 85 | 81 | 81 | -11 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mp make_pair
#define pb push_back
#define fi first
#define si second
#define fastIO \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define F(a, n) \
for (int i = 0; i < n; i++) { \
cin >> a[i]; \
}
#define F1(a, n) \
for (int i = 1; i <= n; i++) { \
cin >> a[i]; \
}
#define P(a, n) \
for (int i = 0; i < n; i++) { \
cout << a[i] << ' '; \
} \
cout << endl;
#define P1(a, n) \
for (int i = 1; i <= n; i++) { \
cout << a[i] << ' '; \
} \
cout << endl;
#define NF(a, n, m) \
for (int i = 0; i < n; i++) { \
for (int j = 0; j < m; j++) { \
cin >> a[i][j]; \
} \
}
#define NF1(a, n, m) \
for (int i = 1; i <= n; i++) { \
for (int j = 1; j <= m; j++) { \
cin >> a[i][j]; \
} \
}
#define PNF(a, n, m) \
for (int i = 0; i < n; i++) { \
for (int j = 0; j < m; j++) { \
cout << a[i][j] << ' '; \
} \
cout << endl; \
} \
cout << endl;
#define PNF1(a, n, m) \
for (int i = 1; i <= n; i++) { \
for (int j = 1; j <= m; j++) { \
cout << a[i][j] << ' '; \
} \
cout << endl; \
} \
cout << endl;
const int nax = 3e2 + 3;
const int mod = 1e9 + 7;
int main() {
fastIO ll n;
ll dp[nax][nax];
cin >> n;
ll *a = new ll[n];
for (ll i = 0; i < n; i++) {
cin >> a[i];
}
for (ll i = n - 1; i >= 0; i--) {
for (ll j = i; j < n; j++) {
dp[i][j] =
(i == j) ? (a[i]) : (max(a[i] - dp[i + 1][j], a[j] - dp[i][j - 1]));
}
}
cout << dp[0][n - 1];
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mp make_pair
#define pb push_back
#define fi first
#define si second
#define fastIO \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define F(a, n) \
for (int i = 0; i < n; i++) { \
cin >> a[i]; \
}
#define F1(a, n) \
for (int i = 1; i <= n; i++) { \
cin >> a[i]; \
}
#define P(a, n) \
for (int i = 0; i < n; i++) { \
cout << a[i] << ' '; \
} \
cout << endl;
#define P1(a, n) \
for (int i = 1; i <= n; i++) { \
cout << a[i] << ' '; \
} \
cout << endl;
#define NF(a, n, m) \
for (int i = 0; i < n; i++) { \
for (int j = 0; j < m; j++) { \
cin >> a[i][j]; \
} \
}
#define NF1(a, n, m) \
for (int i = 1; i <= n; i++) { \
for (int j = 1; j <= m; j++) { \
cin >> a[i][j]; \
} \
}
#define PNF(a, n, m) \
for (int i = 0; i < n; i++) { \
for (int j = 0; j < m; j++) { \
cout << a[i][j] << ' '; \
} \
cout << endl; \
} \
cout << endl;
#define PNF1(a, n, m) \
for (int i = 1; i <= n; i++) { \
for (int j = 1; j <= m; j++) { \
cout << a[i][j] << ' '; \
} \
cout << endl; \
} \
cout << endl;
const int nax = 3e3 + 3;
const int mod = 1e9 + 7;
int main() {
fastIO ll n;
ll dp[nax][nax];
cin >> n;
ll *a = new ll[n];
for (ll i = 0; i < n; i++) {
cin >> a[i];
}
for (ll i = n - 1; i >= 0; i--) {
for (ll j = i; j < n; j++) {
dp[i][j] =
(i == j) ? (a[i]) : (max(a[i] - dp[i + 1][j], a[j] - dp[i][j - 1]));
}
}
cout << dp[0][n - 1];
} | replace | 57 | 58 | 57 | 58 | 0 | |
p03171 | C++ | Runtime Error | #include <iostream>
#include <vector>
using namespace std;
int main() {
int N;
cin >> N;
vector<long long> a(N);
for (int i = 0; i < N; ++i)
cin >> a[i];
// dp[i][j]: Taro のターンで数列が a[i]~a[j] のときの X-Y
long long dp[300][300] = {};
for (int i = 0; i < N; ++i)
dp[i][i] = a[i];
for (int i = N - 2; i >= 0; --i) {
for (int j = i + 1; j < N; ++j) {
dp[i][j] = max(a[i] - dp[i + 1][j], a[j] - dp[i][j - 1]);
}
}
cout << dp[0][N - 1] << endl;
return 0;
} | #include <iostream>
#include <vector>
using namespace std;
int main() {
int N;
cin >> N;
vector<long long> a(N);
for (int i = 0; i < N; ++i)
cin >> a[i];
// dp[i][j]: Taro のターンで数列が a[i]~a[j] のときの X-Y
long long dp[3000][3000] = {};
for (int i = 0; i < N; ++i)
dp[i][i] = a[i];
for (int i = N - 2; i >= 0; --i) {
for (int j = i + 1; j < N; ++j) {
dp[i][j] = max(a[i] - dp[i + 1][j], a[j] - dp[i][j - 1]);
}
}
cout << dp[0][N - 1] << endl;
return 0;
} | replace | 12 | 13 | 12 | 13 | 0 | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ALL(a) begin(a), end(a)
#define SZ(a) ((int)(a).size())
#define REP(a, b) for (int a = 0; a < (b); a++)
#define FOR(a, b, c) for (int a = (b); a <= (c); a++)
#define REPD(a, b) for (int a = (b)-1; a >= 0; a--)
#define FORD(a, b, c) for (int a = (b); a >= (c); a--)
#define MIN(a, b) (a) = min((a), (b))
#define MAX(a, b) (a) = max((a), (b))
#ifdef __DEBUG
#define debug if (true)
#else
#define debug if (false)
#endif
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
template <typename T> using V = vector<T>;
template <typename T> using VV = vector<V<T>>;
template <typename T> using VVV = vector<VV<T>>;
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
return is >> p.fi >> p.se;
}
const int N = 3003;
int n;
int a[N];
ll dp[N][N];
bool visit[N][N];
ll go(int l, int r) {
if (l > r)
return 0;
if (visit[l][r])
return dp[l][r];
int turn = (n - (r - l + 1)) % 2;
if (turn == 0) {
dp[l][r] = max(a[l] + go(l + 1, r), a[r] + go(l, r - 1));
} else {
dp[l][r] = min(go(l + 1, r) - a[l], go(l, r - 1) - a[r]);
}
return dp[l][r];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
REP(i, n) cin >> a[i];
cout << go(0, n - 1) << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ALL(a) begin(a), end(a)
#define SZ(a) ((int)(a).size())
#define REP(a, b) for (int a = 0; a < (b); a++)
#define FOR(a, b, c) for (int a = (b); a <= (c); a++)
#define REPD(a, b) for (int a = (b)-1; a >= 0; a--)
#define FORD(a, b, c) for (int a = (b); a >= (c); a--)
#define MIN(a, b) (a) = min((a), (b))
#define MAX(a, b) (a) = max((a), (b))
#ifdef __DEBUG
#define debug if (true)
#else
#define debug if (false)
#endif
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
template <typename T> using V = vector<T>;
template <typename T> using VV = vector<V<T>>;
template <typename T> using VVV = vector<VV<T>>;
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
return is >> p.fi >> p.se;
}
const int N = 3003;
int n;
int a[N];
ll dp[N][N];
bool visit[N][N];
ll go(int l, int r) {
if (l > r)
return 0;
if (visit[l][r])
return dp[l][r];
int turn = (n - (r - l + 1)) % 2;
if (turn == 0) {
dp[l][r] = max(a[l] + go(l + 1, r), a[r] + go(l, r - 1));
} else {
dp[l][r] = min(go(l + 1, r) - a[l], go(l, r - 1) - a[r]);
}
visit[l][r] = true;
return dp[l][r];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
REP(i, n) cin >> a[i];
cout << go(0, n - 1) << endl;
}
| insert | 56 | 56 | 56 | 57 | TLE | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define REP(i, m, n) for (int i = (m); i < (int)(n); i++)
#define RREP(i, m, n) for (int i = (int)(n - 1); i >= m; i--)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) RREP(i, 0, n)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define aut(r, v) __typeof(v) r = (v)
#define each(it, o) for (aut(it, (o).begin()); it != (o).end(); ++it)
#define reach(it, o) for (aut(it, (o).rbegin()); it != (o).rend(); ++it)
#define fi first
#define se second
#define debug(...) \
{ \
cerr << "[L" << __LINE__ << "] "; \
_debug(__VA_ARGS__); \
}
template <typename T1, typename T2>
ostream &operator<<(ostream &o, const pair<T1, T2> &p) {
return o << "(" << p.first << ", " << p.second << ")";
}
template <typename T> string join(const vector<T> &v, string del = ", ") {
stringstream s;
rep(i, v.size()) s << del << v[i];
return s.str().substr(del.size());
}
template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) {
if (v.size())
o << "[" << join(v) << "]";
return o;
}
template <typename T>
ostream &operator<<(ostream &o, const vector<vector<T>> &vv) {
int l = vv.size();
if (l) {
o << endl;
rep(i, l) {
o << (i == 0 ? "[ " : ",\n ") << vv[i] << (i == l - 1 ? " ]" : "");
}
}
return o;
}
template <typename T> ostream &operator<<(ostream &o, const set<T> &st) {
vector<T> v(st.begin(), st.end());
o << "{ " << join(v) << " }";
return o;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &o, const map<T1, T2> &m) {
each(p, m) {
o << (p == m.begin() ? "{ " : ",\n ") << *p
<< (p == --m.end() ? " }" : "");
}
return o;
}
inline void _debug() { cerr << endl; }
template <class First, class... Rest>
void _debug(const First &first, const Rest &...rest) {
cerr << first << " ";
_debug(rest...);
}
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
const double PI = (1 * acos(0.0));
const double EPS = 1e-9;
const ll INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3fLL;
const ll mod = 1e9 + 7;
inline void finput(string filename) { freopen(filename.c_str(), "r", stdin); }
vl A;
vvl dp;
ll rec(int l, int r, int me) {
if (dp[l][r] != INFL)
return dp[l][r];
if (l == r)
return (me ? 1 : -1) * A[l];
ll ret;
if (me) {
ret = max(A[l] + rec(l + 1, r, me ^ 1), A[r] + rec(l, r - 1, me ^ 1));
debug(l, r, me, ret)
} else {
ret = min(-A[l] + rec(l + 1, r, me ^ 1), -A[r] + rec(l, r - 1, me ^ 1));
}
return dp[l][r] = ret;
}
int main() {
ios_base::sync_with_stdio(0);
// finput("./input");
int n;
cin >> n;
A = vl(n);
rep(i, n) cin >> A[i];
dp = vvl(n, vl(n, INFL));
cout << rec(0, n - 1, 1) << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define REP(i, m, n) for (int i = (m); i < (int)(n); i++)
#define RREP(i, m, n) for (int i = (int)(n - 1); i >= m; i--)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) RREP(i, 0, n)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define aut(r, v) __typeof(v) r = (v)
#define each(it, o) for (aut(it, (o).begin()); it != (o).end(); ++it)
#define reach(it, o) for (aut(it, (o).rbegin()); it != (o).rend(); ++it)
#define fi first
#define se second
#define debug(...) \
{ \
cerr << "[L" << __LINE__ << "] "; \
_debug(__VA_ARGS__); \
}
template <typename T1, typename T2>
ostream &operator<<(ostream &o, const pair<T1, T2> &p) {
return o << "(" << p.first << ", " << p.second << ")";
}
template <typename T> string join(const vector<T> &v, string del = ", ") {
stringstream s;
rep(i, v.size()) s << del << v[i];
return s.str().substr(del.size());
}
template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) {
if (v.size())
o << "[" << join(v) << "]";
return o;
}
template <typename T>
ostream &operator<<(ostream &o, const vector<vector<T>> &vv) {
int l = vv.size();
if (l) {
o << endl;
rep(i, l) {
o << (i == 0 ? "[ " : ",\n ") << vv[i] << (i == l - 1 ? " ]" : "");
}
}
return o;
}
template <typename T> ostream &operator<<(ostream &o, const set<T> &st) {
vector<T> v(st.begin(), st.end());
o << "{ " << join(v) << " }";
return o;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &o, const map<T1, T2> &m) {
each(p, m) {
o << (p == m.begin() ? "{ " : ",\n ") << *p
<< (p == --m.end() ? " }" : "");
}
return o;
}
inline void _debug() { cerr << endl; }
template <class First, class... Rest>
void _debug(const First &first, const Rest &...rest) {
cerr << first << " ";
_debug(rest...);
}
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
const double PI = (1 * acos(0.0));
const double EPS = 1e-9;
const ll INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3fLL;
const ll mod = 1e9 + 7;
inline void finput(string filename) { freopen(filename.c_str(), "r", stdin); }
vl A;
vvl dp;
ll rec(int l, int r, int me) {
if (dp[l][r] != INFL)
return dp[l][r];
if (l == r)
return (me ? 1 : -1) * A[l];
ll ret;
if (me) {
ret = max(A[l] + rec(l + 1, r, me ^ 1), A[r] + rec(l, r - 1, me ^ 1));
} else {
ret = min(-A[l] + rec(l + 1, r, me ^ 1), -A[r] + rec(l, r - 1, me ^ 1));
}
return dp[l][r] = ret;
}
int main() {
ios_base::sync_with_stdio(0);
// finput("./input");
int n;
cin >> n;
A = vl(n);
rep(i, n) cin >> A[i];
dp = vvl(n, vl(n, INFL));
cout << rec(0, n - 1, 1) << endl;
return 0;
} | delete | 91 | 92 | 91 | 91 | TLE | |
p03171 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define REPP(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define PER(i, a) for (int i = (a)-1; i >= 0; --i)
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#define MP make_pair
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 3005;
ll a[maxn], dp[maxn][maxn];
int main() {
int n;
cin >> n;
REP(i, n) cin >> a[i];
REPP(i, 1, n + 1) REP(j, n + i - 1) {
dp[j][j + i] =
max(a[j] - dp[j + 1][j + i], a[j + i - 1] - dp[j][j + i - 1]);
}
cout << dp[0][n] << endl;
return 0;
}
| #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define REPP(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define PER(i, a) for (int i = (a)-1; i >= 0; --i)
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#define MP make_pair
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 3005;
ll a[maxn], dp[maxn][maxn];
int main() {
int n;
cin >> n;
REP(i, n) cin >> a[i];
REPP(i, 1, n + 1) REP(j, n + 1 - i) {
dp[j][j + i] =
max(a[j] - dp[j + 1][j + i], a[j + i - 1] - dp[j][j + i - 1]);
}
cout << dp[0][n] << endl;
return 0;
}
| replace | 30 | 31 | 30 | 31 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define lli long long int
#define ulli unsigned long long int
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define loopl(i, a, b) for (lli i = a; i < b; i++)
#define loop(i, a, b) for (int i = a; i < b; i++)
#define mod 1000000007
#define inf 1000000000000000
#define lld long double
#define pll pair<long long int, long long int>
#define vpll vector<pll>
#define vll vector<lli>
#define eps 0.000001
#define endl '\n'
using namespace std;
#define MAXN 30001
// lli spf[MAXN];
// void sieve();
// set<lli> factors(lli x);
// void getZarr(string str, lli Z[]);
lli power(lli x, lli y, lli p);
lli dp[3001][3001][2];
lli a[3001];
lli f(lli l, lli r, lli n) {
if (dp[l][r][n] != -1)
return dp[l][r][n];
if (l == r && n == 0)
return dp[l][r][0] = a[l];
else if (l == r && n == 1)
return dp[l][r][1] = 0;
if (n == 0)
return dp[l][r][0] = max(a[l] + f(l + 1, r, 1), a[r] + f(l, r - 1, 1));
else
return dp[l][r][1] = min(f(l + 1, r, 0), f(l, r - 1, 0));
}
int main() {
/////////////////////////////////////
fastio;
#ifndef ONLINE_JUDGE
freopen("input.txt", "rt", stdin);
freopen("output.txt", "wt", stdout);
#endif
/////////////////////////////////////
loopl(i, 0, 3001) loopl(j, 0, 3001) dp[i][j][0] = dp[i][j][1] = -1;
lli n;
cin >> n;
lli sum = 0;
loopl(i, 1, n + 1) {
cin >> a[i];
sum += a[i];
}
lli x = f(1, n, 0);
cout << 2 * x - sum << endl;
}
/* void sieve()
{
spf[1] = 1;
loopl(i,2,MAXN)
spf[i] = i;
for (lli i=4; i<MAXN; i+=2)
spf[i] = 2;
for (lli i=3; i*i<MAXN; i++) {
if (spf[i] == i) {
for (lli j=i*i; j<MAXN; j+=i)
if (spf[j]==j)
spf[j] = i;
}
}
}*/
/*
multiset<lli> factors(lli x)
{
multiset<lli> pr;
while (x != 1)
{
pr.insert(spf[x]);
x = x / spf[x];
}
return pr;
}
*/
/*
void dfs(lli q,lli root)
{
vis[q]=1;
ans[q]=1;
for(auto x: v[q])
{
if(!vis[x])
{
if(a[x]==a[root] && x<root) continue;
if(a[x]<a[root] || a[x]>a[root]+d) continue;
dfs(x,root);
ans[q]=(ans[q]*((ans[x]+1)%mod))%mod ;
}
}
}
*/
/*void getZarr(string str, lli Z[])
{
lli n = str.length();
lli L, R, k;
L = R = 0;
for (lli i = 1; i < n; ++i){
if (i > R){
L = R = i;
while (R<n && str[R-L] == str[R])
R++;
Z[i] = R-L;
R--;
} else {
k = i-L;
if (Z[k] < R-i+1)
Z[i] = Z[k];
else {
L = i;
while (R<n && str[R-L] == str[R])
R++;
Z[i] = R-L;
R--;
}
}
}
}
*/
lli power(lli x, lli y, lli p) {
lli res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
| #include <bits/stdc++.h>
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define lli long long int
#define ulli unsigned long long int
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define loopl(i, a, b) for (lli i = a; i < b; i++)
#define loop(i, a, b) for (int i = a; i < b; i++)
#define mod 1000000007
#define inf 1000000000000000
#define lld long double
#define pll pair<long long int, long long int>
#define vpll vector<pll>
#define vll vector<lli>
#define eps 0.000001
#define endl '\n'
using namespace std;
#define MAXN 30001
// lli spf[MAXN];
// void sieve();
// set<lli> factors(lli x);
// void getZarr(string str, lli Z[]);
lli power(lli x, lli y, lli p);
lli dp[3001][3001][2];
lli a[3001];
lli f(lli l, lli r, lli n) {
if (dp[l][r][n] != -1)
return dp[l][r][n];
if (l == r && n == 0)
return dp[l][r][0] = a[l];
else if (l == r && n == 1)
return dp[l][r][1] = 0;
if (n == 0)
return dp[l][r][0] = max(a[l] + f(l + 1, r, 1), a[r] + f(l, r - 1, 1));
else
return dp[l][r][1] = min(f(l + 1, r, 0), f(l, r - 1, 0));
}
int main() {
/////////////////////////////////////
fastio;
/////////////////////////////////////
loopl(i, 0, 3001) loopl(j, 0, 3001) dp[i][j][0] = dp[i][j][1] = -1;
lli n;
cin >> n;
lli sum = 0;
loopl(i, 1, n + 1) {
cin >> a[i];
sum += a[i];
}
lli x = f(1, n, 0);
cout << 2 * x - sum << endl;
}
/* void sieve()
{
spf[1] = 1;
loopl(i,2,MAXN)
spf[i] = i;
for (lli i=4; i<MAXN; i+=2)
spf[i] = 2;
for (lli i=3; i*i<MAXN; i++) {
if (spf[i] == i) {
for (lli j=i*i; j<MAXN; j+=i)
if (spf[j]==j)
spf[j] = i;
}
}
}*/
/*
multiset<lli> factors(lli x)
{
multiset<lli> pr;
while (x != 1)
{
pr.insert(spf[x]);
x = x / spf[x];
}
return pr;
}
*/
/*
void dfs(lli q,lli root)
{
vis[q]=1;
ans[q]=1;
for(auto x: v[q])
{
if(!vis[x])
{
if(a[x]==a[root] && x<root) continue;
if(a[x]<a[root] || a[x]>a[root]+d) continue;
dfs(x,root);
ans[q]=(ans[q]*((ans[x]+1)%mod))%mod ;
}
}
}
*/
/*void getZarr(string str, lli Z[])
{
lli n = str.length();
lli L, R, k;
L = R = 0;
for (lli i = 1; i < n; ++i){
if (i > R){
L = R = i;
while (R<n && str[R-L] == str[R])
R++;
Z[i] = R-L;
R--;
} else {
k = i-L;
if (Z[k] < R-i+1)
Z[i] = Z[k];
else {
L = i;
while (R<n && str[R-L] == str[R])
R++;
Z[i] = R-L;
R--;
}
}
}
}
*/
lli power(lli x, lli y, lli p) {
lli res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
| delete | 58 | 62 | 58 | 58 | -11 | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
void rwFile() {
freopen("INP.inp", "r", stdin);
freopen("OUT.out", "w", stdout);
}
long long arr[3001];
long long res[3001][3001];
void findRes(int a, int b) {
if (a == b) {
res[a][b] = arr[a];
return;
}
findRes(a, b - 1);
findRes(a + 1, b);
res[a][b] = max(arr[b] - res[a][b - 1], arr[a] - res[a + 1][b]);
}
void solve() {
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> arr[i];
findRes(1, n);
// for (int i = 1; i <= n; i++) {
// for (int j = 1; j <= n; j++) cout << res[i][j] << " ";
// cout << endl;
// }
cout << res[1][n];
}
int main() {
// rwFile();
solve();
}
| #include <bits/stdc++.h>
using namespace std;
void rwFile() {
freopen("INP.inp", "r", stdin);
freopen("OUT.out", "w", stdout);
}
long long arr[3001];
long long res[3001][3001];
void findRes(int a, int b) {
if (res[a][b] != 0)
return;
if (a == b) {
res[a][b] = arr[a];
return;
}
findRes(a, b - 1);
findRes(a + 1, b);
res[a][b] = max(arr[b] - res[a][b - 1], arr[a] - res[a + 1][b]);
}
void solve() {
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> arr[i];
findRes(1, n);
// for (int i = 1; i <= n; i++) {
// for (int j = 1; j <= n; j++) cout << res[i][j] << " ";
// cout << endl;
// }
cout << res[1][n];
}
int main() {
// rwFile();
solve();
}
| insert | 13 | 13 | 13 | 15 | TLE | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define ll long long
const long double EPS = 1e-8;
const long double PI = acos(-1);
const int N = 3e3 + 2;
const int mod = 1e5 + 7;
ll mem[N][N][2];
int arr[N];
int n, k;
ll solve(int i = 0, int j = n - 1, int player = 0) {
if (i > j)
return 0;
// if (~mem[i][j][player])return mem[i][j][player];
if (!player) {
return mem[i][j][player] = max(solve(i + 1, j, !player) + arr[i],
solve(i, j - 1, !player) + arr[j]);
} else {
return mem[i][j][player] = min(solve(i + 1, j, !player) - arr[i],
solve(i, j - 1, !player) - arr[j]);
}
}
int main() {
cin >> n;
memset(mem, -1, sizeof mem);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
cout << solve() << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
const long double EPS = 1e-8;
const long double PI = acos(-1);
const int N = 3e3 + 2;
const int mod = 1e5 + 7;
ll mem[N][N][2];
int arr[N];
int n, k;
ll solve(int i = 0, int j = n - 1, int player = 0) {
if (i > j)
return 0;
if (~mem[i][j][player])
return mem[i][j][player];
if (!player) {
return mem[i][j][player] = max(solve(i + 1, j, !player) + arr[i],
solve(i, j - 1, !player) + arr[j]);
} else {
return mem[i][j][player] = min(solve(i + 1, j, !player) - arr[i],
solve(i, j - 1, !player) - arr[j]);
}
}
int main() {
cin >> n;
memset(mem, -1, sizeof mem);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
cout << solve() << endl;
}
| replace | 15 | 16 | 15 | 17 | TLE | |
p03171 | C++ | Runtime Error | #include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using lli = long long int;
template <class T, class U> void init_n(vector<T> &v, size_t n, U x) {
v = vector<T>(n, x);
}
template <class T> void init_n(vector<T> &v, size_t n) { init_n(v, n, T()); }
template <class T> void read_n(vector<T> &v, size_t n, size_t o = 0) {
v = vector<T>(n + o);
for (lli i = o; i < n + o; ++i)
cin >> v[i];
}
template <class T> void read_n(T a[], size_t n, size_t o = 0) {
for (lli i = o; i < n + o; ++i)
cin >> a[i];
}
template <class T> T gabs(const T &x) { return max(x, -x); }
#define abs gabs
lli n, k, a[3000];
lli memo[3000][3000];
lli dfs(lli t, lli l, lli r) {
if (memo[l][r] != 1e18)
return memo[l][r];
lli yl = dfs(-t, l + 1, r);
lli yr = dfs(-t, l, r - 1);
lli res;
if (t == 1)
res = max(yl + a[l], yr + a[r]);
else
res = min(yl - a[l], yr - a[r]);
return memo[l][r] = res;
}
int main() {
cin >> n;
read_n(a, n);
for (lli i = 0; i < n; ++i)
fill(&memo[i][i], &memo[i][n + 1], 1e18);
cout << dfs(1, 0, n - 1) << '\n';
return 0;
}
| #include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using lli = long long int;
template <class T, class U> void init_n(vector<T> &v, size_t n, U x) {
v = vector<T>(n, x);
}
template <class T> void init_n(vector<T> &v, size_t n) { init_n(v, n, T()); }
template <class T> void read_n(vector<T> &v, size_t n, size_t o = 0) {
v = vector<T>(n + o);
for (lli i = o; i < n + o; ++i)
cin >> v[i];
}
template <class T> void read_n(T a[], size_t n, size_t o = 0) {
for (lli i = o; i < n + o; ++i)
cin >> a[i];
}
template <class T> T gabs(const T &x) { return max(x, -x); }
#define abs gabs
lli n, k, a[3000];
lli memo[3001][3001];
lli dfs(lli t, lli l, lli r) {
if (memo[l][r] != 1e18)
return memo[l][r];
lli yl = dfs(-t, l + 1, r);
lli yr = dfs(-t, l, r - 1);
lli res;
if (t == 1)
res = max(yl + a[l], yr + a[r]);
else
res = min(yl - a[l], yr - a[r]);
return memo[l][r] = res;
}
int main() {
cin >> n;
read_n(a, n);
for (lli i = 0; i < n; ++i)
fill(&memo[i][i], &memo[i][n + 1], 1e18);
cout << dfs(1, 0, n - 1) << '\n';
return 0;
}
| replace | 36 | 37 | 36 | 37 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define Elhabashy \
ios::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
void input() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
}
const int N = 50 + 5, mod = 1e9 + 7;
typedef long long ll;
ll n, arr[N], dp[N][N][3];
ll solve(int i = 1, int j = n, bool f = 0) {
if (i > j)
return 0;
ll &ret = dp[i][j][f];
if (~ret)
return ret;
if (f == 0) {
ret = arr[i] + solve(i + 1, j, 1);
ret = max(ret, arr[j] + solve(i, j - 1, 1));
} else {
ret = -arr[i] + solve(i + 1, j, 0);
ret = min(ret, -arr[j] + solve(i, j - 1, 0));
}
return ret;
}
int main() {
Elhabashy input();
memset(dp, -1, sizeof dp);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> arr[i];
cout << solve() << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define Elhabashy \
ios::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
void input() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
}
const int N = 3000 + 5, mod = 1e9 + 7;
typedef long long ll;
ll n, arr[N], dp[N][N][3];
ll solve(int i = 1, int j = n, bool f = 0) {
if (i > j)
return 0;
ll &ret = dp[i][j][f];
if (~ret)
return ret;
if (f == 0) {
ret = arr[i] + solve(i + 1, j, 1);
ret = max(ret, arr[j] + solve(i, j - 1, 1));
} else {
ret = -arr[i] + solve(i + 1, j, 0);
ret = min(ret, -arr[j] + solve(i, j - 1, 0));
}
return ret;
}
int main() {
Elhabashy input();
memset(dp, -1, sizeof dp);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> arr[i];
cout << solve() << endl;
}
| replace | 11 | 12 | 11 | 12 | 0 | |
p03171 | C++ | Time Limit Exceeded | #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
template <class T> using V = vector<T>;
template <class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i = k; i < (ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
#define ALL(v) v.begin(), v.end()
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 DEBUG_VLL(vec) \
REP(sz, vec.size()) \
std::cerr << vec[sz] << (sz == vec.size() - 1 ? '\n' : ' ');
const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;
vll a;
vvll dp;
ll dfs(int left, int right) {
if (dp[left][right] > -HIGHINF)
return dp[left][right];
if (left == right)
return dp[left][right] = a[left];
return max(a[left] - dfs(left + 1, right), a[right] - dfs(left, right - 1));
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
a.resize(n);
for (int i = 0; i < n; i++)
cin >> a[i];
dp.resize(n, vll(n, -HIGHINF));
cout << dfs(0, n - 1) << '\n';
return 0;
}
| #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
template <class T> using V = vector<T>;
template <class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i = k; i < (ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
#define ALL(v) v.begin(), v.end()
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 DEBUG_VLL(vec) \
REP(sz, vec.size()) \
std::cerr << vec[sz] << (sz == vec.size() - 1 ? '\n' : ' ');
const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;
vll a;
vvll dp;
ll dfs(int left, int right) {
if (dp[left][right] > -HIGHINF)
return dp[left][right];
if (left == right)
return dp[left][right] = a[left];
return dp[left][right] = max(a[left] - dfs(left + 1, right),
a[right] - dfs(left, right - 1));
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
a.resize(n);
for (int i = 0; i < n; i++)
cin >> a[i];
dp.resize(n, vll(n, -HIGHINF));
cout << dfs(0, n - 1) << '\n';
return 0;
}
| replace | 63 | 64 | 63 | 65 | TLE | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int N = 305, MOD = 1e9 + 7;
const long long INF = 9e18;
int n, a[N];
long long dp[N][N][2];
long long rec(int l, int r, bool turn) {
if (l > r) {
return 0;
}
if (dp[l][r][turn] != -INF) {
return dp[l][r][turn];
}
dp[l][r][turn] =
max(a[l] - rec(l + 1, r, turn ^ 1), a[r] - rec(l, r - 1, turn ^ 1));
return dp[l][r][turn];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
dp[i][j][0] = dp[i][j][1] = -INF;
}
}
cout << rec(1, n, 0);
}
| #include <bits/stdc++.h>
using namespace std;
const int N = 3005, MOD = 1e9 + 7;
const long long INF = 9e18;
int n, a[N];
long long dp[N][N][2];
long long rec(int l, int r, bool turn) {
if (l > r) {
return 0;
}
if (dp[l][r][turn] != -INF) {
return dp[l][r][turn];
}
dp[l][r][turn] =
max(a[l] - rec(l + 1, r, turn ^ 1), a[r] - rec(l, r - 1, turn ^ 1));
return dp[l][r][turn];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
dp[i][j][0] = dp[i][j][1] = -INF;
}
}
cout << rec(1, n, 0);
}
| replace | 3 | 4 | 3 | 4 | 0 | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define maxn (int)3010
#define int long long
int tab[maxn], dp[maxn][maxn][3], n;
bool odw[maxn][maxn][3];
int rob(int start, int stop, int kto) {
if (odw[start][stop][kto])
return dp[start][stop][kto];
// printf("Przedzial %lld %lld %lld\n", start, stop, kto);
if (kto == 1) {
if (start == stop) {
dp[start][stop][kto] = tab[stop] * (-1);
// printf("DLa przedzialu %lld %lld %lld mamy %lld\n", start, stop, kto,
// dp[start][stop][kto]);
return dp[start][stop][kto];
}
dp[start][stop][kto] = min(rob(start, stop - 1, 2) - tab[stop],
rob(start + 1, stop, 2) - tab[start]);
// printf("DLa przedzialu %lld %lld %lld mamy %lld\n", start, stop, kto,
// dp[start][stop][kto]);
return dp[start][stop][kto];
} else {
if (start == stop) {
dp[start][stop][kto] = tab[stop];
// printf("DLa przedzialu %lld %lld %lld mamy %lld\n", start, stop, kto,
// dp[start][stop][kto]);
return dp[start][stop][kto];
}
dp[start][stop][kto] = max(rob(start, stop - 1, 1) + tab[stop],
rob(start + 1, stop, 1) + tab[start]);
// printf("DLa przedzialu %lld %lld %lld mamy %lld\n", start, stop, kto,
// dp[start][stop][kto]);
return dp[start][stop][kto];
}
}
int32_t main() {
scanf("%lld", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &tab[i]);
}
rob(1, n, 2);
printf("%lld", dp[1][n][2]);
}
| #include <bits/stdc++.h>
using namespace std;
#define maxn (int)3010
#define int long long
int tab[maxn], dp[maxn][maxn][3], n;
bool odw[maxn][maxn][3];
int rob(int start, int stop, int kto) {
if (odw[start][stop][kto])
return dp[start][stop][kto];
odw[start][stop][kto] = true;
// printf("Przedzial %lld %lld %lld\n", start, stop, kto);
if (kto == 1) {
if (start == stop) {
dp[start][stop][kto] = tab[stop] * (-1);
// printf("DLa przedzialu %lld %lld %lld mamy %lld\n", start, stop, kto,
// dp[start][stop][kto]);
return dp[start][stop][kto];
}
dp[start][stop][kto] = min(rob(start, stop - 1, 2) - tab[stop],
rob(start + 1, stop, 2) - tab[start]);
// printf("DLa przedzialu %lld %lld %lld mamy %lld\n", start, stop, kto,
// dp[start][stop][kto]);
return dp[start][stop][kto];
} else {
if (start == stop) {
dp[start][stop][kto] = tab[stop];
// printf("DLa przedzialu %lld %lld %lld mamy %lld\n", start, stop, kto,
// dp[start][stop][kto]);
return dp[start][stop][kto];
}
dp[start][stop][kto] = max(rob(start, stop - 1, 1) + tab[stop],
rob(start + 1, stop, 1) + tab[start]);
// printf("DLa przedzialu %lld %lld %lld mamy %lld\n", start, stop, kto,
// dp[start][stop][kto]);
return dp[start][stop][kto];
}
}
int32_t main() {
scanf("%lld", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &tab[i]);
}
rob(1, n, 2);
printf("%lld", dp[1][n][2]);
}
| insert | 10 | 10 | 10 | 11 | TLE | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1000000007;
const ld PI = acos(-1);
const ld EPS = 0.0000000001;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repd(i, n) for (ll i = n - 1; 0 <= i; i--)
#define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a - 1; (ll)(b) <= i; i--)
#define ALL(x) x.begin(), x.end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int main() {
ll n;
cin >> n;
vector<ll> a(n);
vector<vector<ll>> dp(n + 2, vector<ll>(n + 2, 0));
rep(i, n) {
cin >> a[i];
// dp[i][i] = a[i];
}
FOR(len, 1, n + 1) {
for (int i = 0; i <= n; i++) {
int j = i + len;
if ((n - len) % 2 == 0) {
dp[i][j] = max(dp[i + 1][j] + a[i], dp[i][j - 1] + a[j - 1]);
} else {
dp[i][j] = min(dp[i + 1][j] - a[i], dp[i][j - 1] - a[j - 1]);
}
}
}
cout << dp[0][n] << endl;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1000000007;
const ld PI = acos(-1);
const ld EPS = 0.0000000001;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repd(i, n) for (ll i = n - 1; 0 <= i; i--)
#define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a - 1; (ll)(b) <= i; i--)
#define ALL(x) x.begin(), x.end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int main() {
ll n;
cin >> n;
vector<ll> a(n);
vector<vector<ll>> dp(n + 2, vector<ll>(n + 2, 0));
rep(i, n) {
cin >> a[i];
// dp[i][i] = a[i];
}
FOR(len, 1, n + 1) {
for (int i = 0; i <= n; i++) {
int j = i + len;
if (n < j)
continue;
if ((n - len) % 2 == 0) {
dp[i][j] = max(dp[i + 1][j] + a[i], dp[i][j - 1] + a[j - 1]);
} else {
dp[i][j] = min(dp[i + 1][j] - a[i], dp[i][j - 1] - a[j - 1]);
}
}
}
cout << dp[0][n] << endl;
}
| insert | 31 | 31 | 31 | 33 | -6 | munmap_chunk(): invalid pointer
|
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long int lli;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
lli n, i, j, size, l, r, rv, lv;
cin >> n;
lli a[n];
lli maximize[n][n];
for (i = 0; i < n; ++i) {
cin >> a[i];
maximize[i][i] = a[i];
}
lli INF = 1e18;
// cout << INF << '\n';
/*
for(i = 0; i < n; ++i)
{
for(j = 0; j < n; ++j)
{
maximize[i][j] = -INF;
}
}
*/
for (size = 2; size <= n; ++size) {
for (l = 0; l < n - size + 1; ++l) {
for (r = l + size - 1; r < n; ++r) {
// max
lv = a[l] - maximize[l + 1][r];
rv = a[r] - maximize[l][r - 1];
maximize[l][r] = max(lv, rv);
}
}
}
/*
for(i = 0; i < n; ++i)
{
for(j = 0; j < n; ++j)
{
cout << maximize[i][j] << " ";
}
cout << '\n';
}
for(i = 0; i < n; ++i)
{
for(j = 0; j < n; ++j)
{
cout << minimize[i][j] << " ";
}
cout << '\n';
}
*/
cout << maximize[0][n - 1];
return 0;
} | #include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long int lli;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
lli n, i, j, size, l, r, rv, lv;
cin >> n;
lli a[n];
lli maximize[n][n];
for (i = 0; i < n; ++i) {
cin >> a[i];
maximize[i][i] = a[i];
}
lli INF = 1e18;
// cout << INF << '\n';
/*
for(i = 0; i < n; ++i)
{
for(j = 0; j < n; ++j)
{
maximize[i][j] = -INF;
}
}
*/
for (size = 2; size <= n; ++size) {
for (l = 0; l < n - size + 1; ++l) {
r = l + size - 1;
lv = a[l] - maximize[l + 1][r];
rv = a[r] - maximize[l][r - 1];
maximize[l][r] = max(lv, rv);
}
}
/*
for(i = 0; i < n; ++i)
{
for(j = 0; j < n; ++j)
{
cout << maximize[i][j] << " ";
}
cout << '\n';
}
for(i = 0; i < n; ++i)
{
for(j = 0; j < n; ++j)
{
cout << minimize[i][j] << " ";
}
cout << '\n';
}
*/
cout << maximize[0][n - 1];
return 0;
} | replace | 39 | 45 | 39 | 43 | TLE | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll solve(vector<vector<ll>> &dp, vector<ll> &a, int head, int tail) {
if (head >= tail) {
return dp[head][tail] = a[head];
}
ll best = max(-solve(dp, a, head, tail - 1) + a[tail],
-solve(dp, a, head + 1, tail) + a[head]);
return dp[head][tail] = best;
}
int main() {
int N;
cin >> N;
vector<ll> a(N);
for (int i = 0; i < N; i++) {
cin >> a[i];
}
vector<vector<ll>> dp(N, vector<ll>(N));
cout << solve(dp, a, 0, a.size() - 1) << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll solve(vector<vector<ll>> &dp, vector<ll> &a, int head, int tail) {
if (head >= tail) {
return dp[head][tail] = a[head];
}
if (dp[head][tail] != 0)
return dp[head][tail];
ll best = max(-solve(dp, a, head, tail - 1) + a[tail],
-solve(dp, a, head + 1, tail) + a[head]);
return dp[head][tail] = best;
}
int main() {
int N;
cin >> N;
vector<ll> a(N);
for (int i = 0; i < N; i++) {
cin >> a[i];
}
vector<vector<ll>> dp(N, vector<ll>(N));
cout << solve(dp, a, 0, a.size() - 1) << endl;
return 0;
}
| insert | 8 | 8 | 8 | 10 | TLE | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define int long long int
#define pb push_back
// #define M 1000000007
#define inf 10000000000
#define ff first
#define ss second
#define deb(x) cout < #x < " " < x < endl;
#define fo(i, n) for (int i = 0; i < n; i++)
#define Fo(i, k, n) for (int i = k; i <= n; i++)
#define FORR(a, b, c) for (int a = b; a >= c; a--)
#define fa(arrr) for (auto x : arrr)
#define iPair pair<int, pair<int, int>>
#define sz size()
// #define mp make_pair
#define mxm3(a, b, c) max(a, max(b, c))
#define mi3(a, b, c) min(a, min(b, c))
#define mxn 500005
#define PII pair<int, int>
#define all(v) v.begin(), v.end()
#define endl "\n"
// #define N 1000
#define C 4
#define nfs ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
// Ordered Set Tree
// typedef tree<PII, null_type, less<PII>,
// rb_tree_tag,
// tree_order_statistics_node_update>
// oset;
#define db(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cout << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cout.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
void computeLPSArray(string pat, int M, int *lps);
int dp[1000][1000][2];
int recurr(int *arr, int i, int j, bool turn) {
if (i > j)
return 0;
if (dp[i][j][turn] != -1)
return dp[i][j][turn];
if (turn == 1) {
dp[i][j][turn] = max(arr[i] + recurr(arr, i + 1, j, !turn),
arr[j] + recurr(arr, i, j - 1, !turn));
return dp[i][j][turn];
} else {
return dp[i][j][turn] =
min(recurr(arr, i + 1, j, !turn), recurr(arr, i, j - 1, !turn));
}
}
void solve() {
int n;
cin >> n;
int arr[n];
int sum = 0;
memset(dp, -1, sizeof(dp));
for (int i = 0; i < n; i++) {
cin >> arr[i];
sum += arr[i];
}
int scrx = recurr(arr, 0, n - 1, 1);
int scry = sum - scrx;
cout << scrx - scry << endl;
}
int32_t main() {
nfs;
int t;
// facto();
// cin >> t;
t = 1;
while (t--) {
solve();
}
return 0;
} | #include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define int long long int
#define pb push_back
// #define M 1000000007
#define inf 10000000000
#define ff first
#define ss second
#define deb(x) cout < #x < " " < x < endl;
#define fo(i, n) for (int i = 0; i < n; i++)
#define Fo(i, k, n) for (int i = k; i <= n; i++)
#define FORR(a, b, c) for (int a = b; a >= c; a--)
#define fa(arrr) for (auto x : arrr)
#define iPair pair<int, pair<int, int>>
#define sz size()
// #define mp make_pair
#define mxm3(a, b, c) max(a, max(b, c))
#define mi3(a, b, c) min(a, min(b, c))
#define mxn 500005
#define PII pair<int, int>
#define all(v) v.begin(), v.end()
#define endl "\n"
// #define N 1000
#define C 4
#define nfs ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
// Ordered Set Tree
// typedef tree<PII, null_type, less<PII>,
// rb_tree_tag,
// tree_order_statistics_node_update>
// oset;
#define db(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cout << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cout.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
void computeLPSArray(string pat, int M, int *lps);
int dp[3003][3003][2];
int recurr(int *arr, int i, int j, bool turn) {
if (i > j)
return 0;
if (dp[i][j][turn] != -1)
return dp[i][j][turn];
if (turn == 1) {
dp[i][j][turn] = max(arr[i] + recurr(arr, i + 1, j, !turn),
arr[j] + recurr(arr, i, j - 1, !turn));
return dp[i][j][turn];
} else {
return dp[i][j][turn] =
min(recurr(arr, i + 1, j, !turn), recurr(arr, i, j - 1, !turn));
}
}
void solve() {
int n;
cin >> n;
int arr[n];
int sum = 0;
memset(dp, -1, sizeof(dp));
for (int i = 0; i < n; i++) {
cin >> arr[i];
sum += arr[i];
}
int scrx = recurr(arr, 0, n - 1, 1);
int scry = sum - scrx;
cout << scrx - scry << endl;
}
int32_t main() {
nfs;
int t;
// facto();
// cin >> t;
t = 1;
while (t--) {
solve();
}
return 0;
} | replace | 47 | 48 | 47 | 48 | 0 | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define REP(i, s, l) for (lli i = s; i < l; i++)
#define DEBUG 0
#define INF (1LL << 50)
lli dp[3010][3010];
lli vis[3010][3010];
lli n;
vector<lli> a;
lli f(lli l, lli r) {
if (l > r)
return 0;
if (vis[l][r])
return dp[l][r];
lli diff = n - (r - l + 1);
lli res;
if (diff % 2 == 0) {
res = -INF;
res = max(res, f(l + 1, r) + a[l]);
res = max(res, f(l, r - 1) + a[r]);
} else {
res = INF;
res = min(res, f(l + 1, r) - a[l]);
res = min(res, f(l, r - 1) - a[r]);
}
return dp[l][r] = res;
}
signed main() {
cin >> n;
a.resize(n);
REP(i, 0, n) cin >> a[i];
REP(i, 0, 3010) REP(j, 0, 3010) {
dp[i][j] = 0;
vis[i][j] = 0;
}
cout << f(0, n - 1) << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define REP(i, s, l) for (lli i = s; i < l; i++)
#define DEBUG 0
#define INF (1LL << 50)
lli dp[3010][3010];
lli vis[3010][3010];
lli n;
vector<lli> a;
lli f(lli l, lli r) {
if (l > r)
return 0;
if (vis[l][r])
return dp[l][r];
lli diff = n - (r - l + 1);
lli res;
if (diff % 2 == 0) {
res = -INF;
res = max(res, f(l + 1, r) + a[l]);
res = max(res, f(l, r - 1) + a[r]);
} else {
res = INF;
res = min(res, f(l + 1, r) - a[l]);
res = min(res, f(l, r - 1) - a[r]);
}
vis[l][r] = 1;
return dp[l][r] = res;
}
signed main() {
cin >> n;
a.resize(n);
REP(i, 0, n) cin >> a[i];
REP(i, 0, 3010) REP(j, 0, 3010) {
dp[i][j] = 0;
vis[i][j] = 0;
}
cout << f(0, n - 1) << endl;
return 0;
} | insert | 31 | 31 | 31 | 32 | TLE | |
p03171 | C++ | Runtime Error | /******************************************
* AUTHOR : HRITIK AGGARWAL *
******************************************/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100005
#define MOD 1000000007
#define dd double
#define vi vector<int>
#define vll vector<ll>
#define forr(i, n) for (int i = 0; i < n; i++)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep1(i, b) for (int i = 1; i <= b; i++)
#define pb push_back
#define mp make_pair
#define clr(x) x.clear()
#define sz(x) ((int)(x).size())
#define ms(s, n) memset(s, n, sizeof(s))
#define F first
#define S second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define int ll
ll po(ll a, ll x, ll m) {
if (x == 0) {
return 1;
}
ll ans = 1;
ll k = 1;
while (k <= x) {
if (x & k) {
ans = ((ans * a) % m);
}
k <<= 1;
a *= a;
a %= m;
}
return ans;
}
// DEKHIYE JI WALID JO THE ABBA HAMARE WO YEH function CHHOD KR GYE THE
int a[301];
int dp[301][301];
int rec(int i, int j) {
if (i == j) {
return a[i];
}
if (j == i + 1)
return max(a[i], a[j]);
if (i > j) {
return 0;
}
int &ans = dp[i][j];
if (ans != -1)
return ans;
ans = 0;
ans = max(a[i] + min(rec(i + 2, j), rec(i + 1, j - 1)),
a[j] + min(rec(i + 1, j - 1), rec(i, j - 2)));
return ans;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
;
int sum = 0;
forr(i, n) {
cin >> a[i];
sum += a[i];
}
ms(dp, -1);
int X = rec(0, n - 1);
int Y = sum - X;
int ans = X - Y;
cout << ans;
return 0;
} | /******************************************
* AUTHOR : HRITIK AGGARWAL *
******************************************/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100005
#define MOD 1000000007
#define dd double
#define vi vector<int>
#define vll vector<ll>
#define forr(i, n) for (int i = 0; i < n; i++)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep1(i, b) for (int i = 1; i <= b; i++)
#define pb push_back
#define mp make_pair
#define clr(x) x.clear()
#define sz(x) ((int)(x).size())
#define ms(s, n) memset(s, n, sizeof(s))
#define F first
#define S second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define int ll
ll po(ll a, ll x, ll m) {
if (x == 0) {
return 1;
}
ll ans = 1;
ll k = 1;
while (k <= x) {
if (x & k) {
ans = ((ans * a) % m);
}
k <<= 1;
a *= a;
a %= m;
}
return ans;
}
// DEKHIYE JI WALID JO THE ABBA HAMARE WO YEH function CHHOD KR GYE THE
int a[3001];
int dp[3001][3001];
int rec(int i, int j) {
if (i == j) {
return a[i];
}
if (j == i + 1)
return max(a[i], a[j]);
if (i > j) {
return 0;
}
int &ans = dp[i][j];
if (ans != -1)
return ans;
ans = 0;
ans = max(a[i] + min(rec(i + 2, j), rec(i + 1, j - 1)),
a[j] + min(rec(i + 1, j - 1), rec(i, j - 2)));
return ans;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
;
int sum = 0;
forr(i, n) {
cin >> a[i];
sum += a[i];
}
ms(dp, -1);
int X = rec(0, n - 1);
int Y = sum - X;
int ans = X - Y;
cout << ans;
return 0;
} | replace | 41 | 43 | 41 | 43 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 3;
long long dp[N][N][2];
int a[N];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a[i];
dp[i][i][0] = dp[i][i][1] = a[i];
}
for (int len = 1; len < n; ++len)
for (int st = 0; (st + len) < n; ++st) {
int ed = st + len;
dp[st][ed][0] = max(a[st] - dp[st + 1][ed][1], a[ed] - dp[st][ed - 1][1]);
dp[st][ed][1] = max(a[st] - dp[st + 1][ed][0], a[ed] - dp[st][ed - 1][0]);
}
cout << dp[0][n - 1][0];
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int N = 3e3 + 3;
long long dp[N][N][2];
int a[N];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a[i];
dp[i][i][0] = dp[i][i][1] = a[i];
}
for (int len = 1; len < n; ++len)
for (int st = 0; (st + len) < n; ++st) {
int ed = st + len;
dp[st][ed][0] = max(a[st] - dp[st + 1][ed][1], a[ed] - dp[st][ed - 1][1]);
dp[st][ed][1] = max(a[st] - dp[st + 1][ed][0], a[ed] - dp[st][ed - 1][0]);
}
cout << dp[0][n - 1][0];
return 0;
}
| replace | 2 | 3 | 2 | 3 | 0 | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(), x.end()
#define rep(i, n) for (int i = 0; i < n; i++)
#define debug(n, v) \
cout << #v << ":"; \
for (int i = 0; i < n; i++) \
cout << v[i] << " "; \
cout << endl;
#define INF 1000000000
#define mod 1000000007
typedef long long ll;
const ll LINF = 1001002003004005006ll;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
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;
}
int n;
ll a[3001];
ll dp[3001][3001];
bool f[3001][3001];
ll solve(int l, int r) {
if (f[l][r])
return dp[l][r];
if ((l + n - 1 - r) % 2 == 0) {
if (l == r)
return dp[l][r] = a[l];
return dp[l][r] = max(a[l] + solve(l + 1, r), a[r] + solve(l, r - 1));
} else {
if (l == r)
return dp[l][r] = -a[l];
return dp[l][r] = min(solve(l + 1, r) - a[l], solve(l, r - 1) - a[r]);
}
}
int main() {
cin >> n;
rep(i, n) { cin >> a[i]; }
solve(0, n - 1);
cout << dp[0][n - 1] << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(), x.end()
#define rep(i, n) for (int i = 0; i < n; i++)
#define debug(n, v) \
cout << #v << ":"; \
for (int i = 0; i < n; i++) \
cout << v[i] << " "; \
cout << endl;
#define INF 1000000000
#define mod 1000000007
typedef long long ll;
const ll LINF = 1001002003004005006ll;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
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;
}
int n;
ll a[3001];
ll dp[3001][3001];
bool f[3001][3001];
ll solve(int l, int r) {
if (f[l][r])
return dp[l][r];
f[l][r] = true;
if ((l + n - 1 - r) % 2 == 0) {
if (l == r)
return dp[l][r] = a[l];
return dp[l][r] = max(a[l] + solve(l + 1, r), a[r] + solve(l, r - 1));
} else {
if (l == r)
return dp[l][r] = -a[l];
return dp[l][r] = min(solve(l + 1, r) - a[l], solve(l, r - 1) - a[r]);
}
}
int main() {
cin >> n;
rep(i, n) { cin >> a[i]; }
solve(0, n - 1);
cout << dp[0][n - 1] << endl;
return 0;
} | insert | 37 | 37 | 37 | 38 | TLE | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep1(i, n) for (int i = (n); i > 0; i--)
#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>
#define MOD 1000000007
#define INF 1000000000000000LL
using namespace std;
#define MAXN 3000
ll dp[MAXN + 1][MAXN + 1];
int main() {
int n;
cin >> n;
vector<int> a(n);
rep(i, n) cin >> a[i];
bool f = n % 2;
rep(l, n) rep(i, n - l + 1) {
// n奇数かつ今見ている長さが奇数なら先手番
// n偶数かつ今見ている長さが偶数なら先手番
bool isFirst = (f && !(l % 2)) || (!f && (l % 2));
if (isFirst) {
dp[i][i + l] =
max<ll>(dp[i][i + l - 1] + a[i + l], dp[i + 1][i + l] + a[i]);
} else {
dp[i][i + l] =
min<ll>(dp[i][i + l - 1] - a[i + l], dp[i + 1][i + l] - a[i]);
}
}
cout << dp[0][n - 1] << endl;
return 0;
rep(i, n) {
rep(j, n) cout << dp[i][j] << ' ';
cout << endl;
}
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep1(i, n) for (int i = (n); i > 0; i--)
#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>
#define MOD 1000000007
#define INF 1000000000000000LL
using namespace std;
#define MAXN 4000
ll dp[MAXN + 1][MAXN + 1];
int main() {
int n;
cin >> n;
vector<int> a(n);
rep(i, n) cin >> a[i];
bool f = n % 2;
rep(l, n) rep(i, n - l + 1) {
// n奇数かつ今見ている長さが奇数なら先手番
// n偶数かつ今見ている長さが偶数なら先手番
bool isFirst = (f && !(l % 2)) || (!f && (l % 2));
if (isFirst) {
dp[i][i + l] =
max<ll>(dp[i][i + l - 1] + a[i + l], dp[i + 1][i + l] + a[i]);
} else {
dp[i][i + l] =
min<ll>(dp[i][i + l - 1] - a[i + l], dp[i + 1][i + l] - a[i]);
}
}
cout << dp[0][n - 1] << endl;
return 0;
rep(i, n) {
rep(j, n) cout << dp[i][j] << ' ';
cout << endl;
}
return 0;
}
| replace | 16 | 17 | 16 | 17 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vl;
typedef pair<ll, ll> pll;
#define MOD 1000000007
#define INF 1000000000
#define mp make_pair
#define pb push_back
#define ss second
#define ff first
#define endl '\n'
#define pl cout << endl;
#define tc \
int t; \
cin >> t; \
while (t--)
#define get cin >>
#define geta \
ll a[n]; \
for (int i = 0; i < n; ++i) { \
cin >> a[i]; \
}
#define puta \
for (int i = 0; i < n; ++i) { \
cout << a[i]; \
}
#define put cout <<
ll maxi = LLONG_MIN;
ll mini = LLONG_MAX;
void fast() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
int dp[301][301];
int main() {
fast();
memset(dp, 0, sizeof(dp));
int n;
cin >> n;
vector<ll> a(n + 1);
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int i = n; i >= 1; --i) {
dp[i][i] = a[i];
for (int j = i + 1; j <= n; ++j) {
dp[i][j] = max(a[j] - dp[i][j - 1], a[i] - dp[i + 1][j]);
}
}
cout << dp[1][n];
return 0;
}
// dp1[i][j] =maximum score the first one to make
// the move can obtain by using a[i] to a[j]
// dp2[i][j] =maximum score the 2nd one to make
// the move can obtain by using a[i] to a[j]
// dp1[i][i] for all i from 1 to n is a[i]
// dp2[i][i] = 0
// dp1[i][j]=max(a[i]+dp2[i+1][j],a[j]+dp2[i][j-1])
// if(a[i]+dp2[i+1][j]>a[j]+dp2[i][j-1])
// {
// dp2[i][j]=dp1[i+1][j];
// }
// else
// {
// dp2[i][j]=dp1[i][j-1];
// }
// What if dp1[i+1][j]>a[i]+dp2[i+1][j] and a[i]+dp2[i+1][j]>a[j]+dp2[i][j-1]
// and a[j]+dp2[i][j-1]>dp1[i][j-1] | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vl;
typedef pair<ll, ll> pll;
#define MOD 1000000007
#define INF 1000000000
#define mp make_pair
#define pb push_back
#define ss second
#define ff first
#define endl '\n'
#define pl cout << endl;
#define tc \
int t; \
cin >> t; \
while (t--)
#define get cin >>
#define geta \
ll a[n]; \
for (int i = 0; i < n; ++i) { \
cin >> a[i]; \
}
#define puta \
for (int i = 0; i < n; ++i) { \
cout << a[i]; \
}
#define put cout <<
ll maxi = LLONG_MIN;
ll mini = LLONG_MAX;
void fast() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
ll dp[3005][3005];
int main() {
fast();
memset(dp, 0, sizeof(dp));
int n;
cin >> n;
vector<ll> a(n + 1);
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int i = n; i >= 1; --i) {
dp[i][i] = a[i];
for (int j = i + 1; j <= n; ++j) {
dp[i][j] = max(a[j] - dp[i][j - 1], a[i] - dp[i + 1][j]);
}
}
cout << dp[1][n];
return 0;
}
// dp1[i][j] =maximum score the first one to make
// the move can obtain by using a[i] to a[j]
// dp2[i][j] =maximum score the 2nd one to make
// the move can obtain by using a[i] to a[j]
// dp1[i][i] for all i from 1 to n is a[i]
// dp2[i][i] = 0
// dp1[i][j]=max(a[i]+dp2[i+1][j],a[j]+dp2[i][j-1])
// if(a[i]+dp2[i+1][j]>a[j]+dp2[i][j-1])
// {
// dp2[i][j]=dp1[i+1][j];
// }
// else
// {
// dp2[i][j]=dp1[i][j-1];
// }
// What if dp1[i+1][j]>a[i]+dp2[i+1][j] and a[i]+dp2[i+1][j]>a[j]+dp2[i][j-1]
// and a[j]+dp2[i][j-1]>dp1[i][j-1] | replace | 36 | 37 | 36 | 37 | 0 | |
p03171 | C++ | Runtime Error | #include "bits/stdc++.h"
using namespace std;
#define show(x) cerr << #x << " = " << x << endl;
#define input_from_file freopen("input.txt", "r", stdin)
#define output_to_file freopen("output.txt", "w", stdout)
#define fastio ios_base::sync_with_stdio(0)
#define endl '\n'
const int N = 3e3 + 1;
long long dp[N][N][2], in[N], pre[N];
int main() {
#ifdef LOCAL
input_from_file;
output_to_file;
#endif
fastio;
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> in[i], pre[i] = pre[i - 1] + in[i];
for (int dif = 0; dif < n; ++dif) {
for (int i = 1; i <= n - dif; ++i) {
dp[i][i + dif][1] = max(dp[i + 1][i + dif][2] + in[i],
dp[i][i + dif - 1][2] + in[i + dif]);
dp[i][i + dif][2] = pre[i + dif] - pre[i - 1] - dp[i][i + dif][1];
// cerr << i << " "<< i+dif << " "<<dp[i][i+dif][1] << " "<<
// dp[i][i+dif][2] << endl;
}
}
cout << dp[1][n][1] - (pre[n] - dp[1][n][1]);
return 0;
} | #include "bits/stdc++.h"
using namespace std;
#define show(x) cerr << #x << " = " << x << endl;
#define input_from_file freopen("input.txt", "r", stdin)
#define output_to_file freopen("output.txt", "w", stdout)
#define fastio ios_base::sync_with_stdio(0)
#define endl '\n'
const int N = 3e3 + 5;
long long dp[N][N][2], in[N], pre[N];
int main() {
#ifdef LOCAL
input_from_file;
output_to_file;
#endif
fastio;
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> in[i], pre[i] = pre[i - 1] + in[i];
for (int dif = 0; dif < n; ++dif) {
for (int i = 1; i <= n - dif; ++i) {
dp[i][i + dif][1] = max(dp[i + 1][i + dif][2] + in[i],
dp[i][i + dif - 1][2] + in[i + dif]);
dp[i][i + dif][2] = pre[i + dif] - pre[i - 1] - dp[i][i + dif][1];
// cerr << i << " "<< i+dif << " "<<dp[i][i+dif][1] << " "<<
// dp[i][i+dif][2] << endl;
}
}
cout << dp[1][n][1] - (pre[n] - dp[1][n][1]);
return 0;
} | replace | 9 | 10 | 9 | 10 | -11 | |
p03171 | C++ | Runtime Error | // fahadmd16(IIEST Shibpur)
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define pf push_front
#define eb emplace_back
#define mp make_pair
#define ff first
#define ss second
#define db double
#define pll pair<ll, ll>
#define ALL(a) (a).begin(), (a).end()
#define endl "\n"
#define forn(i, n) for (ll i = 0; i < n; i++)
#define forn1(i, n) for (ll i = 1; i < n; i++)
#define ford(i, n) for (ll i = n - 1; i >= 0; i--)
#define ford1(i, n) for (ll i = n - 1; i >= 1; i--)
#define forab(i, a, b) for (ll i = a; i < b; i++)
#define forabd(i, a, b) for (ll i = a; i >= b; i--)
#define eps 1e-10
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define IOS \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define trace1(x) cout << #x << ": " << x << endl
#define trace2(x, y) cout << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z) \
cout << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " \
<< z << endl
#define trace4(a, b, c, d) \
cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e) \
cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define trace6(a, b, c, d, e, f) \
cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " \
<< #f << ": " << f << endl
#define tracen(x, n) \
{ \
cout << (#x) << ":"; \
for (int i = 0; i < n; i++) \
cout << (#x) << "[" << i << "]:" << x[i] << " "; \
cout << endl; \
}
#define tracenm(x, n, m) \
{ \
cout << (#x) << endl; \
for (int i = 0; i < n; i++) { \
cout << "Row #" << i << ":"; \
for (int j = 0; j < m; j++) \
cout << x[i][j] << " "; \
cout << endl; \
} \
}
#define trace(v) \
{ \
cout << #v << ": "; \
for (auto it = v.begin(); it != v.end(); it++) \
cout << *it << " "; \
cout << endl; \
}
using namespace std;
const ll sz = 3e3 + 2;
const ll mod = 1e9 + 7;
const db PI = acos(-1);
typedef vector<ll> VL;
typedef vector<pll> VLL;
typedef vector<vector<ll>> VVL;
ll power(ll n, ll k, ll m) {
ll ans = 1;
while (k) {
if (k & 1)
ans = (ans * n) % m;
k /= 2;
n = (n * n) % m;
}
return ans;
}
ll dp[sz][sz][2]; // dp[i][j][type] -- optimal way to play the game with values
// from i,j and type denotes the turn of the player.
ll n;
ll a[sz];
void solver() {
cin >> n;
forn(i, n) cin >> a[i];
forab(L, 1, n + 1) {
forn1(i, n + 2 - L) {
ll j = i + L - 1;
if (i == j) {
dp[i][j][0] = a[i - 1];
dp[i][j][1] = -a[i - 1];
} else {
dp[i][j][0] = dp[i + 1][j][1] + a[i - 1];
dp[i][j][0] = max(dp[i][j][0], dp[i][j - 1][1] + a[j - 1]);
dp[i][j][1] = dp[i + 1][j][0] - a[i - 1];
dp[i][j][1] = min(dp[i][j][1], dp[i][j - 1][0] - a[j - 1]);
}
}
}
cout << dp[1][n][0] << endl;
// forn(i, n + 1) {
// forn(j, n + 1) {
// trace4(i, j, dp[i][j][0], dp[i][j][1]);
// }
// }
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
IOS;
ll T = 1; // cin >> T;
forn1(i, T + 1) {
clock_t z = clock();
solver();
debug("Total Time: %.3f\n", (double)(clock() - z) / CLOCKS_PER_SEC);
}
return 0;
} | // fahadmd16(IIEST Shibpur)
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define pf push_front
#define eb emplace_back
#define mp make_pair
#define ff first
#define ss second
#define db double
#define pll pair<ll, ll>
#define ALL(a) (a).begin(), (a).end()
#define endl "\n"
#define forn(i, n) for (ll i = 0; i < n; i++)
#define forn1(i, n) for (ll i = 1; i < n; i++)
#define ford(i, n) for (ll i = n - 1; i >= 0; i--)
#define ford1(i, n) for (ll i = n - 1; i >= 1; i--)
#define forab(i, a, b) for (ll i = a; i < b; i++)
#define forabd(i, a, b) for (ll i = a; i >= b; i--)
#define eps 1e-10
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define IOS \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define trace1(x) cout << #x << ": " << x << endl
#define trace2(x, y) cout << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z) \
cout << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " \
<< z << endl
#define trace4(a, b, c, d) \
cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e) \
cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define trace6(a, b, c, d, e, f) \
cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " \
<< #f << ": " << f << endl
#define tracen(x, n) \
{ \
cout << (#x) << ":"; \
for (int i = 0; i < n; i++) \
cout << (#x) << "[" << i << "]:" << x[i] << " "; \
cout << endl; \
}
#define tracenm(x, n, m) \
{ \
cout << (#x) << endl; \
for (int i = 0; i < n; i++) { \
cout << "Row #" << i << ":"; \
for (int j = 0; j < m; j++) \
cout << x[i][j] << " "; \
cout << endl; \
} \
}
#define trace(v) \
{ \
cout << #v << ": "; \
for (auto it = v.begin(); it != v.end(); it++) \
cout << *it << " "; \
cout << endl; \
}
using namespace std;
const ll sz = 3e3 + 2;
const ll mod = 1e9 + 7;
const db PI = acos(-1);
typedef vector<ll> VL;
typedef vector<pll> VLL;
typedef vector<vector<ll>> VVL;
ll power(ll n, ll k, ll m) {
ll ans = 1;
while (k) {
if (k & 1)
ans = (ans * n) % m;
k /= 2;
n = (n * n) % m;
}
return ans;
}
ll dp[sz][sz][2]; // dp[i][j][type] -- optimal way to play the game with values
// from i,j and type denotes the turn of the player.
ll n;
ll a[sz];
void solver() {
cin >> n;
forn(i, n) cin >> a[i];
forab(L, 1, n + 1) {
forn1(i, n + 2 - L) {
ll j = i + L - 1;
if (i == j) {
dp[i][j][0] = a[i - 1];
dp[i][j][1] = -a[i - 1];
} else {
dp[i][j][0] = dp[i + 1][j][1] + a[i - 1];
dp[i][j][0] = max(dp[i][j][0], dp[i][j - 1][1] + a[j - 1]);
dp[i][j][1] = dp[i + 1][j][0] - a[i - 1];
dp[i][j][1] = min(dp[i][j][1], dp[i][j - 1][0] - a[j - 1]);
}
}
}
cout << dp[1][n][0] << endl;
// forn(i, n + 1) {
// forn(j, n + 1) {
// trace4(i, j, dp[i][j][0], dp[i][j][1]);
// }
// }
}
int main() {
IOS;
ll T = 1; // cin >> T;
forn1(i, T + 1) {
clock_t z = clock();
solver();
debug("Total Time: %.3f\n", (double)(clock() - z) / CLOCKS_PER_SEC);
}
return 0;
} | replace | 124 | 128 | 124 | 125 | -11 | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(a, n) for (ll a = 0; a < (n); ++a)
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<vector<ll>> Graph;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const ll INF = 1e18;
ll dp[3010][3010];
vector<ll> a;
ll n;
ll rec(ll i, ll j) {
if (i == j) {
dp[i][i] = 0;
return 0;
}
ll len = j - i;
if ((n - len) % 2 == 0) {
dp[i][j] = rec(i + 1, j) + a[i];
chmax(dp[i][j], rec(i, j - 1) + a[j - 1]);
if (j < n)
chmax(dp[i][j], rec(i, j - 1) + a[j - 1]);
} else {
dp[i][j] = rec(i + 1, j) - a[i];
chmin(dp[i][j], rec(i, j - 1) - a[j - 1]);
if (j < n)
chmin(dp[i][j], rec(i, j - 1) - a[j - 1]);
}
return dp[i][j];
}
int main() {
cin >> n;
a.resize(n);
rep(i, n) cin >> a[i];
rep(i, 3010) rep(j, 3010) dp[i][j] = 0;
cout << rec(0, n) << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(a, n) for (ll a = 0; a < (n); ++a)
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<vector<ll>> Graph;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const ll INF = 1e18;
ll dp[3010][3010];
vector<ll> a;
ll n;
ll rec(ll i, ll j) {
if (dp[i][j] != 0)
return dp[i][j];
if (i == j) {
dp[i][i] = 0;
return 0;
}
ll len = j - i;
if ((n - len) % 2 == 0) {
dp[i][j] = rec(i + 1, j) + a[i];
chmax(dp[i][j], rec(i, j - 1) + a[j - 1]);
if (j < n)
chmax(dp[i][j], rec(i, j - 1) + a[j - 1]);
} else {
dp[i][j] = rec(i + 1, j) - a[i];
chmin(dp[i][j], rec(i, j - 1) - a[j - 1]);
if (j < n)
chmin(dp[i][j], rec(i, j - 1) - a[j - 1]);
}
return dp[i][j];
}
int main() {
cin >> n;
a.resize(n);
rep(i, n) cin >> a[i];
rep(i, 3010) rep(j, 3010) dp[i][j] = 0;
cout << rec(0, n) << endl;
return 0;
} | insert | 28 | 28 | 28 | 30 | TLE | |
p03171 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
// vector push_back push front top empty pop make_pair long long insert begin
// end
typedef long long ll;
typedef vector<int> vi;
typedef vector<pair<int, int>> vpi;
typedef vector<long long> vll;
typedef pair<int, int> pi;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define B begin()
#define RB rbegin()
#define E end()
#define RE rend()
#define Z size()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define L length()
#define show(a) cerr << " *** " << a << endl;
#define show1(a) cerr << " /// " << a << endl;
#define valid(a, b, c) (a >= b && a < c ? 1 : 0)
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
const int mod = (int)1e9 + 7;
ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
ll egcd(ll a, ll b, ll &x, ll &y) {
if (a == 0) {
x = 0;
y = 1;
return b;
}
ll x1, y1;
ll d = egcd(b % a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
return d;
}
ll mbinp(ll a, ll b) {
a %= mod;
if (b == 0)
return 1;
ll ans = mbinp(a, b / 2);
ll tmp = (ans * ans) % mod;
if (b % 2)
return ((tmp * a) % mod);
return ((tmp) % mod);
}
ll binp(ll a, ll b) {
if (b == 0)
return 1;
ll ans = binp(a, b / 2);
ll tmp = (ans * ans);
if (b % 2)
return ((tmp * a));
return ((tmp));
}
const int maax = 3050;
ll dp[maax][maax];
ll tab[maax];
int n;
ll solve(int i, int j) {
if (j - i == 1)
return (abs(tab[i] - tab[j]));
if (dp[i][j] != -1)
return dp[i][j];
ll tmp = 0;
tmp = tab[i] - solve(i + 1, j);
tmp = max(tmp, tab[j] - solve(i, j - 1));
return (dp[i][j] = tmp);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
memset(dp, -1, sizeof(dp));
REP(i, 0, n)
cin >> tab[i];
ll res = solve(0, n - 1);
cout << res;
}
| #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
// vector push_back push front top empty pop make_pair long long insert begin
// end
typedef long long ll;
typedef vector<int> vi;
typedef vector<pair<int, int>> vpi;
typedef vector<long long> vll;
typedef pair<int, int> pi;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define B begin()
#define RB rbegin()
#define E end()
#define RE rend()
#define Z size()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define L length()
#define show(a) cerr << " *** " << a << endl;
#define show1(a) cerr << " /// " << a << endl;
#define valid(a, b, c) (a >= b && a < c ? 1 : 0)
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
const int mod = (int)1e9 + 7;
ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
ll egcd(ll a, ll b, ll &x, ll &y) {
if (a == 0) {
x = 0;
y = 1;
return b;
}
ll x1, y1;
ll d = egcd(b % a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
return d;
}
ll mbinp(ll a, ll b) {
a %= mod;
if (b == 0)
return 1;
ll ans = mbinp(a, b / 2);
ll tmp = (ans * ans) % mod;
if (b % 2)
return ((tmp * a) % mod);
return ((tmp) % mod);
}
ll binp(ll a, ll b) {
if (b == 0)
return 1;
ll ans = binp(a, b / 2);
ll tmp = (ans * ans);
if (b % 2)
return ((tmp * a));
return ((tmp));
}
const int maax = 3050;
ll dp[maax][maax];
ll tab[maax];
int n;
ll solve(int i, int j) {
if (j - i == 0)
return tab[i];
if (j - i == 1)
return (abs(tab[i] - tab[j]));
if (dp[i][j] != -1)
return dp[i][j];
ll tmp = 0;
tmp = tab[i] - solve(i + 1, j);
tmp = max(tmp, tab[j] - solve(i, j - 1));
return (dp[i][j] = tmp);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
memset(dp, -1, sizeof(dp));
REP(i, 0, n)
cin >> tab[i];
ll res = solve(0, n - 1);
cout << res;
}
| insert | 77 | 77 | 77 | 79 | 0 | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
ll dp[3005][3005];
bool solved[3005][3005];
int l[3005];
int n;
ll solve(int lhs, int rhs) {
if (lhs == rhs) {
return l[lhs];
}
if (solved[lhs][rhs])
return dp[lhs][rhs];
dp[lhs][rhs] =
max(l[lhs] - solve(lhs + 1, rhs), l[rhs] - solve(lhs, rhs - 1));
return dp[lhs][rhs];
}
void solve() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> l[i];
}
cout << solve(0, n - 1) << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
solve();
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
ll dp[3005][3005];
bool solved[3005][3005];
int l[3005];
int n;
ll solve(int lhs, int rhs) {
if (lhs == rhs) {
return l[lhs];
}
if (solved[lhs][rhs])
return dp[lhs][rhs];
solved[lhs][rhs] = true;
dp[lhs][rhs] =
max(l[lhs] - solve(lhs + 1, rhs), l[rhs] - solve(lhs, rhs - 1));
return dp[lhs][rhs];
}
void solve() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> l[i];
}
cout << solve(0, n - 1) << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
solve();
}
| insert | 18 | 18 | 18 | 19 | TLE | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ops cout << "ops" << endl;
#define freopens \
freopen("cowpatibility.in", "r", stdin); \
freopen("cowpatibility.out", "w", stdout);
#define fast \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define DIM 3007
#define DIMM 100007
#define INF 1000000000007.0
#define X (ll)(1 << 32)
#define eps 0.0000001
#define PI 3.14159265358979323846
#define MAX 25
#define MODULO (long long)1000000007
const long double gr = (1 + sqrt(5)) / 2;
typedef int I;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<I, I> pII;
typedef pair<ll, ll> pllll;
typedef pair<ld, ld> pldld;
typedef vector<I> vI;
typedef vector<ll> vll;
typedef vector<pllll> vpllll;
typedef char cr;
typedef string str;
ll n;
ll a[DIM], dp[DIM][DIM];
int main() {
// ops;
// freopens;
fast;
// ll x1,y1,xs,ys,x2,y2,x3,x4,y3,y4;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int l = 1; l <= n; l++) {
for (int s = 1; s <= (n - s + 1); s++) {
dp[s][l] = max((a[s + l - 1] - dp[s][l - 1]), (a[s] - dp[s + 1][l - 1]));
}
}
cout << dp[1][n] << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ops cout << "ops" << endl;
#define freopens \
freopen("cowpatibility.in", "r", stdin); \
freopen("cowpatibility.out", "w", stdout);
#define fast \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define DIM 3007
#define DIMM 100007
#define INF 1000000000007.0
#define X (ll)(1 << 32)
#define eps 0.0000001
#define PI 3.14159265358979323846
#define MAX 25
#define MODULO (long long)1000000007
const long double gr = (1 + sqrt(5)) / 2;
typedef int I;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<I, I> pII;
typedef pair<ll, ll> pllll;
typedef pair<ld, ld> pldld;
typedef vector<I> vI;
typedef vector<ll> vll;
typedef vector<pllll> vpllll;
typedef char cr;
typedef string str;
ll n;
ll a[DIM], dp[DIM][DIM];
int main() {
// ops;
// freopens;
fast;
// ll x1,y1,xs,ys,x2,y2,x3,x4,y3,y4;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int l = 1; l <= n; l++) {
for (int s = 1; s <= (n - l + 1); s++) {
dp[s][l] = max((a[s + l - 1] - dp[s][l - 1]), (a[s] - dp[s + 1][l - 1]));
}
}
cout << dp[1][n] << endl;
return 0;
}
| replace | 54 | 55 | 54 | 55 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sc scanf
#define scin(x) sc("%d", &(x))
#define scln(x) sc("%lld", &(x))
#define pf printf
#define ms(a, b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define mp make_pair
#define db double
#define EPS 10E-10
#define ff first
#define ss second
#define sqr(x) (x) * (x)
#define vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define vll vector<vector<ll>>
#define DBG pf("HI\n")
#define MOD 1000000007
#define CIN \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define RUN_CASE(t, T) for (__typeof(t) t = 1; t <= T; t++)
#define CASE(t) printf("Case %d: ", t)
#define CASEL(t) printf("Case %d:\n", t)
#define intlimit 2147483648
#define longlimit 9223372036854775808
#define infinity (1 << 28)
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) ((a) * (b) / gcd(a, b))
#define mx 1234567899
#define PI 2 * acos(0.0)
#define all(v) v.begin(), v.end()
#define rep(i, a, b) for (__typeof(i) i = a; i <= b; i++)
#define rev(i, a, b) for (__typeof(i) i = a; i >= b; i--)
using namespace std;
template <typename T> inline T POW(T B, T P) {
if (P == 0)
return 1;
if (P & 1)
return B * POW(B, P - 1);
else
return sqr(POW(B, P / 2));
}
ll Bigmod(ll base, ll power, ll mod) {
if (power == 0)
return 1 % mod;
if (power == 1)
return base % mod;
ll x = Bigmod(base, power / 2, mod);
x = (x * x) % mod;
if (power % 2) {
x = (x * (base % mod)) % mod;
}
return x;
}
ll Mulmod(ll base, ll power, ll mod) {
if (power == 0)
return 0;
if (power == 1)
return base % mod;
ll x = Mulmod(base, power / 2, mod);
x = (x + x) % mod;
if (power % 2) {
x = (x + (base % mod)) % mod;
}
return x;
}
ll BigMulmod(ll base, ll power, ll mod) {
if (power == 0)
return 1 % mod;
if (power == 1)
return base % mod;
ll x = BigMulmod(base, power / 2, mod);
x = Mulmod(x, x, mod);
if (power % 2) {
x = Mulmod(x, (base % mod), mod);
}
return x;
}
bool isVowel(char ch) {
ch = toupper(ch);
if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E')
return true;
return false;
}
ll ModInverse(ll number, ll mod) { return Bigmod(number, mod - 2, mod); }
bool isConso(char ch) {
if (isalpha(ch) && !isVowel(ch))
return true;
return false;
}
int toInt(string s) {
int sm;
stringstream ss(s);
ss >> sm;
return sm;
}
bool isPrime(ll val) {
if (val == 2)
return true;
if (val % 2 == 0 || val == 1)
return false;
ll sqrt_N = (ll)((double)sqrt(val));
for (ll i = 3; i <= sqrt_N; i += 2) {
if (val % i == 0)
return false;
}
return true;
}
template <class T> string convert(T _input) {
stringstream blah;
blah << _input;
return blah.str();
}
/**
// Ordered set
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<typename T> using orderset =
tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
//orderset<int> s ; //orderset<int>::iterator it ;
//orderset<int> X; //X.insert(1); //X.insert(2); //X.insert(4); //X.insert(8);
//X.insert(16);
//cout<<*X.find_by_order(1)<<endl; // 2 //cout<<*X.find_by_order(2)<<endl; // 4
//cout<<*X.find_by_order(4)<<endl; // 16
//cout<<(end(X)==X.find_by_order(6))<<endl; // true
//cout<<X.order_of_key(-5)<<endl; // 0 //cout<<X.order_of_key(1)<<endl; // 0
//cout<<X.order_of_key(3)<<endl; // 2 //cout<<X.order_of_key(4)<<endl; // 2
//cout<<X.order_of_key(400)<<endl; // 5
*/
bool cmp(ll x, ll y) { return x > y; }
ll ar[3005];
ll dp[3005][3005];
int main() {
int n, k;
scin(n);
for (int i = 0; i < n; i++)
scln(ar[i]);
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
if (i == j)
dp[i][j] = ar[i];
else
dp[i][j] = max(ar[i] - dp[i - 1][j], ar[j] - dp[i][j - 1]);
}
}
pf("%lld\n", dp[0][n - 1]);
return 0;
}
| #include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sc scanf
#define scin(x) sc("%d", &(x))
#define scln(x) sc("%lld", &(x))
#define pf printf
#define ms(a, b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define mp make_pair
#define db double
#define EPS 10E-10
#define ff first
#define ss second
#define sqr(x) (x) * (x)
#define vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define vll vector<vector<ll>>
#define DBG pf("HI\n")
#define MOD 1000000007
#define CIN \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define RUN_CASE(t, T) for (__typeof(t) t = 1; t <= T; t++)
#define CASE(t) printf("Case %d: ", t)
#define CASEL(t) printf("Case %d:\n", t)
#define intlimit 2147483648
#define longlimit 9223372036854775808
#define infinity (1 << 28)
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) ((a) * (b) / gcd(a, b))
#define mx 1234567899
#define PI 2 * acos(0.0)
#define all(v) v.begin(), v.end()
#define rep(i, a, b) for (__typeof(i) i = a; i <= b; i++)
#define rev(i, a, b) for (__typeof(i) i = a; i >= b; i--)
using namespace std;
template <typename T> inline T POW(T B, T P) {
if (P == 0)
return 1;
if (P & 1)
return B * POW(B, P - 1);
else
return sqr(POW(B, P / 2));
}
ll Bigmod(ll base, ll power, ll mod) {
if (power == 0)
return 1 % mod;
if (power == 1)
return base % mod;
ll x = Bigmod(base, power / 2, mod);
x = (x * x) % mod;
if (power % 2) {
x = (x * (base % mod)) % mod;
}
return x;
}
ll Mulmod(ll base, ll power, ll mod) {
if (power == 0)
return 0;
if (power == 1)
return base % mod;
ll x = Mulmod(base, power / 2, mod);
x = (x + x) % mod;
if (power % 2) {
x = (x + (base % mod)) % mod;
}
return x;
}
ll BigMulmod(ll base, ll power, ll mod) {
if (power == 0)
return 1 % mod;
if (power == 1)
return base % mod;
ll x = BigMulmod(base, power / 2, mod);
x = Mulmod(x, x, mod);
if (power % 2) {
x = Mulmod(x, (base % mod), mod);
}
return x;
}
bool isVowel(char ch) {
ch = toupper(ch);
if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E')
return true;
return false;
}
ll ModInverse(ll number, ll mod) { return Bigmod(number, mod - 2, mod); }
bool isConso(char ch) {
if (isalpha(ch) && !isVowel(ch))
return true;
return false;
}
int toInt(string s) {
int sm;
stringstream ss(s);
ss >> sm;
return sm;
}
bool isPrime(ll val) {
if (val == 2)
return true;
if (val % 2 == 0 || val == 1)
return false;
ll sqrt_N = (ll)((double)sqrt(val));
for (ll i = 3; i <= sqrt_N; i += 2) {
if (val % i == 0)
return false;
}
return true;
}
template <class T> string convert(T _input) {
stringstream blah;
blah << _input;
return blah.str();
}
/**
// Ordered set
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<typename T> using orderset =
tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
//orderset<int> s ; //orderset<int>::iterator it ;
//orderset<int> X; //X.insert(1); //X.insert(2); //X.insert(4); //X.insert(8);
//X.insert(16);
//cout<<*X.find_by_order(1)<<endl; // 2 //cout<<*X.find_by_order(2)<<endl; // 4
//cout<<*X.find_by_order(4)<<endl; // 16
//cout<<(end(X)==X.find_by_order(6))<<endl; // true
//cout<<X.order_of_key(-5)<<endl; // 0 //cout<<X.order_of_key(1)<<endl; // 0
//cout<<X.order_of_key(3)<<endl; // 2 //cout<<X.order_of_key(4)<<endl; // 2
//cout<<X.order_of_key(400)<<endl; // 5
*/
bool cmp(ll x, ll y) { return x > y; }
ll ar[3005];
ll dp[3005][3005];
int main() {
int n, k;
scin(n);
for (int i = 0; i < n; i++)
scln(ar[i]);
for (int i = 0; i < n; i++)
dp[i][i] = ar[i];
for (int i = n - 2; i >= 0; i--) {
for (int j = i + 1; j < n; j++) {
dp[i][j] = max(ar[i] - dp[i + 1][j], ar[j] - dp[i][j - 1]);
}
}
pf("%lld\n", dp[0][n - 1]);
return 0;
}
| replace | 151 | 157 | 151 | 156 | 0 | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
int n;
int a[3005];
const ll INF = 1e18;
// dp[i][j] 区間[i,j)におけるX-Yの値
ll dp[3005][3005];
ll solve(int i, int j) {
if (dp[i][j] >= 0)
return dp[i][j];
if (j - i == 0)
return dp[i][j] = 0;
if ((n - (j - i)) % 2 == 0) {
// 先手
dp[i][j] = max(solve(i + 1, j) + a[i], solve(i, j - 1) + a[j - 1]);
} else {
// 後手
dp[i][j] = min(solve(i + 1, j) - a[i], solve(i, j - 1) - a[j - 1]);
}
return dp[i][j];
}
int main() {
cin >> n;
rep(i, n) cin >> a[i];
rep(i, 3005) rep(j, 3005) dp[i][j] = -INF;
ll ans = solve(0, n);
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
int n;
int a[3005];
const ll INF = 1e18;
// dp[i][j] 区間[i,j)におけるX-Yの値
ll dp[3005][3005];
ll solve(int i, int j) {
if (dp[i][j] != -INF)
return dp[i][j];
if (j - i == 0)
return dp[i][j] = 0;
if ((n - (j - i)) % 2 == 0) {
// 先手
dp[i][j] = max(solve(i + 1, j) + a[i], solve(i, j - 1) + a[j - 1]);
} else {
// 後手
dp[i][j] = min(solve(i + 1, j) - a[i], solve(i, j - 1) - a[j - 1]);
}
return dp[i][j];
}
int main() {
cin >> n;
rep(i, n) cin >> a[i];
rep(i, 3005) rep(j, 3005) dp[i][j] = -INF;
ll ans = solve(0, n);
cout << ans << endl;
return 0;
}
| replace | 13 | 14 | 13 | 14 | TLE | |
p03171 | C++ | Runtime Error | #include "bits/stdc++.h"
using namespace std;
#define fi first
#define se second
#define pb push_back
#define int long long
#define all(x) (x).begin(), (x).end()
using LL = long long;
using LD = long double;
using pii = pair<int, int>;
using vii = vector<pii>;
const int INF = 1e18;
const int MOD = 1e9 + 7;
const int N = 1e3 + 5;
int n, a[N];
int dp[N][N];
int solve(int l, int r) {
if (l == r) {
return a[l]; // or a[r]
}
if (dp[l][r] != -1) {
return dp[l][r];
}
int ans1 = a[l] - solve(l + 1, r);
int ans2 = a[r] - solve(l, r - 1);
return dp[l][r] = max(ans1, ans2);
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
dp[i][j] = -1;
}
}
cout << solve(0, n - 1) << "\n";
return 0;
}
| #include "bits/stdc++.h"
using namespace std;
#define fi first
#define se second
#define pb push_back
#define int long long
#define all(x) (x).begin(), (x).end()
using LL = long long;
using LD = long double;
using pii = pair<int, int>;
using vii = vector<pii>;
const int INF = 1e18;
const int MOD = 1e9 + 7;
const int N = 3e3 + 5;
int n, a[N];
int dp[N][N];
int solve(int l, int r) {
if (l == r) {
return a[l]; // or a[r]
}
if (dp[l][r] != -1) {
return dp[l][r];
}
int ans1 = a[l] - solve(l + 1, r);
int ans2 = a[r] - solve(l, r - 1);
return dp[l][r] = max(ans1, ans2);
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
dp[i][j] = -1;
}
}
cout << solve(0, n - 1) << "\n";
return 0;
}
| replace | 16 | 17 | 16 | 17 | 0 | |
p03171 | C++ | Runtime Error | // ----- In the name of ALLAH, the Most Gracious, the Most Merciful -----
#include <bits/stdc++.h>
using namespace std;
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x) \
sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<( \
c i) {
sim > struct rge {
c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i;
ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c &) { ris; }
#endif
}
;
#define deb(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define memo(a, val) memset(a, val, sizeof(a))
#define NINJA \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define all(a) a.begin(), a.end()
#define fo(i, n) for (int i = 0; i < n; i++)
#define Fo(i, k, n) for (int i = k; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define dloop(i, n) for (int i = n - 1; i >= 0; i--)
#define iii tuple<int, int, int>
#define vi vector<int>
#define ii pair<int, int>
#define vii vector<ii>
#define int long long
#define ld long double
#define pb push_back
#define endl "\n"
#define setbits __builtin_popcountll
#define mp map<int, int>
#define F first
#define S second
#define sz(v) (int)v.size()
#define mod 1000000007
#define inf (int)1e18
const int mxn = 3010;
int a[mxn];
int dp[mxn][mxn][2];
int32_t main() {
NINJA;
int n;
cin >> n;
int sum = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
for (int i = 0; i < n; ++i) {
dp[i][i][0] = a[i];
dp[i][i][1] = 0;
}
for (int RminusL = 1; RminusL < n; RminusL++) {
for (int L = 0; L < n; ++L) {
int R = L + RminusL;
dp[L][R][0] = max(a[L] + dp[L + 1][R][1], a[R] + dp[L][R - 1][1]);
dp[L][R][1] = min(dp[L + 1][R][0], dp[L][R - 1][0]);
}
}
cout << 2 * dp[0][n - 1][0] - sum << endl;
return 0;
} | // ----- In the name of ALLAH, the Most Gracious, the Most Merciful -----
#include <bits/stdc++.h>
using namespace std;
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x) \
sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<( \
c i) {
sim > struct rge {
c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i;
ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c &) { ris; }
#endif
}
;
#define deb(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define memo(a, val) memset(a, val, sizeof(a))
#define NINJA \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define all(a) a.begin(), a.end()
#define fo(i, n) for (int i = 0; i < n; i++)
#define Fo(i, k, n) for (int i = k; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define dloop(i, n) for (int i = n - 1; i >= 0; i--)
#define iii tuple<int, int, int>
#define vi vector<int>
#define ii pair<int, int>
#define vii vector<ii>
#define int long long
#define ld long double
#define pb push_back
#define endl "\n"
#define setbits __builtin_popcountll
#define mp map<int, int>
#define F first
#define S second
#define sz(v) (int)v.size()
#define mod 1000000007
#define inf (int)1e18
const int mxn = 3010;
int a[mxn];
int dp[mxn][mxn][2];
int32_t main() {
NINJA;
int n;
cin >> n;
int sum = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
for (int i = 0; i < n; ++i) {
dp[i][i][0] = a[i];
dp[i][i][1] = 0;
}
for (int RminusL = 1; RminusL < n; RminusL++) {
for (int L = 0; L + RminusL < n; ++L) {
int R = L + RminusL;
dp[L][R][0] = max(a[L] + dp[L + 1][R][1], a[R] + dp[L][R - 1][1]);
dp[L][R][1] = min(dp[L + 1][R][0], dp[L][R - 1][0]);
}
}
cout << 2 * dp[0][n - 1][0] - sum << endl;
return 0;
} | replace | 88 | 89 | 88 | 89 | -11 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long *a = new long long[n];
for (int i = 0; i < n; i++)
cin >> a[i];
long long dp[n][n];
// memset(dp,0,sizeof(dp));
for (int l = n - 1; l >= 0; l--) {
for (int r = l; r < n; r++) {
if (l == r)
dp[l][r] = a[l];
dp[l][r] = max(a[l] - dp[l + 1][r], a[r] - dp[l][r - 1]);
}
}
// for(int i=0;i<n;i++)
// {
// for(int j=0;j<n;j++)
// cout<<dp[i][j]<<"\t";
// cout<<endl;
// }
cout << dp[0][n - 1];
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long *a = new long long[n];
for (int i = 0; i < n; i++)
cin >> a[i];
long long dp[n][n];
// memset(dp,0,sizeof(dp));
for (int l = n - 1; l >= 0; l--) {
for (int r = l; r < n; r++) {
if (l == r)
dp[l][r] = a[l];
else
dp[l][r] = max(a[l] - dp[l + 1][r], a[r] - dp[l][r - 1]);
}
}
// for(int i=0;i<n;i++)
// {
// for(int j=0;j<n;j++)
// cout<<dp[i][j]<<"\t";
// cout<<endl;
// }
cout << dp[0][n - 1];
} | replace | 14 | 15 | 14 | 16 | 0 | |
p03171 | C++ | Runtime Error | #include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
long long int grr[3000][3000];
long long int compute(long long int arr[], long long int start,
long long int end) {
if (grr[start][end] != 0)
return grr[start][end];
if (start > end)
return 0;
if (start == end) {
grr[start][end] = arr[start];
return grr[start][end];
}
grr[start][end] = max(arr[start] + min(compute(arr, start + 2, end),
compute(arr, start + 1, end - 1)),
arr[end] + min(compute(arr, start, end - 2),
compute(arr, start + 1, end - 1)));
return grr[start][end];
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int n;
cin >> n;
long long int arr[n];
long long int a = 0, b = 0;
for (long long int i = 0; i < n; i++) {
cin >> arr[i];
b += arr[i];
}
a = compute(arr, 0, n - 1);
cout << a - (b - a) << endl;
return 0;
}
| #include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
long long int grr[3005][3005];
long long int compute(long long int arr[], long long int start,
long long int end) {
if (grr[start][end] != 0)
return grr[start][end];
if (start > end)
return 0;
if (start == end) {
grr[start][end] = arr[start];
return grr[start][end];
}
grr[start][end] = max(arr[start] + min(compute(arr, start + 2, end),
compute(arr, start + 1, end - 1)),
arr[end] + min(compute(arr, start, end - 2),
compute(arr, start + 1, end - 1)));
return grr[start][end];
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int n;
cin >> n;
long long int arr[n];
long long int a = 0, b = 0;
for (long long int i = 0; i < n; i++) {
cin >> arr[i];
b += arr[i];
}
a = compute(arr, 0, n - 1);
cout << a - (b - a) << endl;
return 0;
}
| replace | 12 | 13 | 12 | 13 | -11 | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
ll dp[3001][3001];
ll fun(int x, int y, int *a) {
if (x > y)
return 0;
if (x == y)
return a[x];
if (dp[x][y] > -1)
return dp[x][y];
ll X = a[x] + min(fun(x + 2, y, a), fun(x + 1, y - 1, a));
ll Y = a[y] + min(fun(x + 1, y - 1, a), fun(x, y - 2, a));
ll ans = max(X, Y);
return ans;
}
void solve() {
int n;
cin >> n;
int a[n];
ll sum = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
memset(dp, -1, sizeof dp);
ll X = fun(0, n - 1, a);
cout << X - (sum - X);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
t = 1;
while (t--) {
solve();
cout << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
ll dp[3001][3001];
ll fun(int x, int y, int *a) {
if (x > y)
return 0;
if (x == y)
return a[x];
if (dp[x][y] > -1)
return dp[x][y];
ll X = a[x] + min(fun(x + 2, y, a), fun(x + 1, y - 1, a));
ll Y = a[y] + min(fun(x + 1, y - 1, a), fun(x, y - 2, a));
ll ans = max(X, Y);
return dp[x][y] = ans;
}
void solve() {
int n;
cin >> n;
int a[n];
ll sum = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
memset(dp, -1, sizeof dp);
ll X = fun(0, n - 1, a);
cout << X - (sum - X);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
t = 1;
while (t--) {
solve();
cout << endl;
}
}
| replace | 15 | 16 | 15 | 16 | TLE | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#ifdef _PRINTDEBUG
#include "lib/printdebug.hpp"
#else
#define printdebug(...) 1
#endif
#define I_1000000007 (1000000007)
#define I_998244353 (998244353)
#define I_INF (1 << 28)
#define L_INF (1LL << 60)
#define rep(i, n) REP(i, 0, (n))
#define repc(i, n) REPC(i, 0, (n))
#define REP(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define REPC(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define yesno(cond) YESNO((cond), "Yes", "No")
#define YESNO(cond, a, b) cout << ((cond) ? (a) : (b)) << "\n"
template <class T> inline bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
} else
return false;
}
template <class T> inline bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
} else
return false;
}
namespace solver {
using namespace std;
using ll = long long;
int N, as[3030];
ll dp[3030][3030][2];
void init() {
cin >> N;
rep(i, N) { cin >> as[i]; }
}
ll rec(int l, int r, bool turn) {
if (dp[l][r][turn] >= 0)
return dp[l][r][turn];
int k = 1;
if (turn)
k = -1;
if (l == r - 1)
return dp[l][r][turn] = k * as[l];
ll left = rec(l + 1, r, !turn) + k * as[l];
ll right = rec(l, r - 1, !turn) + k * as[r - 1];
if (turn) {
return dp[l][r][turn] = min(left, right);
} else {
return dp[l][r][turn] = max(left, right);
}
}
void solve() {
repc(i, N) repc(j, N) rep(k, N) dp[i][j][k] = -L_INF;
cout << rec(0, N, 0) << "\n";
}
} // namespace solver
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
solver::init();
solver::solve();
return 0;
} | #include <bits/stdc++.h>
#ifdef _PRINTDEBUG
#include "lib/printdebug.hpp"
#else
#define printdebug(...) 1
#endif
#define I_1000000007 (1000000007)
#define I_998244353 (998244353)
#define I_INF (1 << 28)
#define L_INF (1LL << 60)
#define rep(i, n) REP(i, 0, (n))
#define repc(i, n) REPC(i, 0, (n))
#define REP(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define REPC(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define yesno(cond) YESNO((cond), "Yes", "No")
#define YESNO(cond, a, b) cout << ((cond) ? (a) : (b)) << "\n"
template <class T> inline bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
} else
return false;
}
template <class T> inline bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
} else
return false;
}
namespace solver {
using namespace std;
using ll = long long;
int N, as[3030];
ll dp[3030][3030][2];
void init() {
cin >> N;
rep(i, N) { cin >> as[i]; }
}
ll rec(int l, int r, bool turn) {
if (dp[l][r][turn] > -L_INF)
return dp[l][r][turn];
int k = 1;
if (turn)
k = -1;
if (l == r - 1)
return dp[l][r][turn] = k * as[l];
ll left = rec(l + 1, r, !turn) + k * as[l];
ll right = rec(l, r - 1, !turn) + k * as[r - 1];
if (turn) {
return dp[l][r][turn] = min(left, right);
} else {
return dp[l][r][turn] = max(left, right);
}
}
void solve() {
repc(i, N) repc(j, N) rep(k, N) dp[i][j][k] = -L_INF;
cout << rec(0, N, 0) << "\n";
}
} // namespace solver
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
solver::init();
solver::solve();
return 0;
} | replace | 46 | 47 | 46 | 47 | TLE | |
p03171 | C++ | Runtime Error | /*input
4
10 80 90 30
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less<long long>, rb_tree_tag,
tree_order_statistics_node_update>
indexed_set;
#pragma GCC optimize("unroll-loops,no-stack-protector")
// order_of_key #of elements less than x
// find_by_order kth element
typedef long long int ll;
#define ld long double
#define pii pair<int, int>
#define f first
#define s second
#define pb emplace_back
#define REP(i, n) for (ll i = 0; i < n; i++)
#define sz(_a) (ll)(_a.size())
#define FILL(n) memset(n, 0, sizeof(n))
#define ALL(_a) _a.begin(), _a.end()
const ll maxn = 100005;
const ll maxlg = __lg(maxn) + 2;
const ll INF64 = 8000000000000000000LL;
const int INF = 0x3f3f3f3f;
const ll MOD = ll(1e9 + 7);
const ld PI = acos(-1);
const ld eps = 1e-9;
ll mypow(ll a, ll b) {
ll res = 1LL;
while (b) {
if (b & 1)
res = res * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return res;
}
int dp[1005][1005];
int arr[maxn];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
REP(i, n) cin >> arr[i];
for (int i = n - 1; i >= 0; i--) {
for (int j = i; j < n; j++) {
if (i == j)
dp[i][j] = arr[i];
else
dp[i][j] = max(arr[j] - dp[i][j - 1], arr[i] - dp[i + 1][j]);
}
}
cout << dp[0][n - 1];
} | /*input
4
10 80 90 30
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less<long long>, rb_tree_tag,
tree_order_statistics_node_update>
indexed_set;
#pragma GCC optimize("unroll-loops,no-stack-protector")
// order_of_key #of elements less than x
// find_by_order kth element
typedef long long int ll;
#define ld long double
#define pii pair<int, int>
#define f first
#define s second
#define pb emplace_back
#define REP(i, n) for (ll i = 0; i < n; i++)
#define sz(_a) (ll)(_a.size())
#define FILL(n) memset(n, 0, sizeof(n))
#define ALL(_a) _a.begin(), _a.end()
const ll maxn = 100005;
const ll maxlg = __lg(maxn) + 2;
const ll INF64 = 8000000000000000000LL;
const int INF = 0x3f3f3f3f;
const ll MOD = ll(1e9 + 7);
const ld PI = acos(-1);
const ld eps = 1e-9;
ll mypow(ll a, ll b) {
ll res = 1LL;
while (b) {
if (b & 1)
res = res * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return res;
}
ll dp[3005][3005];
ll arr[maxn];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
REP(i, n) cin >> arr[i];
for (int i = n - 1; i >= 0; i--) {
for (int j = i; j < n; j++) {
if (i == j)
dp[i][j] = arr[i];
else
dp[i][j] = max(arr[j] - dp[i][j - 1], arr[i] - dp[i + 1][j]);
}
}
cout << dp[0][n - 1];
} | replace | 42 | 44 | 42 | 44 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int INF = 1e9 + 5;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key
const int mxs = 302;
int main() {
ll dp[mxs][mxs] = {}, in[mxs];
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> in[i];
dp[i][i] = in[i];
}
for (int l = n - 1; l >= 0; l--)
for (int r = l; r < n; r++) {
if (l < r) {
dp[l][r] = max(in[l] - dp[l + 1][r], in[r] - dp[l][r - 1]);
}
}
cout << dp[0][n - 1] << '\n';
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int INF = 1e9 + 5;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key
const int mxs = 3002;
int main() {
ll dp[mxs][mxs] = {}, in[mxs];
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> in[i];
dp[i][i] = in[i];
}
for (int l = n - 1; l >= 0; l--)
for (int r = l; r < n; r++) {
if (l < r) {
dp[l][r] = max(in[l] - dp[l + 1][r], in[r] - dp[l][r - 1]);
}
}
cout << dp[0][n - 1] << '\n';
}
| replace | 14 | 15 | 14 | 15 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#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 REP(i, n) for (ll i = 0; i < (n); ++i)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
#define VECCIN(x) \
for (auto &youso_ : (x)) \
cin >> youso_
#define bitcnt __builtin_popcount
#define SZ(x) ((ll)(x).size())
#define fi first
#define se second
#define All(a) (a).begin(), (a).end()
template <typename T = long long> inline T IN() {
T x;
cin >> x;
return (x);
}
inline void CIN() {}
template <class Head, class... Tail>
inline void CIN(Head &&head, Tail &&...tail) {
cin >> head;
CIN(move(tail)...);
}
#define CCIN(...) \
char __VA_ARGS__; \
CIN(__VA_ARGS__)
#define DCIN(...) \
double __VA_ARGS__; \
CIN(__VA_ARGS__)
#define LCIN(...) \
ll __VA_ARGS__; \
CIN(__VA_ARGS__)
#define SCIN(...) \
string __VA_ARGS__; \
CIN(__VA_ARGS__)
#define Printv(v) \
{ \
REP(x, v.size()) { cout << v[x] << (x == v.size() - 1 ? "\n" : " "); } \
}
template <typename T = string> inline void eputs(T s) {
cout << s << "\n";
exit(0);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
std::fill((T *)array, (T *)(array + N), val);
}
template <typename T> using PQG = priority_queue<T, vector<T>, greater<T>>;
template <typename T> using PQ = priority_queue<T>;
typedef long long ll;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<ll, ll> PL;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const double PI = atan(1.0) * 4.0;
const ll LINF = 1e18;
const ll dx[] = {1, 0, -1, 0};
const ll dy[] = {0, 1, 0, -1};
void cinfast() {
cin.tie(0);
ios::sync_with_stdio(false);
}
ll N, K;
ll dp[3000][3000];
ll a[3000];
signed main() {
cin >> N;
REP(i, N) cin >> a[i];
REPR(i, N - 1) {
FOR(j, i, N) {
if ((N - i - j - 1) % 2 == 0) {
dp[i][j] = max(dp[i + 1][j] + a[i], dp[i][j - 1] + a[j]);
} else {
dp[i][j] = min(dp[i + 1][j] - a[i], dp[i][j - 1] - a[j]);
}
}
}
cout << dp[0][N - 1] << "\n";
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#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 REP(i, n) for (ll i = 0; i < (n); ++i)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
#define VECCIN(x) \
for (auto &youso_ : (x)) \
cin >> youso_
#define bitcnt __builtin_popcount
#define SZ(x) ((ll)(x).size())
#define fi first
#define se second
#define All(a) (a).begin(), (a).end()
template <typename T = long long> inline T IN() {
T x;
cin >> x;
return (x);
}
inline void CIN() {}
template <class Head, class... Tail>
inline void CIN(Head &&head, Tail &&...tail) {
cin >> head;
CIN(move(tail)...);
}
#define CCIN(...) \
char __VA_ARGS__; \
CIN(__VA_ARGS__)
#define DCIN(...) \
double __VA_ARGS__; \
CIN(__VA_ARGS__)
#define LCIN(...) \
ll __VA_ARGS__; \
CIN(__VA_ARGS__)
#define SCIN(...) \
string __VA_ARGS__; \
CIN(__VA_ARGS__)
#define Printv(v) \
{ \
REP(x, v.size()) { cout << v[x] << (x == v.size() - 1 ? "\n" : " "); } \
}
template <typename T = string> inline void eputs(T s) {
cout << s << "\n";
exit(0);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
std::fill((T *)array, (T *)(array + N), val);
}
template <typename T> using PQG = priority_queue<T, vector<T>, greater<T>>;
template <typename T> using PQ = priority_queue<T>;
typedef long long ll;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<ll, ll> PL;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const double PI = atan(1.0) * 4.0;
const ll LINF = 1e18;
const ll dx[] = {1, 0, -1, 0};
const ll dy[] = {0, 1, 0, -1};
void cinfast() {
cin.tie(0);
ios::sync_with_stdio(false);
}
ll N, K;
ll dp[3001][3001];
ll a[3001];
signed main() {
cin >> N;
REP(i, N) cin >> a[i];
REPR(i, N - 1) {
FOR(j, i, N) {
if ((N - i - j - 1) % 2 == 0) {
dp[i][j] = max(dp[i + 1][j] + a[i], dp[i][j - 1] + a[j]);
} else {
dp[i][j] = min(dp[i + 1][j] - a[i], dp[i][j - 1] - a[j]);
}
}
}
cout << dp[0][N - 1] << "\n";
}
| replace | 75 | 77 | 75 | 77 | 0 | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int n;
vector<long long> v;
pair<long long, long long> dp[3000][3000][2];
pair<long long, long long> slv(int i = 0, int j = n - 1, int f = 0) {
if (i > j)
return {0, 0};
if (dp[i][j][f].first != 0ll && dp[i][j][f].second != 0ll)
return dp[i][j][f];
if (f == 0) {
pair<long long, long long> chc1 = slv(i, j - 1, f = 1);
chc1.first += v[j];
pair<long long, long long> chc2 = slv(i + 1, j, f = 1);
chc2.first += v[i];
if (chc1.first - chc1.second >= chc2.first - chc2.second)
return dp[i][j][f] = chc1;
else
return dp[i][j][f] = chc2;
} else {
pair<long long, long long> chc1 = slv(i, j - 1, f = 0);
chc1.second += v[j];
pair<long long, long long> chc2 = slv(i + 1, j, f = 0);
chc2.second += v[i];
if (chc1.first - chc1.second <= chc2.first - chc2.second)
return dp[i][j][f] = chc1;
else
return dp[i][j][f] = chc2;
}
}
int main() {
cin >> n;
v = vector<long long>(n);
for (int i = 0; i < n; i++)
cin >> v[i];
pair<long long, long long> ans = slv();
cout << (ans.first - ans.second) << endl;
/**
5
0.42 0.01 0.42 0.99 0.42
*/
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int n;
vector<long long> v;
pair<long long, long long> dp[3000][3000][2];
pair<long long, long long> slv(int i = 0, int j = n - 1, int f = 0) {
if (i > j)
return {0, 0};
if (dp[i][j][f].first != 0ll && dp[i][j][f].second != 0ll)
return dp[i][j][f];
if (f == 0) {
pair<long long, long long> chc1 = slv(i, j - 1, f = 1);
chc1.first += v[j];
pair<long long, long long> chc2 = slv(i + 1, j, f = 1);
chc2.first += v[i];
if (chc1.first - chc1.second >= chc2.first - chc2.second)
return dp[i][j][f] = chc1;
else
return dp[i][j][f] = chc2;
} else {
pair<long long, long long> chc1 = slv(i, j - 1, f = 0);
chc1.second += v[j];
pair<long long, long long> chc2 = slv(i + 1, j, f = 0);
chc2.second += v[i];
if (chc1.first - chc1.second <= chc2.first - chc2.second)
return dp[i][j][f] = chc1;
else
return dp[i][j][f] = chc2;
}
}
int main() {
cin >> n;
v = vector<long long>(n);
for (int i = 0; i < n; i++)
cin >> v[i];
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j < n; j++) {
if (i > j)
continue;
for (int f = 0; f < 2; f++) {
if (f == 0) {
if (i == j)
dp[i][j][f] = {v[i], 0ll};
else {
pair<long long, long long> chc1 = dp[i][j - 1][1];
/// cout<<dp[i][j-1][1].first<<' '<<dp[i][j-1][1].second<<'\n';
chc1.first += v[j];
pair<long long, long long> chc2 = dp[i + 1][j][1];
chc2.first += v[i];
/// cout<<dp[i+1][j][1].first<<' '<<dp[i+1][j][1].second<<'\n';
if (chc1.first - chc1.second >= chc2.first - chc2.second)
dp[i][j][f] = chc1;
else
dp[i][j][f] = chc2;
}
} else {
if (i == j)
dp[i][j][f] = {0ll, v[i]};
else {
pair<long long, long long> chc1 = dp[i][j - 1][0];
/// cout<<dp[i][j-1][0].first<<' '<<dp[i][j-1][0].second<<'\n';
chc1.second += v[j];
pair<long long, long long> chc2 = dp[i + 1][j][0];
/// cout<<dp[i+1][j][0].first<<' '<<dp[i+1][j][0].second<<'\n';
chc2.second += v[i];
if (chc1.first - chc1.second <= chc2.first - chc2.second)
dp[i][j][f] = chc1;
else
dp[i][j][f] = chc2;
}
}
}
}
}
pair<long long, long long> ans = slv();
cout << (ans.first - ans.second) << endl;
/**
5
0.42 0.01 0.42 0.99 0.42
*/
return 0;
}
| insert | 37 | 37 | 37 | 76 | TLE | |
p03171 | C++ | Runtime Error | // fest
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define y1 dasdasfasfas
#define x1 wqdadfasfasfas
#define All(c) c.begin(), c.end()
#define SZ(A) (int((A).size()))
#define umap unordered_map
#define __ fflush(stdout)
#define FILENAME ""
typedef long long ll;
typedef long double ld;
using namespace std;
void FREOPEN() {
#ifdef LOCAL
freopen(".in", "r", stdin);
freopen("1.out", "w", stdout);
#else
// freopen(FILENAME".in", "r", stdin);
// freopen(FILENAME".out", "w", stdout);
#endif
}
inline double Time() { return (clock() * 1.0) / CLOCKS_PER_SEC; }
const int N = 3000, inf = 1e9 * 2;
const ll MOD = 1e9 + 7ll, INF = 1e18;
const int dx[] = {1, -1, 0, 0, -1, 1, -1, 1};
const int dy[] = {0, 0, 1, -1, -1, 1, 1, -1};
ll dp[N][N];
bool was[N][N];
int a[N];
ll solve(int l, int r, int player) {
if (was[l][r])
return dp[l][r];
was[l][r] = 1;
if (l == r) {
if (player)
return dp[l][r] = -a[l];
else
return dp[l][r] = a[l];
}
if (!player) {
dp[l][r] = max(solve(l + 1, r, 1) + a[l], solve(l, r - 1, 1) + a[r]);
} else {
dp[l][r] = min(solve(l + 1, r, 0) - a[l], solve(l, r - 1, 0) - a[r]);
}
return dp[l][r];
}
int main() {
FREOPEN();
int n;
cin >> n;
for (int i = 1; i <= n; i++)
scanf("%d ", &a[i]);
printf("%lld ", solve(1, n, 0));
return 0;
}
| // fest
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define y1 dasdasfasfas
#define x1 wqdadfasfasfas
#define All(c) c.begin(), c.end()
#define SZ(A) (int((A).size()))
#define umap unordered_map
#define __ fflush(stdout)
#define FILENAME ""
typedef long long ll;
typedef long double ld;
using namespace std;
void FREOPEN() {
#ifdef LOCAL
freopen(".in", "r", stdin);
freopen("1.out", "w", stdout);
#else
// freopen(FILENAME".in", "r", stdin);
// freopen(FILENAME".out", "w", stdout);
#endif
}
inline double Time() { return (clock() * 1.0) / CLOCKS_PER_SEC; }
const int N = 3001, inf = 1e9 * 2;
const ll MOD = 1e9 + 7ll, INF = 1e18;
const int dx[] = {1, -1, 0, 0, -1, 1, -1, 1};
const int dy[] = {0, 0, 1, -1, -1, 1, 1, -1};
ll dp[N][N];
bool was[N][N];
int a[N];
ll solve(int l, int r, int player) {
if (was[l][r])
return dp[l][r];
was[l][r] = 1;
if (l == r) {
if (player)
return dp[l][r] = -a[l];
else
return dp[l][r] = a[l];
}
if (!player) {
dp[l][r] = max(solve(l + 1, r, 1) + a[l], solve(l, r - 1, 1) + a[r]);
} else {
dp[l][r] = min(solve(l + 1, r, 0) - a[l], solve(l, r - 1, 0) - a[r]);
}
return dp[l][r];
}
int main() {
FREOPEN();
int n;
cin >> n;
for (int i = 1; i <= n; i++)
scanf("%d ", &a[i]);
printf("%lld ", solve(1, n, 0));
return 0;
}
| replace | 31 | 32 | 31 | 32 | 0 | |
p03171 | C++ | Runtime Error | // I am Varun Das from Jadavpur University
#include <bits/stdc++.h>
#define mod 1000000007
#define ll long long
#define dbg(x) cerr << #x << " = " << x << endl
#define INF LLONG_MAX
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a, b) for (int i = a; i >= b; i--)
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// cout<<fixed<<setprecision(12);
ll n, a[10004];
cin >> n;
ll dp[10004][10004];
memset(dp, 0, sizeof(dp));
loop(i, 0, n) cin >> a[i];
rep(i, n - 1, 0) {
loop(j, i, n) {
if (i == j)
dp[i][j] = a[i];
else {
dp[i][j] = max(a[i] - dp[i + 1][j], a[j] - dp[i][j - 1]);
}
}
}
cout << dp[0][n - 1];
return 0;
} | // I am Varun Das from Jadavpur University
#include <bits/stdc++.h>
#define mod 1000000007
#define ll long long
#define dbg(x) cerr << #x << " = " << x << endl
#define INF LLONG_MAX
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a, b) for (int i = a; i >= b; i--)
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// cout<<fixed<<setprecision(12);
ll n, a[10004];
cin >> n;
ll dp[3004][3004];
memset(dp, 0, sizeof(dp));
loop(i, 0, n) cin >> a[i];
rep(i, n - 1, 0) {
loop(j, i, n) {
if (i == j)
dp[i][j] = a[i];
else {
dp[i][j] = max(a[i] - dp[i + 1][j], a[j] - dp[i][j - 1]);
}
}
}
cout << dp[0][n - 1];
return 0;
} | replace | 20 | 21 | 20 | 21 | -11 | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
const ll INF = (1LL << 60);
const int MOD = 1000000007;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int n;
vector<int> a(3030);
vector<vector<ll>> dp(3030, vector<ll>(3030, 0));
bool seen[3030][3030];
// [l, r)
ll rec(int l, int r) {
if (l > r)
return 0;
if (seen[l][r])
return dp[l][r];
ll res = 0;
int dif = n - (r - l + 1);
if (dif % 2 == 0) {
res = -INF;
chmax(res, rec(l + 1, r) + a[l]);
chmax(res, rec(l, r - 1) + a[r]);
} else {
res = INF;
chmin(res, rec(l + 1, r) - a[l]);
chmin(res, rec(l, r - 1) - a[r]);
}
return dp[l][r] = res;
}
int main() {
cin >> n;
rep(i, n) cin >> a[i];
rec(0, n - 1);
cout << dp[0][n - 1] << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
const ll INF = (1LL << 60);
const int MOD = 1000000007;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int n;
vector<int> a(3030);
vector<vector<ll>> dp(3030, vector<ll>(3030, 0));
bool seen[3030][3030];
// [l, r)
ll rec(int l, int r) {
if (l > r)
return 0;
if (seen[l][r])
return dp[l][r];
seen[l][r] = true;
ll res = 0;
int dif = n - (r - l + 1);
if (dif % 2 == 0) {
res = -INF;
chmax(res, rec(l + 1, r) + a[l]);
chmax(res, rec(l, r - 1) + a[r]);
} else {
res = INF;
chmin(res, rec(l + 1, r) - a[l]);
chmin(res, rec(l, r - 1) - a[r]);
}
return dp[l][r] = res;
}
int main() {
cin >> n;
rep(i, n) cin >> a[i];
rec(0, n - 1);
cout << dp[0][n - 1] << endl;
} | insert | 34 | 34 | 34 | 36 | TLE | |
p03171 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
#define el_cucuy \
ios_base::sync_with_stdio(0), cout.tie(0); \
cin.tie(0);
#define llong long long
#define pb push_back
#define bpc __builtin_popcount
#define pii pair<int, int>
#define f first
#define s second
const llong mxn = 4 * 1e5 + 3;
const llong mod = 1e9 + 7;
const llong inf = 1e18 + 9;
using namespace std;
// int dx[]= {1,0,-1,0}; int dy[]= {0,1,0,-1}; //4 Direction
// int dx[]={1,1,0,-1,-1,-1,0,1}; int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
// int dx[]={2,1,-1,-2,-2,-1,1,2}; int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight
// Direction int dx[]={-1,-1,+0,+1,+1,+0}; int dy[]={-1,+1,+2,+1,-1,-2};
// //Hexagonal Direction
llong n, m, a[mxn], dp[1001][1001], sum[mxn];
int main() {
/// freopen("input.txt", "r", stdin);
/// freopen("output.txt", "w", stdout);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
dp[i][i] = a[i];
}
for (int sz = 2; sz <= n; sz++) {
for (int l = 1; l <= n - sz + 1; l++) {
llong r = l + sz - 1;
dp[l][r] = max(a[l] - dp[l + 1][r], a[r] - dp[l][r - 1]);
}
}
cout << dp[1][n];
return 0;
}
| #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
#define el_cucuy \
ios_base::sync_with_stdio(0), cout.tie(0); \
cin.tie(0);
#define llong long long
#define pb push_back
#define bpc __builtin_popcount
#define pii pair<int, int>
#define f first
#define s second
const llong mxn = 4 * 1e5 + 3;
const llong mod = 1e9 + 7;
const llong inf = 1e18 + 9;
using namespace std;
// int dx[]= {1,0,-1,0}; int dy[]= {0,1,0,-1}; //4 Direction
// int dx[]={1,1,0,-1,-1,-1,0,1}; int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
// int dx[]={2,1,-1,-2,-2,-1,1,2}; int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight
// Direction int dx[]={-1,-1,+0,+1,+1,+0}; int dy[]={-1,+1,+2,+1,-1,-2};
// //Hexagonal Direction
llong n, m, a[mxn], dp[3001][3001], sum[mxn];
int main() {
/// freopen("input.txt", "r", stdin);
/// freopen("output.txt", "w", stdout);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
dp[i][i] = a[i];
}
for (int sz = 2; sz <= n; sz++) {
for (int l = 1; l <= n - sz + 1; l++) {
llong r = l + sz - 1;
dp[l][r] = max(a[l] - dp[l + 1][r], a[r] - dp[l][r - 1]);
}
}
cout << dp[1][n];
return 0;
}
| replace | 47 | 48 | 47 | 48 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
#define sanyes \
ios_base::sync_with_stdio(0), cout.tie(0); \
cin.tie(0);
#define llong long long
#define pb push_back
#define bpc __builtin_popcount
const llong mxn = 1e5 + 7;
const llong mod = 1e9 + 7;
const llong inf = 1e18 + 9;
using namespace std;
deque<llong> deq;
vector<llong> v;
llong n, a[mxn], k, dp[1001][1001], sum2, sum1;
bool ok = 0;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j + i - 1 <= n; j++) {
dp[i][j] = max(a[j] - dp[i - 1][j + 1], a[j + i - 1] - dp[i - 1][j]);
/// cout << dp[i][j] << ' ';
} /// cout << endl;
}
cout << dp[n][1];
return 0;
}
| #include <bits/stdc++.h>
#define sanyes \
ios_base::sync_with_stdio(0), cout.tie(0); \
cin.tie(0);
#define llong long long
#define pb push_back
#define bpc __builtin_popcount
const llong mxn = 1e5 + 7;
const llong mod = 1e9 + 7;
const llong inf = 1e18 + 9;
using namespace std;
deque<llong> deq;
vector<llong> v;
llong n, a[mxn], k, dp[3001][3001], sum2, sum1;
bool ok = 0;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j + i - 1 <= n; j++) {
dp[i][j] = max(a[j] - dp[i - 1][j + 1], a[j + i - 1] - dp[i - 1][j]);
/// cout << dp[i][j] << ' ';
} /// cout << endl;
}
cout << dp[n][1];
return 0;
}
| replace | 17 | 18 | 17 | 18 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
#define pb push_back
#define f first
#define s second
#define mp make_pair
#define inlld(x) scanf("%lld", &x)
#define ind(x) scanf("%d", &x)
#define inlld2(x, y) scanf("%lld%lld", &x, &y)
#define inlld3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z)
#define ind2(x, y) scanf("%d%d", &x, &y)
#define ind3(x, y, z) scanf("%d%d%d", &x, &y, &z)
const int N = 1e3 + 5;
const int MOD = 1e9 + 7;
typedef long long ll;
typedef long double ld;
using namespace std;
ll n, arr[N], dp[N][N][2];
ll solve(ll i, ll j, ll mov) {
if (dp[i][j][mov] != -1)
return dp[i][j][mov];
if (i == j && mov)
return arr[i];
else if (i == j)
return -arr[i];
if (mov)
return dp[i][j][mov] =
max(solve(i + 1, j, 0) + arr[i], solve(i, j - 1, 0) + arr[j]);
else
return dp[i][j][mov] =
min(solve(i + 1, j, 1) - arr[i], solve(i, j - 1, 1) - arr[j]);
}
int main() {
inlld(n);
for (ll a = 1; a <= n; a++)
inlld(arr[a]);
memset(dp, -1, sizeof(dp));
printf("%lld\n", solve(1, n, 1));
return 0;
} | #include <bits/stdc++.h>
#define pb push_back
#define f first
#define s second
#define mp make_pair
#define inlld(x) scanf("%lld", &x)
#define ind(x) scanf("%d", &x)
#define inlld2(x, y) scanf("%lld%lld", &x, &y)
#define inlld3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z)
#define ind2(x, y) scanf("%d%d", &x, &y)
#define ind3(x, y, z) scanf("%d%d%d", &x, &y, &z)
const int N = 3e3 + 5;
const int MOD = 1e9 + 7;
typedef long long ll;
typedef long double ld;
using namespace std;
ll n, arr[N], dp[N][N][2];
ll solve(ll i, ll j, ll mov) {
if (dp[i][j][mov] != -1)
return dp[i][j][mov];
if (i == j && mov)
return arr[i];
else if (i == j)
return -arr[i];
if (mov)
return dp[i][j][mov] =
max(solve(i + 1, j, 0) + arr[i], solve(i, j - 1, 0) + arr[j]);
else
return dp[i][j][mov] =
min(solve(i + 1, j, 1) - arr[i], solve(i, j - 1, 1) - arr[j]);
}
int main() {
inlld(n);
for (ll a = 1; a <= n; a++)
inlld(arr[a]);
memset(dp, -1, sizeof(dp));
printf("%lld\n", solve(1, n, 1));
return 0;
} | replace | 12 | 13 | 12 | 13 | 0 | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, a[3009], dp[3009][3009];
ll best(ll l, ll r, bool b) {
if (l > r)
return 0;
ll &ret = dp[l][r];
if (ret < 1e4)
return ret;
if (b)
return ret = min(best(l + 1, r, 0) - a[l], best(l, r - 1, 0) - a[r]);
return ret = max(best(l + 1, r, 1) + a[l], best(l, r - 1, 1) + a[r]);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (ll i = 0; i < n; i++)
cin >> a[i];
for (ll i = 0; i <= n; i++)
for (ll j = 0; j <= n; j++)
dp[i][j] = 1e14;
cout << best(0, n - 1, 0);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, a[3009], dp[3009][3009];
ll best(ll l, ll r, bool b) {
if (l > r)
return 0;
ll &ret = dp[l][r];
if (ret < 1e14)
return ret;
if (b)
return ret = min(best(l + 1, r, 0) - a[l], best(l, r - 1, 0) - a[r]);
return ret = max(best(l + 1, r, 1) + a[l], best(l, r - 1, 1) + a[r]);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (ll i = 0; i < n; i++)
cin >> a[i];
for (ll i = 0; i <= n; i++)
for (ll j = 0; j <= n; j++)
dp[i][j] = 1e14;
cout << best(0, n - 1, 0);
return 0;
}
| replace | 8 | 9 | 8 | 9 | TLE | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long
using namespace std;
#define w(x) \
int x; \
cin >> x; \
while (x--)
ll int n, k, mod = 1e9 + 7;
ll int a[101], dp[3000][3000];
ll int recur(int i, int j) {
if (i == j)
return a[i];
if (j - i == 1)
return abs(a[i] - a[j]);
if (dp[i][j] != -1)
return dp[i][j];
ll int ans1 = a[i] - a[i + 1] + recur(i + 2, j);
ll int ans2 = a[i] - a[j] + recur(i + 1, j - 1);
ll int ans5 = min(ans1, ans2);
ll int ans3 = a[j] - a[i] + recur(i + 1, j - 1);
ll int ans4 = a[j] - a[j - 1] + recur(i, j - 2);
ll int ans6 = min(ans3, ans4);
return dp[i][j] = max(ans5, ans6);
}
int main() {
memset(dp, -1, sizeof(dp));
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
cout << recur(0, n - 1);
} | #include <bits/stdc++.h>
#define ll long long
using namespace std;
#define w(x) \
int x; \
cin >> x; \
while (x--)
ll int n, k, mod = 1e9 + 7;
ll int a[3001], dp[3000][3000];
ll int recur(int i, int j) {
if (i == j)
return a[i];
if (j - i == 1)
return abs(a[i] - a[j]);
if (dp[i][j] != -1)
return dp[i][j];
ll int ans1 = a[i] - a[i + 1] + recur(i + 2, j);
ll int ans2 = a[i] - a[j] + recur(i + 1, j - 1);
ll int ans5 = min(ans1, ans2);
ll int ans3 = a[j] - a[i] + recur(i + 1, j - 1);
ll int ans4 = a[j] - a[j - 1] + recur(i, j - 2);
ll int ans6 = min(ans3, ans4);
return dp[i][j] = max(ans5, ans6);
}
int main() {
memset(dp, -1, sizeof(dp));
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
cout << recur(0, n - 1);
} | replace | 8 | 9 | 8 | 9 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, x, n) for (int i = x; i < (int)(n); ++i)
const int N = 3000;
int n, k, a[100];
ll dp[N][N][2];
ll cal(int i, int j, bool p) {
if (i > j)
return 0;
if (dp[i][j][p] != -1)
return dp[i][j][p];
if (p == 0)
return dp[i][j][p] =
max(cal(i + 1, j, !p) + a[i], cal(i, j - 1, !p) + a[j]);
else
return dp[i][j][p] =
min(cal(i + 1, j, !p) - a[i], cal(i, j - 1, !p) - a[j]);
}
int main() {
cin >> n;
f(i, 0, n) cin >> a[i];
memset(dp, -1, sizeof dp);
cout << cal(0, n - 1, 0);
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, x, n) for (int i = x; i < (int)(n); ++i)
const int N = 3000;
int n, k, a[N];
ll dp[N][N][2];
ll cal(int i, int j, bool p) {
if (i > j)
return 0;
if (dp[i][j][p] != -1)
return dp[i][j][p];
if (p == 0)
return dp[i][j][p] =
max(cal(i + 1, j, !p) + a[i], cal(i, j - 1, !p) + a[j]);
else
return dp[i][j][p] =
min(cal(i + 1, j, !p) - a[i], cal(i, j - 1, !p) - a[j]);
}
int main() {
cin >> n;
f(i, 0, n) cin >> a[i];
memset(dp, -1, sizeof dp);
cout << cal(0, n - 1, 0);
} | replace | 5 | 6 | 5 | 6 | -11 | |
p03171 | C++ | Runtime Error | #define taskname ""
#include <algorithm>
#include <iostream>
using namespace std;
#define long long long
const int N = 2e3 + 10;
int n, a[N];
long f[N][N];
int main() {
// freopen(taskname".INP", "r", stdin);
// freopen(taskname".OUT", "w", stdout);
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> a[i];
for (int r = 1; r <= n; ++r)
for (int l = r; l > 0; --l)
f[l][r] = max(a[l] - f[l + 1][r], a[r] - f[l][r - 1]);
cout << f[1][n];
return 0;
}
| #define taskname ""
#include <algorithm>
#include <iostream>
using namespace std;
#define long long long
const int N = 3e3 + 10;
int n, a[N];
long f[N][N];
int main() {
// freopen(taskname".INP", "r", stdin);
// freopen(taskname".OUT", "w", stdout);
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> a[i];
for (int r = 1; r <= n; ++r)
for (int l = r; l > 0; --l)
f[l][r] = max(a[l] - f[l + 1][r], a[r] - f[l][r - 1]);
cout << f[1][n];
return 0;
}
| replace | 8 | 9 | 8 | 9 | 0 | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
const int N = 3e3 + 10;
pll dp[N][N];
int a[N];
pll rec(int l, int r) {
if (l == r) {
return {a[l], 0};
} else {
pll f = {0, 0};
if (dp[l][r] != f)
return dp[l][r];
int x = a[l], y = a[r];
pll p1 = rec(l + 1, r);
pll p2 = rec(l, r - 1);
swap(p1.first, p1.second);
swap(p2.first, p2.second);
p1.first += x;
p2.first += y;
return max(p1, p2);
}
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
pll p = rec(0, n - 1);
cout << p.first - p.second;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
const int N = 3e3 + 10;
pll dp[N][N];
int a[N];
pll rec(int l, int r) {
if (l == r) {
return {a[l], 0};
} else {
pll f = {0, 0};
if (dp[l][r] != f)
return dp[l][r];
int x = a[l], y = a[r];
pll p1 = rec(l + 1, r);
pll p2 = rec(l, r - 1);
swap(p1.first, p1.second);
swap(p2.first, p2.second);
p1.first += x;
p2.first += y;
pll p = max(p1, p2);
dp[l][r] = p;
return p;
}
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
pll p = rec(0, n - 1);
cout << p.first - p.second;
} | replace | 28 | 29 | 28 | 31 | TLE | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
ll const INF = 1LL << 60;
#define p_ary(ary, a, b, i) \
do { \
cout << "["; \
for (int(i) = (a); (i) < (b); ++(i)) \
cout << ary[(i)] << ((b)-1 == (i) ? "" : ", "); \
cout << "]\n"; \
} while (0)
#define p_map(map, it) \
do { \
cout << "{"; \
for (auto(it) = map.begin();; ++(it)) { \
if ((it) == map.end()) { \
cout << "}\n"; \
break; \
} else \
cout << "" << (it)->first << "=>" << (it)->second << ", "; \
} \
} while (0)
int n;
ll dp[3010][3010];
int a[3010];
ll rec(int l, int r) {
if (dp[l][r] > INF)
return dp[l][r];
if (l > r)
dp[l][r] = 0;
else if ((l + n - r) & 1)
dp[l][r] = max(rec(l + 1, r) + a[l], rec(l, r - 1) + a[r]);
else
dp[l][r] = min(rec(l + 1, r) - a[l], rec(l, r - 1) - a[r]);
return dp[l][r];
}
int main() {
cin >> n;
for (int i = 0; i < n; ++i)
cin >> a[i];
for (int i = 0; i < n + 1; ++i)
for (int j = 0; j < n + 1; ++j)
dp[i][j] = -INF;
cout << rec(0, n - 1) << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
ll const INF = 1LL << 60;
#define p_ary(ary, a, b, i) \
do { \
cout << "["; \
for (int(i) = (a); (i) < (b); ++(i)) \
cout << ary[(i)] << ((b)-1 == (i) ? "" : ", "); \
cout << "]\n"; \
} while (0)
#define p_map(map, it) \
do { \
cout << "{"; \
for (auto(it) = map.begin();; ++(it)) { \
if ((it) == map.end()) { \
cout << "}\n"; \
break; \
} else \
cout << "" << (it)->first << "=>" << (it)->second << ", "; \
} \
} while (0)
int n;
ll dp[3010][3010];
int a[3010];
ll rec(int l, int r) {
if (dp[l][r] > -INF)
return dp[l][r];
if (l > r)
dp[l][r] = 0;
else if ((l + n - r) & 1)
dp[l][r] = max(rec(l + 1, r) + a[l], rec(l, r - 1) + a[r]);
else
dp[l][r] = min(rec(l + 1, r) - a[l], rec(l, r - 1) - a[r]);
return dp[l][r];
}
int main() {
cin >> n;
for (int i = 0; i < n; ++i)
cin >> a[i];
for (int i = 0; i < n + 1; ++i)
for (int j = 0; j < n + 1; ++j)
dp[i][j] = -INF;
cout << rec(0, n - 1) << endl;
return 0;
} | replace | 31 | 32 | 31 | 32 | TLE | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long
using namespace std;
int n;
int a[3005];
ll dp[3005][3005];
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = n - 1; i >= 0; i--) {
for (int j = i; j < n; j++) {
if (dp[i] == dp[j])
dp[i][j] = a[i];
else
dp[i][j] = max(a[i] - dp[i - 1][j], a[j] - dp[i][j - 1]);
}
}
cout << dp[0][n - 1] << endl;
return 0;
}
| #include <bits/stdc++.h>
#define ll long long
using namespace std;
int n;
int a[3005];
ll dp[3005][3005];
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = n - 1; i >= 0; i--) {
for (int j = i; j < n; j++) {
if (dp[i] == dp[j])
dp[i][j] = a[i];
else
dp[i][j] = max(a[i] - dp[i + 1][j], a[j] - dp[i][j - 1]);
}
}
cout << dp[0][n - 1] << endl;
return 0;
}
| replace | 16 | 17 | 16 | 17 | 0 | |
p03171 | C++ | Runtime Error |
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define endl '\n'
#define vll vector<ll>
#define vvll vector<vll>
#define Cout(x) cout << #x << " " << x << endl
#define pll pair<ll, ll>
#define vpll vector<pll>
#define mp make_pair
#define pb push_back
#define MOD 1000000007
#define inf 1e18;
#define vfind(v, x) (find(all(v), x) != (v).end())
#define clr(c) (c).clear()
#define cres(c, n) (c).clear(), (c).resize(n)
#define ios \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define fst first
#define scd second
#define eb(a) emplace_back(a)
#define ef(a) emplace_front(a)
#define fr(i, n) for (ll(i) = 0; (i) < (n); ++(i))
#define fr1(i, n) for (ll(i) = 1; (i) <= (n); ++(i))
#define frr(i, n) for (ll(i) = (n)-1; (i) >= 0; --(i))
#define frab(i, a, b, c) for (ll(i) = a; (i) <= (b); (i) += (c))
#define mst(A) memset((A), 0, sizeof(A));
using namespace std;
void write(vll &oneD) {
for (ll i = 0; i < oneD.size(); i++) {
printf("%lld ", oneD[i]);
}
printf("\n");
}
void write(vvll &twoD) {
for (ll i = 0; i < twoD.size(); i++) {
write(twoD[i]);
}
}
void write(vpll &oneDP) {
fr(i, oneDP.size()) { printf("%lld %lld\n", oneDP[i].fst, oneDP[i].scd); }
cout << "\n";
}
void write(map<ll, ll> &mpp) {
for (map<ll, ll>::iterator it = mpp.begin(); it != mpp.end(); it++) {
cout << it->fst << " : " << it->scd << endl;
}
cout << endl;
}
bool cmp(const pair<ll, ll> &a, const pair<ll, ll> &b) {
if (a.first == b.first)
return (a.scd >= b.scd);
return (a.first < b.first);
}
vll seive;
vll primes;
void Seive() {
const ll maxn = 100005;
seive.resize(maxn);
fr(i, maxn) seive[i] = 1;
for (ll i = 2; i * i < maxn; i++) {
if (seive[i] == 1) {
for (ll j = i * i; j < maxn; j += i) {
seive[j] = 0;
}
}
}
primes.pb(2);
for (ll i = 3; i < maxn; i += 2) {
if (seive[i])
primes.pb(i);
}
}
ll isprime(ll N) {
if (N < 2 || (!(N & 1) && N != 2))
return 0;
for (ll i = 3; i * i <= N; i += 2) {
if (!(N % i))
return 0;
}
return 1;
}
ll power(ll x, ll y, ll m) {
long long int res = 1;
x = x % m;
while (y > 0) {
// If y is odd, multiply x with result
if (y & 1)
res = (res * x) % MOD;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % MOD;
}
return res;
}
ll modinv(ll x, ll mod = MOD) { return power(x, mod - 2, mod); }
////////////////////////////////////////////////////////////////////////////////
ll A[1000005];
ll B[100001];
ll C[100001];
ll dp[3004][3003];
ll s1 = 0, s2 = 0;
ll n;
ll cal(ll s, ll e) {
if (s > e) {
return 0;
}
if (dp[s][e] != -1) {
return dp[s][e];
}
ll y = n - (e - s + 1) + 1;
return dp[s][e] = max(cal(s + 1, e) + y * A[s], cal(s, e - 1) + y * A[e]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("allo.txt", "r", stdin);
#endif
// freopen("out.txt","w",stdout);
ll a, b, c, d;
ll k, l, m, p, q, r, t, x, y;
// cin>>t;while(t--){ map<ll,ll> M; string s;
cin >> n;
memset(dp, -1, sizeof(dp));
fr(i, n) {
cin >> A[i];
// cout<<A[i];
}
for (int l = n - 1; l >= 0; l--) {
for (int r = l; r < n; r++) {
if (l == r) {
dp[l][r] = A[l];
} else {
dp[l][r] = max(A[l] - dp[l + 1][r], A[r] - dp[l][r - 1]);
}
}
}
cout << dp[0][n - 1] << endl;
// cout<<s1-s2<<endl;
// }
}
|
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define endl '\n'
#define vll vector<ll>
#define vvll vector<vll>
#define Cout(x) cout << #x << " " << x << endl
#define pll pair<ll, ll>
#define vpll vector<pll>
#define mp make_pair
#define pb push_back
#define MOD 1000000007
#define inf 1e18;
#define vfind(v, x) (find(all(v), x) != (v).end())
#define clr(c) (c).clear()
#define cres(c, n) (c).clear(), (c).resize(n)
#define ios \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define fst first
#define scd second
#define eb(a) emplace_back(a)
#define ef(a) emplace_front(a)
#define fr(i, n) for (ll(i) = 0; (i) < (n); ++(i))
#define fr1(i, n) for (ll(i) = 1; (i) <= (n); ++(i))
#define frr(i, n) for (ll(i) = (n)-1; (i) >= 0; --(i))
#define frab(i, a, b, c) for (ll(i) = a; (i) <= (b); (i) += (c))
#define mst(A) memset((A), 0, sizeof(A));
using namespace std;
void write(vll &oneD) {
for (ll i = 0; i < oneD.size(); i++) {
printf("%lld ", oneD[i]);
}
printf("\n");
}
void write(vvll &twoD) {
for (ll i = 0; i < twoD.size(); i++) {
write(twoD[i]);
}
}
void write(vpll &oneDP) {
fr(i, oneDP.size()) { printf("%lld %lld\n", oneDP[i].fst, oneDP[i].scd); }
cout << "\n";
}
void write(map<ll, ll> &mpp) {
for (map<ll, ll>::iterator it = mpp.begin(); it != mpp.end(); it++) {
cout << it->fst << " : " << it->scd << endl;
}
cout << endl;
}
bool cmp(const pair<ll, ll> &a, const pair<ll, ll> &b) {
if (a.first == b.first)
return (a.scd >= b.scd);
return (a.first < b.first);
}
vll seive;
vll primes;
void Seive() {
const ll maxn = 100005;
seive.resize(maxn);
fr(i, maxn) seive[i] = 1;
for (ll i = 2; i * i < maxn; i++) {
if (seive[i] == 1) {
for (ll j = i * i; j < maxn; j += i) {
seive[j] = 0;
}
}
}
primes.pb(2);
for (ll i = 3; i < maxn; i += 2) {
if (seive[i])
primes.pb(i);
}
}
ll isprime(ll N) {
if (N < 2 || (!(N & 1) && N != 2))
return 0;
for (ll i = 3; i * i <= N; i += 2) {
if (!(N % i))
return 0;
}
return 1;
}
ll power(ll x, ll y, ll m) {
long long int res = 1;
x = x % m;
while (y > 0) {
// If y is odd, multiply x with result
if (y & 1)
res = (res * x) % MOD;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % MOD;
}
return res;
}
ll modinv(ll x, ll mod = MOD) { return power(x, mod - 2, mod); }
////////////////////////////////////////////////////////////////////////////////
ll A[1000005];
ll B[100001];
ll C[100001];
ll dp[3004][3003];
ll s1 = 0, s2 = 0;
ll n;
ll cal(ll s, ll e) {
if (s > e) {
return 0;
}
if (dp[s][e] != -1) {
return dp[s][e];
}
ll y = n - (e - s + 1) + 1;
return dp[s][e] = max(cal(s + 1, e) + y * A[s], cal(s, e - 1) + y * A[e]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll a, b, c, d;
ll k, l, m, p, q, r, t, x, y;
// cin>>t;while(t--){ map<ll,ll> M; string s;
cin >> n;
memset(dp, -1, sizeof(dp));
fr(i, n) {
cin >> A[i];
// cout<<A[i];
}
for (int l = n - 1; l >= 0; l--) {
for (int r = l; r < n; r++) {
if (l == r) {
dp[l][r] = A[l];
} else {
dp[l][r] = max(A[l] - dp[l + 1][r], A[r] - dp[l][r - 1]);
}
}
}
cout << dp[0][n - 1] << endl;
// cout<<s1-s2<<endl;
// }
}
| delete | 178 | 182 | 178 | 178 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD (int)1e9 + 7
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define rrep(i, a, b) for (int i = a; i > b; --i)
#define vi vector<int>
#define vl vector<ll>
#define ld long double
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define pii pair<int, int>
#define pll pair<long, long>
#define vpii vector<pii>
#define vpll vector<pll>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define PI 3.1415926535897932384626433832795
#define fix(f, n) fixed << setprecision(n) << f
#define all(x) x.begin(), x.end()
ll powM(ll a, ll b, ll m) {
a %= m;
ll ans = 1;
while (b > 0) {
if (b & 1)
ans = ans * a % m;
a = a * a % m;
b >>= 1;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin >> n;
vl a(n + 1);
for (int i = 1; i <= n; i++)
cin >> a[i];
vvl dp(n + 2, vl(n + 1, 0));
// dp[l][r]---all the cards from [l,r] have been distributed .
for (int l = n; l >= 1; l--) {
for (int r = l; r <= n; r++) {
dp[l][r] = max(a[l] - dp[l + 1][r], a[r] - dp[l][r - 1]);
}
}
cout << dp[3][4] << "\n";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD (int)1e9 + 7
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define rrep(i, a, b) for (int i = a; i > b; --i)
#define vi vector<int>
#define vl vector<ll>
#define ld long double
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define pii pair<int, int>
#define pll pair<long, long>
#define vpii vector<pii>
#define vpll vector<pll>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define PI 3.1415926535897932384626433832795
#define fix(f, n) fixed << setprecision(n) << f
#define all(x) x.begin(), x.end()
ll powM(ll a, ll b, ll m) {
a %= m;
ll ans = 1;
while (b > 0) {
if (b & 1)
ans = ans * a % m;
a = a * a % m;
b >>= 1;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin >> n;
vl a(n + 1);
for (int i = 1; i <= n; i++)
cin >> a[i];
vvl dp(n + 2, vl(n + 1, 0));
// dp[l][r]---all the cards from [l,r] have been distributed .
for (int l = n; l >= 1; l--) {
for (int r = l; r <= n; r++) {
dp[l][r] = max(a[l] - dp[l + 1][r], a[r] - dp[l][r - 1]);
}
}
cout << dp[1][n] << "\n";
return 0;
}
| replace | 61 | 62 | 61 | 62 | 0 | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb push_back
#define pf push_front
#define pof pop_front
#define pob pop_back
#define mp make_pair
#define pll pair<ll, ll>
#define pii pair<int, int>
#define all(s) s.begin(), s.end()
#define vll vector<ll>
#define vi vector<int>
#define vpii vector<pii>
#define vpll vector<pll>
#define vd vector<double>
#define vull vector<ull>
#define vc vector<char>
#define vs vector<string>
#define fi first
#define se second
#define sz(x) x.size()
#define cinarr(n, a) \
for (int i = 0; i < n; i++) \
cin >> a[i];
#define coutarr(n, a) \
for (int i = 0; i < n; i++) \
cout << a[i] << (i + 1 == n ? '\n' : ' ');
#define fori(i, a, b, x) for (int i = a; i <= b; i += x)
#define ford(i, a, b, x) for (int i = a; i >= b; i -= x)
#define tc \
int t; \
cin >> t; \
for (int cs = 1; cs <= t; cs++)
#define IOS \
ios_base::sync_with_stdio(0); \
cin.tie(NULL);
#define ld long double
#define reset(x, y) memset(x, y, sizeof x);
#define sqr(x) 1LL * (x) * (x)
#define cube(x) 1LL * (x) * (x) * (x)
#define ub(v, a) upper_bound(all(v), a)
#define lb(v, a) lower_bound(all(v), a)
#define EPS 1e-10
#define INF 2000000000
#define mod 1000000007
#define minof(a) min_element(all(a))
#define maxof(a) max_element(all(a))
#define debug(val) cerr << "The value of " << #val << " is = " << val << endl;
#define testing cerr << "--------------------------\n";
// input space ---> scanf("%[^\n]%*c",s);
ll n, a[3005], sum, dp[3005][3005], vis[3005][3005];
ll f(ll l, ll r) {
if (l + 1 == r) {
return max(a[l], a[r]);
}
if (l == r) {
return a[l];
}
ll ret = -INF;
ll add = min(f(l + 1, r - 1), f(l + 2, r));
ret = max(ret, a[l] + add);
add = min(f(l, r - 2), f(l + 1, r - 1));
ret = max(ret, a[r] + add);
vis[l][r] = 1;
return dp[l][r] = ret;
}
int main() {
IOS cin >> n;
fori(i, 0, n - 1, 1) {
cin >> a[i];
sum += a[i];
}
ll X = f(0, n - 1);
cout << 2 * X - sum << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb push_back
#define pf push_front
#define pof pop_front
#define pob pop_back
#define mp make_pair
#define pll pair<ll, ll>
#define pii pair<int, int>
#define all(s) s.begin(), s.end()
#define vll vector<ll>
#define vi vector<int>
#define vpii vector<pii>
#define vpll vector<pll>
#define vd vector<double>
#define vull vector<ull>
#define vc vector<char>
#define vs vector<string>
#define fi first
#define se second
#define sz(x) x.size()
#define cinarr(n, a) \
for (int i = 0; i < n; i++) \
cin >> a[i];
#define coutarr(n, a) \
for (int i = 0; i < n; i++) \
cout << a[i] << (i + 1 == n ? '\n' : ' ');
#define fori(i, a, b, x) for (int i = a; i <= b; i += x)
#define ford(i, a, b, x) for (int i = a; i >= b; i -= x)
#define tc \
int t; \
cin >> t; \
for (int cs = 1; cs <= t; cs++)
#define IOS \
ios_base::sync_with_stdio(0); \
cin.tie(NULL);
#define ld long double
#define reset(x, y) memset(x, y, sizeof x);
#define sqr(x) 1LL * (x) * (x)
#define cube(x) 1LL * (x) * (x) * (x)
#define ub(v, a) upper_bound(all(v), a)
#define lb(v, a) lower_bound(all(v), a)
#define EPS 1e-10
#define INF 2000000000
#define mod 1000000007
#define minof(a) min_element(all(a))
#define maxof(a) max_element(all(a))
#define debug(val) cerr << "The value of " << #val << " is = " << val << endl;
#define testing cerr << "--------------------------\n";
// input space ---> scanf("%[^\n]%*c",s);
ll n, a[3005], sum, dp[3005][3005], vis[3005][3005];
ll f(ll l, ll r) {
if (l + 1 == r) {
return max(a[l], a[r]);
}
if (l == r) {
return a[l];
}
if (vis[l][r])
return dp[l][r];
ll ret = -INF;
ll add = min(f(l + 1, r - 1), f(l + 2, r));
ret = max(ret, a[l] + add);
add = min(f(l, r - 2), f(l + 1, r - 1));
ret = max(ret, a[r] + add);
vis[l][r] = 1;
return dp[l][r] = ret;
}
int main() {
IOS cin >> n;
fori(i, 0, n - 1, 1) {
cin >> a[i];
sum += a[i];
}
ll X = f(0, n - 1);
cout << 2 * X - sum << endl;
}
| insert | 64 | 64 | 64 | 66 | TLE | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
#ifdef DEMETRIO
#define deb(...) fprintf(stderr, __VA_ARGS__)
#define deb1(x) cerr << #x << " = " << x << endl
#else
#define deb(...) 0
#define deb1(x) 0
#endif
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define fore(i, a, b) for (int i = a, ThxDem = b; i < ThxDem; ++i)
#define SZ(x) ((int)(x).size())
#define mset(a, v) memset(a, v, sizeof(a))
#define mcpy(a, b) memcpy(a, b, sizeof(a))
using namespace std;
typedef long long ll;
int n;
ll a[3005];
ll f[3005][3005];
int main() {
scanf("%d", &n);
fore(i, 0, n) scanf("%lld", a + i);
fore(l, 1, n + 1) fore(i, 0, n) {
int j = i + l;
f[i][j] = max(a[i] - f[i + 1][j], a[j - 1] - f[i][j - 1]);
}
printf("%lld\n", f[0][n]);
return 0;
} | #include <bits/stdc++.h>
#ifdef DEMETRIO
#define deb(...) fprintf(stderr, __VA_ARGS__)
#define deb1(x) cerr << #x << " = " << x << endl
#else
#define deb(...) 0
#define deb1(x) 0
#endif
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define fore(i, a, b) for (int i = a, ThxDem = b; i < ThxDem; ++i)
#define SZ(x) ((int)(x).size())
#define mset(a, v) memset(a, v, sizeof(a))
#define mcpy(a, b) memcpy(a, b, sizeof(a))
using namespace std;
typedef long long ll;
int n;
ll a[3005];
ll f[3005][3005];
int main() {
scanf("%d", &n);
fore(i, 0, n) scanf("%lld", a + i);
fore(l, 1, n + 1) fore(i, 0, n - l + 1) {
int j = i + l;
f[i][j] = max(a[i] - f[i + 1][j], a[j - 1] - f[i][j - 1]);
}
printf("%lld\n", f[0][n]);
return 0;
} | replace | 26 | 27 | 26 | 27 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define deb(x) cout << #x << " " << x << "\n";
#define MAX 9223372036854775807
#define MIN -9223372036854775807
#define PI 3.141592653589
#define setbits(n) __builtin_popcountll(n)
const ll mod = 1e9 + 7;
const int N = 1e3 + 1;
ll dp[N][N][2];
vector<int> a(N);
ll n;
ll go(int l, int r, int mov) {
if (l > r)
return 0;
if (dp[l][r][mov] != -1)
return dp[l][r][mov];
ll ans;
if (mov == 0)
ans = max(a[l] + go(l + 1, r, 1 ^ mov), a[r] + go(l, r - 1, 1 ^ mov));
else
ans = min(go(l + 1, r, 1 ^ mov) - a[l], go(l, r - 1, 1 ^ mov) - a[r]);
return dp[l][r][mov] = ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll T = clock();
cin >> n;
memset(dp, -1, sizeof(dp));
for (ll i = 0; i < n; i++)
cin >> a[i];
cout << go(0, n - 1, 0);
cerr << "\n\nTIME: " << (double)(clock() - T) / CLOCKS_PER_SEC << " sec\n";
T = clock();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define deb(x) cout << #x << " " << x << "\n";
#define MAX 9223372036854775807
#define MIN -9223372036854775807
#define PI 3.141592653589
#define setbits(n) __builtin_popcountll(n)
const ll mod = 1e9 + 7;
const int N = 3e3 + 1;
ll dp[N][N][2];
vector<int> a(N);
ll n;
ll go(int l, int r, int mov) {
if (l > r)
return 0;
if (dp[l][r][mov] != -1)
return dp[l][r][mov];
ll ans;
if (mov == 0)
ans = max(a[l] + go(l + 1, r, 1 ^ mov), a[r] + go(l, r - 1, 1 ^ mov));
else
ans = min(go(l + 1, r, 1 ^ mov) - a[l], go(l, r - 1, 1 ^ mov) - a[r]);
return dp[l][r][mov] = ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll T = clock();
cin >> n;
memset(dp, -1, sizeof(dp));
for (ll i = 0; i < n; i++)
cin >> a[i];
cout << go(0, n - 1, 0);
cerr << "\n\nTIME: " << (double)(clock() - T) / CLOCKS_PER_SEC << " sec\n";
T = clock();
return 0;
} | replace | 12 | 13 | 12 | 13 | 0 |
TIME: 0.013744 sec
|
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#define mt make_tuple
#define ll long long
#define ld long double
#define eb emplace_back
#define fi first
#define pb push_back
// #define endl '\n'
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)((x).size()))
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define scanVec(vec) \
for (int i = 0; i < SZ(vec); i++) { \
cin >> vec[i]; \
}
#define printVec(vec) \
for (int i = 0; i < SZ(vec); i++) { \
cout << vec[i] << " "; \
}
#define mod(a, b) ((a % b + b) % b)
#define bit(x, i) (x & (1 << i)) // select the bit of position i of x
#define lowbit(x) ((x) & ((x) ^ ((x)-1))) // get the lowest bit of x
#define hBit(msb, n) \
asm("bsrl %1,%0" \
: "=r"(msb) \
: "r"(n)) // get the highest bit of x, maybe the fastest
// #define error(args...) { string _s = #args; replace(_s.begin(), _s.end(),
// ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it,
// args); }
#define IN(i, l, r) (l < i && i < r)
#define LINR(i, l, r) (l <= i && i <= r)
#define LIN(i, l, r) (l <= i && i < r)
#define INR(i, l, r) (l < i && i <= r)
#define lastEle(vec) vec[vec.size() - 1]
#define FOREACH(i, t) for (typeof(t.begin()) i = t.begin(); i != t.end(); i++)
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short
#define INF 1001001001
// #define PI 3.1415926535897932384626
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
/*
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
err(++it, args...);
}
*/
//----------------------------------------------------------------------------------------------------------------------
void swap(int &a, int &b) {
auto temp = a;
a = b;
b = temp;
}
const ll MOD = 1e9 + 7;
ll inf = 1LL * 1e17;
void no() {
cout << "NO" << endl;
// exit(0);
}
void yes() {
cout << "YES" << endl; // exit(0);
}
const ll md = 998244353;
const int MAXN = 1e3 + 10;
// pref and suff
ll dp[MAXN][MAXN];
void sl() {
int n;
cin >> n;
vector<ll> arr(n);
scanVec(arr);
for (int L = n - 1; L >= 0; L--) {
for (int R = L; R < n; R++) {
if (L == R) {
dp[L][R] = arr[L];
continue;
}
dp[L][R] = max(-dp[L + 1][R] + arr[L], -dp[L][R - 1] + arr[R]);
}
}
cout << dp[0][n - 1] << endl;
}
int main() {
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int ts = 0;
if (ts == 1)
cin >> ts;
else
ts = 1;
while (ts--)
sl();
return 0;
}
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#define mt make_tuple
#define ll long long
#define ld long double
#define eb emplace_back
#define fi first
#define pb push_back
// #define endl '\n'
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)((x).size()))
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define scanVec(vec) \
for (int i = 0; i < SZ(vec); i++) { \
cin >> vec[i]; \
}
#define printVec(vec) \
for (int i = 0; i < SZ(vec); i++) { \
cout << vec[i] << " "; \
}
#define mod(a, b) ((a % b + b) % b)
#define bit(x, i) (x & (1 << i)) // select the bit of position i of x
#define lowbit(x) ((x) & ((x) ^ ((x)-1))) // get the lowest bit of x
#define hBit(msb, n) \
asm("bsrl %1,%0" \
: "=r"(msb) \
: "r"(n)) // get the highest bit of x, maybe the fastest
// #define error(args...) { string _s = #args; replace(_s.begin(), _s.end(),
// ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it,
// args); }
#define IN(i, l, r) (l < i && i < r)
#define LINR(i, l, r) (l <= i && i <= r)
#define LIN(i, l, r) (l <= i && i < r)
#define INR(i, l, r) (l < i && i <= r)
#define lastEle(vec) vec[vec.size() - 1]
#define FOREACH(i, t) for (typeof(t.begin()) i = t.begin(); i != t.end(); i++)
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short
#define INF 1001001001
// #define PI 3.1415926535897932384626
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
/*
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
err(++it, args...);
}
*/
//----------------------------------------------------------------------------------------------------------------------
void swap(int &a, int &b) {
auto temp = a;
a = b;
b = temp;
}
const ll MOD = 1e9 + 7;
ll inf = 1LL * 1e17;
void no() {
cout << "NO" << endl;
// exit(0);
}
void yes() {
cout << "YES" << endl; // exit(0);
}
const ll md = 998244353;
const int MAXN = 3e3 + 10;
// pref and suff
ll dp[MAXN][MAXN];
void sl() {
int n;
cin >> n;
vector<ll> arr(n);
scanVec(arr);
for (int L = n - 1; L >= 0; L--) {
for (int R = L; R < n; R++) {
if (L == R) {
dp[L][R] = arr[L];
continue;
}
dp[L][R] = max(-dp[L + 1][R] + arr[L], -dp[L][R - 1] + arr[R]);
}
}
cout << dp[0][n - 1] << endl;
}
int main() {
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int ts = 0;
if (ts == 1)
cin >> ts;
else
ts = 1;
while (ts--)
sl();
return 0;
}
| replace | 90 | 91 | 90 | 91 | 0 | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define gcj "Case #"
#define adj_list vector<vi>
#define endl "\n"
#define INF_INT 2e9
#define INF_LL 2e18
#define matmax 25
#define mod 1000000007
#define mp make_pair
#define pb push_back
#define pi pair<int, int>
#define pii pair<int, pair<int, int>>
#define pl pair<ll, ll>
#define pll pair<ll, pair<ll, ll>>
#define vi vector<int>
#define vl vector<ll>
#define fastio \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
using namespace std;
typedef long long int ll;
ll pow1(ll a, ll b, ll m = mod) {
if (b == 0)
return 1ll;
else if (b == 1)
return a;
else {
ll x = pow1(a, b / 2, m);
x *= x;
x %= m;
if (b % 2) {
x *= a;
x %= m;
}
return x;
}
}
template <class avgType> avgType avg(avgType a, avgType b) {
return (a + b) / 2;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
int randInt() { return rng() % INT_MAX; }
ll randLL() { return rng64() % LLONG_MAX; }
vector<int> hashmods = {1000000007, 1000000009, 1000000021,
1000000033, 1000000087, 1000000093};
int n, a[3100], vis[2][3100][3100];
ll dp[2][3100][3100];
ll solve(int mode, int x, int y) {
if (vis[mode][x][y])
return dp[mode][x][y];
if (x > y)
return 0;
if (mode == 1)
dp[mode][x][y] = max(a[x] + solve(0, x + 1, y), a[y] + solve(0, x, y - 1));
else
dp[mode][x][y] =
min(-a[x] + solve(1, x + 1, y), -a[y] + solve(1, x, y - 1));
return dp[mode][x][y];
}
int main() {
fastio;
memset(vis, 0, sizeof(vis));
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
ll ans = solve(1, 1, n);
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#define gcj "Case #"
#define adj_list vector<vi>
#define endl "\n"
#define INF_INT 2e9
#define INF_LL 2e18
#define matmax 25
#define mod 1000000007
#define mp make_pair
#define pb push_back
#define pi pair<int, int>
#define pii pair<int, pair<int, int>>
#define pl pair<ll, ll>
#define pll pair<ll, pair<ll, ll>>
#define vi vector<int>
#define vl vector<ll>
#define fastio \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
using namespace std;
typedef long long int ll;
ll pow1(ll a, ll b, ll m = mod) {
if (b == 0)
return 1ll;
else if (b == 1)
return a;
else {
ll x = pow1(a, b / 2, m);
x *= x;
x %= m;
if (b % 2) {
x *= a;
x %= m;
}
return x;
}
}
template <class avgType> avgType avg(avgType a, avgType b) {
return (a + b) / 2;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
int randInt() { return rng() % INT_MAX; }
ll randLL() { return rng64() % LLONG_MAX; }
vector<int> hashmods = {1000000007, 1000000009, 1000000021,
1000000033, 1000000087, 1000000093};
int n, a[3100], vis[2][3100][3100];
ll dp[2][3100][3100];
ll solve(int mode, int x, int y) {
if (vis[mode][x][y])
return dp[mode][x][y];
vis[mode][x][y] = 1;
if (x > y)
return 0;
if (mode == 1)
dp[mode][x][y] = max(a[x] + solve(0, x + 1, y), a[y] + solve(0, x, y - 1));
else
dp[mode][x][y] =
min(-a[x] + solve(1, x + 1, y), -a[y] + solve(1, x, y - 1));
return dp[mode][x][y];
}
int main() {
fastio;
memset(vis, 0, sizeof(vis));
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
ll ans = solve(1, 1, n);
cout << ans << endl;
return 0;
} | insert | 60 | 60 | 60 | 61 | TLE | |
p03171 | C++ | Runtime Error | #include <iomanip>
#include <iostream>
using namespace std;
const long long MAXN = 3001;
long long dp[MAXN][MAXN][2];
long long arr[MAXN];
long long f(long long x, long long y, long long turn) {
if (dp[x][y][turn]) {
return dp[x][y][turn];
}
if (x > y) {
return 0;
}
// cout<<x<<" "<<y<<" "<<turn<<endl;
if (turn % 2 == 1) {
return dp[x][y][turn] = max(arr[x] + f(x + 1, y, 1 - turn),
arr[y] + f(x, y - 1, 1 - turn));
} else {
return dp[x][y][turn] = min(f(x + 1, y, 1 - turn) - arr[x],
f(x, y - 1, 1 - turn) - arr[y]);
}
}
int main() {
long long n;
cin >> n;
for (long long i = 1; i <= n; i++) {
cin >> arr[i];
}
cout << f(1, n, 1) << endl;
}
| #include <iomanip>
#include <iostream>
using namespace std;
const long long MAXN = 3010;
long long dp[MAXN][MAXN][2];
long long arr[MAXN];
long long f(long long x, long long y, long long turn) {
if (dp[x][y][turn]) {
return dp[x][y][turn];
}
if (x > y) {
return 0;
}
// cout<<x<<" "<<y<<" "<<turn<<endl;
if (turn % 2 == 1) {
return dp[x][y][turn] = max(arr[x] + f(x + 1, y, 1 - turn),
arr[y] + f(x, y - 1, 1 - turn));
} else {
return dp[x][y][turn] = min(f(x + 1, y, 1 - turn) - arr[x],
f(x, y - 1, 1 - turn) - arr[y]);
}
}
int main() {
long long n;
cin >> n;
for (long long i = 1; i <= n; i++) {
cin >> arr[i];
}
cout << f(1, n, 1) << endl;
}
| replace | 4 | 5 | 4 | 5 | -11 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define mod 1000000007
#define dbg(n) cout << #n << ' ' << n << endl;
ll dp[101][101];
ll solve(int i, int j, ll a[]) {
if (i == j)
return dp[i][j] = a[i];
else if (j == i + 1)
return dp[i][j] = max(a[i], a[j]);
else if (dp[i][j] != 0)
return dp[i][j];
return dp[i][j] = max(a[i] + min(solve(i + 2, j, a), solve(i + 1, j - 1, a)),
a[j] + min(solve(i, j - 2, a), solve(i + 1, j - 1, a)));
}
int main() {
for (int i = 0; i < 101; i++)
for (int j = 0; j < 101; j++)
dp[i][j] = 0;
int n;
cin >> n;
ll a[n];
ll sum = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
ll mx = solve(0, n - 1, a);
ll ans = 2 * mx - sum;
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define mod 1000000007
#define dbg(n) cout << #n << ' ' << n << endl;
ll dp[3001][3001];
ll solve(int i, int j, ll a[]) {
if (i == j)
return dp[i][j] = a[i];
else if (j == i + 1)
return dp[i][j] = max(a[i], a[j]);
else if (dp[i][j] != 0)
return dp[i][j];
return dp[i][j] = max(a[i] + min(solve(i + 2, j, a), solve(i + 1, j - 1, a)),
a[j] + min(solve(i, j - 2, a), solve(i + 1, j - 1, a)));
}
int main() {
for (int i = 0; i < 101; i++)
for (int j = 0; j < 101; j++)
dp[i][j] = 0;
int n;
cin >> n;
ll a[n];
ll sum = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
ll mx = solve(0, n - 1, a);
ll ans = 2 * mx - sum;
cout << ans << endl;
}
| replace | 9 | 10 | 9 | 10 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int MAX = 3005;
int64_t dp[MAX][MAX];
vector<int> vals(4);
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> vals[i];
for (int L = n - 1; L >= 0; L--) {
for (int R = L; R < n; R++) {
if (L != R) {
dp[L][R] = max(vals[L] - dp[L + 1][R], vals[R] - dp[L][R - 1]);
} else {
dp[L][R] = vals[L];
}
}
}
cout << dp[0][n - 1];
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int MAX = 3005;
int64_t dp[MAX][MAX];
vector<int> vals(MAX, 0);
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> vals[i];
for (int L = n - 1; L >= 0; L--) {
for (int R = L; R < n; R++) {
if (L != R) {
dp[L][R] = max(vals[L] - dp[L + 1][R], vals[R] - dp[L][R - 1]);
} else {
dp[L][R] = vals[L];
}
}
}
cout << dp[0][n - 1];
return 0;
}
| replace | 6 | 7 | 6 | 7 | 0 | |
p03171 | C++ | Time Limit Exceeded | /*input
6
4 2 9 7 1 5
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 3003;
vector<ll> v;
ll dp[N][N];
ll func(int l, int r) {
if (l > r) {
return 0;
}
if (dp[l][r] > -1) {
return dp[l][r];
}
ll a = v[l] + min(func(l + 2, r), func(l + 1, r - 1));
ll b = v[r] + min(func(l + 1, r - 1), func(l, r - 2));
return max(a, b);
}
int main() {
int n;
cin >> n;
v.resize(n);
ll sum = 0;
for (auto &x : v) {
cin >> x;
sum += x;
}
memset(dp, -1, sizeof(dp));
ll d = func(0, n - 1);
sum -= d;
cout << d - sum;
return 0;
} | /*input
6
4 2 9 7 1 5
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 3003;
vector<ll> v;
ll dp[N][N];
ll func(int l, int r) {
if (l > r) {
return 0;
}
if (dp[l][r] > -1) {
return dp[l][r];
}
ll a = v[l] + min(func(l + 2, r), func(l + 1, r - 1));
ll b = v[r] + min(func(l + 1, r - 1), func(l, r - 2));
return dp[l][r] = max(a, b);
}
int main() {
int n;
cin >> n;
v.resize(n);
ll sum = 0;
for (auto &x : v) {
cin >> x;
sum += x;
}
memset(dp, -1, sizeof(dp));
ll d = func(0, n - 1);
sum -= d;
cout << d - sum;
return 0;
} | replace | 23 | 24 | 23 | 24 | TLE | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int mxN = 3000;
int n, a[mxN];
long long dp[mxN][mxN + 1];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; ++i)
cin >> a[i];
for (int i = n - 1; i >= 0; --i) {
for (int j = i + 1; j <= n; ++j) {
dp[i][j] = max(a[i] - dp[i + 1][j], a[j - 1] - dp[i][j - 1]);
}
}
cout << dp[0][n];
} | #include <bits/stdc++.h>
using namespace std;
const int mxN = 3000;
int n, a[mxN];
long long dp[mxN + 1][mxN + 1];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; ++i)
cin >> a[i];
for (int i = n - 1; i >= 0; --i) {
for (int j = i + 1; j <= n; ++j) {
dp[i][j] = max(a[i] - dp[i + 1][j], a[j - 1] - dp[i][j - 1]);
}
}
cout << dp[0][n];
}
| replace | 5 | 6 | 5 | 6 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
#define ll long long int
#define ar array
#define ld long double
#define bigint int64_t
#define vll vector<ll>
#define pll ar<ll, 3>
#define vpll vector<pll>
#define pb push_back
#define sz(x) (int)(x).size()
#define ff first
#define MOD 1000000007
#define ss second
#define en cout << "\n"
#define LCM(a, b) (a * (b / __gcd(a, b)))
#define all(v) v.begin(), v.end()
#define maxqueue priority_queue<int>
#define minqueue priority_queue<int, vector<int>, greater<int>>
#define lb(v, val) (lower_bound(v.begin(), v.end(), val) - v.begin())
#define ub(v, val) (upper_bound(v.begin(), v.end(), val) - v.begin())
#define INF (ll)(1e18)
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define ini(a, b) memset(a, b, sizeof(a))
#define vvll vector<vector<ll>>
#define fast \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define MEM(a, b) memset(a, b, sizeof(a))
#define MAXN (int)(1 * 1e6 + 10)
#define prec(n) fixed << setprecision(n)
#define deb1(x) cerr << #x << ": " << x << endl
#define deb2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define deb3(x, y, z) \
cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": " \
<< z << endl
#define deb4(a, b, c, d) \
cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << endl
#define deb5(a, b, c, d, e) \
cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define deb6(a, b, c, d, e, f) \
cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " \
<< #f << ": " << f << endl
#define GET5(a, b, c, d, e, ...) e
#define D_EBC(...) GET5(__VA_ARGS__, deb4, deb3, deb2, deb1)
#define deb(...) D_EBC(__VA_ARGS__)(__VA_ARGS__)
#define each(x, a) for (auto &x : a)
#define F_OR(i, a, b, s) \
for (int i = (a); ((s) > 0 ? i < (b) : i > (b)); i += (s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define forn(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag,
// tree_order_statistics_node_update>; template <typename T> using mset =
// tree<T, null_type, less_equal<T>, rb_tree_tag,
// tree_order_statistics_node_update>; find_by_order(k) : iterator to value at
// kth index in sorted array. order_of_key(k) : Index of the value k
template <typename T> void read(T &x) { cin >> x; }
template <typename T, typename T0> void read(T &x, T0 &y) { cin >> x >> y; }
template <typename T, typename T0, typename T1> void read(T &x, T0 &y, T1 &z) {
cin >> x >> y >> z;
}
template <typename T, typename T0, typename T1, typename T2>
void read(T &x, T0 &y, T1 &z, T2 &w) {
cin >> x >> y >> z >> w;
}
template <typename T, typename T0> void read(pair<T, T0> &p) {
cin >> p.fst >> p.scd;
}
template <typename T> void read(vector<T> &oneD, ll n) {
for (ll i = 0; i < n; i++) {
read(oneD[i]);
}
}
template <typename T> void read(T oneD[], ll n) {
for (ll i = 0; i < n; i++) {
read(oneD[i]);
}
}
template <typename T> void write(T &x) { cout << x << " "; }
template <typename T, typename T0> void write(T &x, T0 &y) {
cout << x << " " << y << "\n";
}
template <typename T, typename T0, typename T1> void write(T &x, T0 &y, T1 &z) {
cout << x << " " << y << " " << z << "\n";
}
template <typename T, typename T0, typename T1, typename T2>
void write(T &x, T0 &y, T1 &z, T2 &w) {
cout << x << " " << y << " " << z << " " << w << "\n";
}
template <typename T, typename T0> void write(pair<T, T0> &p) {
write(p.fst);
write(p.scd);
cout << endl;
}
template <typename T> void write(vector<T> &oneD) {
for (ll i = 0; i < oneD.size(); i++) {
write(oneD[i]);
}
cout << endl;
}
template <typename T> void write(T oneD[], int n) {
for (ll i = 0; i < n; i++) {
write(oneD[i]);
}
cout << endl;
}
template <typename T, typename T0> void write(map<T, T0> &mpp) {
for (auto it : mpp) {
write(it.fst);
cout << ": ";
write(it.scd);
cout << "\n";
}
cout << endl;
}
template <typename T> inline bool umax(T &x, T y) {
return (y > x) ? x = y, true : false;
}
template <typename T> inline bool umin(T &x, T y) {
return (y < x) ? x = y, true : false;
}
ll FIRSTTRUE(function<bool(ll)> f, ll lb, ll rb) {
while (lb < rb) {
ll mb = (lb + rb) / 2;
f(mb) ? rb = mb : lb = mb + 1;
}
return lb;
}
ll LASTTRUE(function<bool(ll)> f, ll lb, ll rb) {
while (lb < rb) {
ll mb = (lb + rb + 1) / 2;
f(mb) ? lb = mb : rb = mb - 1;
}
return lb;
}
inline bool isSquare(ll x) {
ll s = sqrt(x);
return (s * s == x);
}
inline bool isPowerOfTwo(ll x) { return ((1LL << (ll)log2(x)) == x); }
ll extended_GCD(ll a, ll b, ll &x, ll &y) {
if (a == 0) {
x = 0;
y = 1;
return b;
}
ll x1, y1;
ll gcd = extended_GCD(b % a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
return gcd;
}
vector<ll> prime;
void sieve(ll N) {
bool isPrime[N + !1];
for (ll i = 0; i <= N; ++i) {
isPrime[i] = true;
}
isPrime[0] = false;
isPrime[1] = false;
for (ll i = 2; i * i <= N; ++i) {
if (isPrime[i] == true) {
for (ll j = i * i; j <= N; j += i)
isPrime[j] = false;
}
}
for (ll i = 2; i <= N; i++) {
if (isPrime[i] == true) {
prime.push_back(i);
}
}
}
ll minPrime[MAXN];
void factorSieve(ll n) {
memset(minPrime, 0, sizeof(minPrime));
for (ll i = 2; i * i <= n; ++i) {
if (minPrime[i] == 0) {
for (ll j = i * i; j <= n; j += i) {
if (minPrime[j] == 0) {
minPrime[j] = i;
}
}
}
}
for (ll i = 2; i <= n; ++i) {
if (minPrime[i] == 0) {
minPrime[i] = i;
}
}
}
ll factorize(ll n) {
map<ll, ll> m;
m.clear();
while (n != 1) {
m[minPrime[n]]++;
n /= minPrime[n];
}
for (auto i : m) {
if (i.second > 1) {
return 0;
}
}
return 1;
}
ll modinv(ll a, ll mod = MOD) {
ll x, y, g;
g = extended_GCD(a, mod, x, y);
if (g != 1)
return -1;
else {
if (x < 0)
x += mod;
return x;
}
}
ll power(ll a, ll b, ll m = MOD) {
a %= m;
ll res = 1;
while (b > 0) {
if (b & 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
string to_string(char c) { return string(1, c); }
struct comp {
bool operator()(pair<ll, ll> a, pair<ll, ll> b) {
if (a.first == b.first) {
return a.second > b.second;
}
return a.first < b.first;
}
};
bool cmp(pair<ll, ll> a, pair<ll, ll> b) {
if (a.first == b.first) {
return a.second > b.second;
}
return a.first < b.first;
}
// bool safe(ll i, ll j) { return i >= 0 && i < n && j >= 0 && j < m; }
const ll N = 1e3;
ll dp[N + 1][N + 1];
void solve() {
ll n;
read(n);
ll arr[n], sum = 0;
read(arr, n);
forn(i, n) sum += arr[i];
forn(i, n - 1, -1, -1) {
forn(j, n) {
if (i == 0 && j == 0)
dp[i][j] = arr[0];
else if (i <= j) {
dp[i][j] = max(-dp[i + 1][j] + arr[i], -dp[i][j - 1] + arr[j]);
}
}
}
cout << dp[0][n - 1];
}
int main() {
fast;
int t = 1;
// read(t);
forn(t) {
// cout << "Case #" << i+1 << ": ";
solve();
}
} | #include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
#define ll long long int
#define ar array
#define ld long double
#define bigint int64_t
#define vll vector<ll>
#define pll ar<ll, 3>
#define vpll vector<pll>
#define pb push_back
#define sz(x) (int)(x).size()
#define ff first
#define MOD 1000000007
#define ss second
#define en cout << "\n"
#define LCM(a, b) (a * (b / __gcd(a, b)))
#define all(v) v.begin(), v.end()
#define maxqueue priority_queue<int>
#define minqueue priority_queue<int, vector<int>, greater<int>>
#define lb(v, val) (lower_bound(v.begin(), v.end(), val) - v.begin())
#define ub(v, val) (upper_bound(v.begin(), v.end(), val) - v.begin())
#define INF (ll)(1e18)
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define ini(a, b) memset(a, b, sizeof(a))
#define vvll vector<vector<ll>>
#define fast \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define MEM(a, b) memset(a, b, sizeof(a))
#define MAXN (int)(1 * 1e6 + 10)
#define prec(n) fixed << setprecision(n)
#define deb1(x) cerr << #x << ": " << x << endl
#define deb2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define deb3(x, y, z) \
cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": " \
<< z << endl
#define deb4(a, b, c, d) \
cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << endl
#define deb5(a, b, c, d, e) \
cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define deb6(a, b, c, d, e, f) \
cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " \
<< #f << ": " << f << endl
#define GET5(a, b, c, d, e, ...) e
#define D_EBC(...) GET5(__VA_ARGS__, deb4, deb3, deb2, deb1)
#define deb(...) D_EBC(__VA_ARGS__)(__VA_ARGS__)
#define each(x, a) for (auto &x : a)
#define F_OR(i, a, b, s) \
for (int i = (a); ((s) > 0 ? i < (b) : i > (b)); i += (s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define forn(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag,
// tree_order_statistics_node_update>; template <typename T> using mset =
// tree<T, null_type, less_equal<T>, rb_tree_tag,
// tree_order_statistics_node_update>; find_by_order(k) : iterator to value at
// kth index in sorted array. order_of_key(k) : Index of the value k
template <typename T> void read(T &x) { cin >> x; }
template <typename T, typename T0> void read(T &x, T0 &y) { cin >> x >> y; }
template <typename T, typename T0, typename T1> void read(T &x, T0 &y, T1 &z) {
cin >> x >> y >> z;
}
template <typename T, typename T0, typename T1, typename T2>
void read(T &x, T0 &y, T1 &z, T2 &w) {
cin >> x >> y >> z >> w;
}
template <typename T, typename T0> void read(pair<T, T0> &p) {
cin >> p.fst >> p.scd;
}
template <typename T> void read(vector<T> &oneD, ll n) {
for (ll i = 0; i < n; i++) {
read(oneD[i]);
}
}
template <typename T> void read(T oneD[], ll n) {
for (ll i = 0; i < n; i++) {
read(oneD[i]);
}
}
template <typename T> void write(T &x) { cout << x << " "; }
template <typename T, typename T0> void write(T &x, T0 &y) {
cout << x << " " << y << "\n";
}
template <typename T, typename T0, typename T1> void write(T &x, T0 &y, T1 &z) {
cout << x << " " << y << " " << z << "\n";
}
template <typename T, typename T0, typename T1, typename T2>
void write(T &x, T0 &y, T1 &z, T2 &w) {
cout << x << " " << y << " " << z << " " << w << "\n";
}
template <typename T, typename T0> void write(pair<T, T0> &p) {
write(p.fst);
write(p.scd);
cout << endl;
}
template <typename T> void write(vector<T> &oneD) {
for (ll i = 0; i < oneD.size(); i++) {
write(oneD[i]);
}
cout << endl;
}
template <typename T> void write(T oneD[], int n) {
for (ll i = 0; i < n; i++) {
write(oneD[i]);
}
cout << endl;
}
template <typename T, typename T0> void write(map<T, T0> &mpp) {
for (auto it : mpp) {
write(it.fst);
cout << ": ";
write(it.scd);
cout << "\n";
}
cout << endl;
}
template <typename T> inline bool umax(T &x, T y) {
return (y > x) ? x = y, true : false;
}
template <typename T> inline bool umin(T &x, T y) {
return (y < x) ? x = y, true : false;
}
ll FIRSTTRUE(function<bool(ll)> f, ll lb, ll rb) {
while (lb < rb) {
ll mb = (lb + rb) / 2;
f(mb) ? rb = mb : lb = mb + 1;
}
return lb;
}
ll LASTTRUE(function<bool(ll)> f, ll lb, ll rb) {
while (lb < rb) {
ll mb = (lb + rb + 1) / 2;
f(mb) ? lb = mb : rb = mb - 1;
}
return lb;
}
inline bool isSquare(ll x) {
ll s = sqrt(x);
return (s * s == x);
}
inline bool isPowerOfTwo(ll x) { return ((1LL << (ll)log2(x)) == x); }
ll extended_GCD(ll a, ll b, ll &x, ll &y) {
if (a == 0) {
x = 0;
y = 1;
return b;
}
ll x1, y1;
ll gcd = extended_GCD(b % a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
return gcd;
}
vector<ll> prime;
void sieve(ll N) {
bool isPrime[N + !1];
for (ll i = 0; i <= N; ++i) {
isPrime[i] = true;
}
isPrime[0] = false;
isPrime[1] = false;
for (ll i = 2; i * i <= N; ++i) {
if (isPrime[i] == true) {
for (ll j = i * i; j <= N; j += i)
isPrime[j] = false;
}
}
for (ll i = 2; i <= N; i++) {
if (isPrime[i] == true) {
prime.push_back(i);
}
}
}
ll minPrime[MAXN];
void factorSieve(ll n) {
memset(minPrime, 0, sizeof(minPrime));
for (ll i = 2; i * i <= n; ++i) {
if (minPrime[i] == 0) {
for (ll j = i * i; j <= n; j += i) {
if (minPrime[j] == 0) {
minPrime[j] = i;
}
}
}
}
for (ll i = 2; i <= n; ++i) {
if (minPrime[i] == 0) {
minPrime[i] = i;
}
}
}
ll factorize(ll n) {
map<ll, ll> m;
m.clear();
while (n != 1) {
m[minPrime[n]]++;
n /= minPrime[n];
}
for (auto i : m) {
if (i.second > 1) {
return 0;
}
}
return 1;
}
ll modinv(ll a, ll mod = MOD) {
ll x, y, g;
g = extended_GCD(a, mod, x, y);
if (g != 1)
return -1;
else {
if (x < 0)
x += mod;
return x;
}
}
ll power(ll a, ll b, ll m = MOD) {
a %= m;
ll res = 1;
while (b > 0) {
if (b & 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
string to_string(char c) { return string(1, c); }
struct comp {
bool operator()(pair<ll, ll> a, pair<ll, ll> b) {
if (a.first == b.first) {
return a.second > b.second;
}
return a.first < b.first;
}
};
bool cmp(pair<ll, ll> a, pair<ll, ll> b) {
if (a.first == b.first) {
return a.second > b.second;
}
return a.first < b.first;
}
// bool safe(ll i, ll j) { return i >= 0 && i < n && j >= 0 && j < m; }
const ll N = 3e3;
ll dp[N + 1][N + 1];
void solve() {
ll n;
read(n);
ll arr[n], sum = 0;
read(arr, n);
forn(i, n) sum += arr[i];
forn(i, n - 1, -1, -1) {
forn(j, n) {
if (i == 0 && j == 0)
dp[i][j] = arr[0];
else if (i <= j) {
dp[i][j] = max(-dp[i + 1][j] + arr[i], -dp[i][j - 1] + arr[j]);
}
}
}
cout << dp[0][n - 1];
}
int main() {
fast;
int t = 1;
// read(t);
forn(t) {
// cout << "Case #" << i+1 << ": ";
solve();
}
} | replace | 290 | 291 | 290 | 291 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
#define vi vector<int>
#define rep(i, xtz, n) for (int i = xtz; i < n; i++)
#define repx(i, xtz, n) for (int i = xtz; i >= n; i--)
#define lli long long int
using namespace std;
lli dp[305][305];
int main() {
// freopen("i.in", "r", stdin);
int n;
scanf("%d", &n);
vector<lli> vec(n);
rep(i, 0, n) scanf("%d", &vec[i]);
repx(L, n - 1, 0) {
rep(R, L, n) {
if (L == R)
dp[L][R] = vec[L];
else {
dp[L][R] = max(vec[L] - dp[L + 1][R], vec[R] - dp[L][R - 1]);
}
}
}
cout << dp[0][n - 1];
} | #include <bits/stdc++.h>
#define vi vector<int>
#define rep(i, xtz, n) for (int i = xtz; i < n; i++)
#define repx(i, xtz, n) for (int i = xtz; i >= n; i--)
#define lli long long int
using namespace std;
lli dp[3005][3005];
int main() {
// freopen("i.in", "r", stdin);
int n;
scanf("%d", &n);
vector<lli> vec(n);
rep(i, 0, n) scanf("%d", &vec[i]);
repx(L, n - 1, 0) {
rep(R, L, n) {
if (L == R)
dp[L][R] = vec[L];
else {
dp[L][R] = max(vec[L] - dp[L + 1][R], vec[R] - dp[L][R - 1]);
}
}
}
cout << dp[0][n - 1];
} | replace | 8 | 9 | 8 | 9 | 0 | |
p03171 | C++ | Runtime Error | #pragma GCC optimize "trapv"
#include <bits/stdc++.h>
#define faster \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
using namespace std;
#define ll long long
#define pb push_back
#define fo(i, a, b) for (int i = a; i < b; i++)
#define foe(i, a, b) for (int i = a; i <= b; i++)
ll dp[1000][1000];
ll fundp(int i, int j, ll *a) {
// cout<<i<<" "<<j<<endl;
if (dp[i][j] != 1000000000000001)
return dp[i][j];
if (i == j)
dp[i][j] = a[i];
else {
dp[i][j] = max(a[i] - fundp(i + 1, j, a), a[j] - fundp(i, j - 1, a));
}
return dp[i][j];
}
void solve() {
int n;
cin >> n;
ll a[n];
fo(i, 0, n) cin >> a[i];
fo(i, 0, n) { fo(j, 0, n) dp[i][j] = 1000000000000001; }
cout << fundp(0, n - 1, a) << endl;
}
signed main() {
faster;
// int t;cin>>t;
// while(t--)
solve();
} | #pragma GCC optimize "trapv"
#include <bits/stdc++.h>
#define faster \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
using namespace std;
#define ll long long
#define pb push_back
#define fo(i, a, b) for (int i = a; i < b; i++)
#define foe(i, a, b) for (int i = a; i <= b; i++)
ll dp[3000][3000];
ll fundp(int i, int j, ll *a) {
// cout<<i<<" "<<j<<endl;
if (dp[i][j] != 1000000000000001)
return dp[i][j];
if (i == j)
dp[i][j] = a[i];
else {
dp[i][j] = max(a[i] - fundp(i + 1, j, a), a[j] - fundp(i, j - 1, a));
}
return dp[i][j];
}
void solve() {
int n;
cin >> n;
ll a[n];
fo(i, 0, n) cin >> a[i];
fo(i, 0, n) { fo(j, 0, n) dp[i][j] = 1000000000000001; }
cout << fundp(0, n - 1, a) << endl;
}
signed main() {
faster;
// int t;cin>>t;
// while(t--)
solve();
} | replace | 11 | 12 | 11 | 12 | 0 | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
// #define INF 1145141919810893364
// #define INF 810114514
#define PI 3.141592653589
typedef pair<int, int> PP;
typedef long long ll;
#define int ll
#define setdouble setprecision
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define GOODBYE \
do { \
cout << "0" << endl; \
return 0; \
} while (false)
#define MM << " " <<
#define Endl endl
#define debug false
int N;
vector<int> A;
const int MEM = 3030;
int dp[MEM][MEM];
int dequeue(int l, int r) {
if (dp[l][r] != -1) {
return dp[l][r];
}
if (l >= r) {
return A[l];
}
return max(A[l] - dequeue(l + 1, r), A[r] - dequeue(l, r - 1));
}
signed main(void) {
int a;
cin >> N;
REP(i, N) {
cin >> a;
A.push_back(a);
}
REP(i, MEM) {
REP(j, MEM) { dp[i][j] = -1; }
}
cout << dequeue(0, N - 1) << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
// #define INF 1145141919810893364
// #define INF 810114514
#define PI 3.141592653589
typedef pair<int, int> PP;
typedef long long ll;
#define int ll
#define setdouble setprecision
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define GOODBYE \
do { \
cout << "0" << endl; \
return 0; \
} while (false)
#define MM << " " <<
#define Endl endl
#define debug false
int N;
vector<int> A;
const int MEM = 3030;
int dp[MEM][MEM];
int dequeue(int l, int r) {
if (dp[l][r] != -1) {
return dp[l][r];
}
if (l >= r) {
return A[l];
}
int k = max(A[l] - dequeue(l + 1, r), A[r] - dequeue(l, r - 1));
dp[l][r] = k;
return k;
}
signed main(void) {
int a;
cin >> N;
REP(i, N) {
cin >> a;
A.push_back(a);
}
REP(i, MEM) {
REP(j, MEM) { dp[i][j] = -1; }
}
cout << dequeue(0, N - 1) << endl;
return 0;
}
| replace | 36 | 38 | 36 | 39 | TLE | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int n;
vector<int> a;
vector<vector<bool>> vis;
vector<vector<int64_t>> dp;
int64_t go(int i, int j, int p) {
if (vis[i][j])
return dp[i][j];
if (i == j)
return dp[i][j] = p * a[i];
int64_t x = go(i + 1, j, -p) + p * a[i];
int64_t y = go(i, j - 1, -p) + p * a[j];
return dp[i][j] = p * x > p * y ? x : y;
}
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
cin >> n;
a.resize(n);
vis.assign(n, vector<bool>(n, false));
dp.assign(n, vector<int64_t>(n));
for (int &it : a)
cin >> it;
// int64_t ans = go(0, n - 1, 1);
cout << go(0, n - 1, 1) << '\n';
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int n;
vector<int> a;
vector<vector<bool>> vis;
vector<vector<int64_t>> dp;
int64_t go(int i, int j, int p) {
if (vis[i][j])
return dp[i][j];
if (i == j)
return dp[i][j] = p * a[i];
vis[i][j] = true;
int64_t x = go(i + 1, j, -p) + p * a[i];
int64_t y = go(i, j - 1, -p) + p * a[j];
return dp[i][j] = p * x > p * y ? x : y;
}
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
cin >> n;
a.resize(n);
vis.assign(n, vector<bool>(n, false));
dp.assign(n, vector<int64_t>(n));
for (int &it : a)
cin >> it;
// int64_t ans = go(0, n - 1, 1);
cout << go(0, n - 1, 1) << '\n';
return 0;
} | insert | 12 | 12 | 12 | 13 | TLE | |
p03171 | C++ | Runtime Error | #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define speed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std;
const ll N = 1e2 + 1;
ll n, k;
ll arr[N][N];
bool kek[N][N];
ll a[N];
ll recurs(ll l = 0, ll r = n - 1) {
if (l == r) {
arr[l][r] = a[l];
return a[l];
}
if (kek[l][r])
return arr[l][r];
kek[l][r] = 1;
arr[l][r] = max(a[l] - recurs(l + 1, r), a[r] - recurs(l, r - 1));
return arr[l][r];
}
int main() {
speed;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
recurs();
cout << arr[0][n - 1];
}
/*
Timus : 288471KD
Atcoder : Zhabka
Codeforces : Zhabka
mail : [email protected]
*/
| #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define speed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std;
const ll N = 3e3 + 1;
ll n, k;
ll arr[N][N];
bool kek[N][N];
ll a[N];
ll recurs(ll l = 0, ll r = n - 1) {
if (l == r) {
arr[l][r] = a[l];
return a[l];
}
if (kek[l][r])
return arr[l][r];
kek[l][r] = 1;
arr[l][r] = max(a[l] - recurs(l + 1, r), a[r] - recurs(l, r - 1));
return arr[l][r];
}
int main() {
speed;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
recurs();
cout << arr[0][n - 1];
}
/*
Timus : 288471KD
Atcoder : Zhabka
Codeforces : Zhabka
mail : [email protected]
*/
| replace | 7 | 8 | 7 | 8 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
#define int long long
#define ld long double
#define MAX 100001
#define MOD 1000000007
using namespace std;
int dp[1001][1001][2];
void play(int s, int e, int turn, int *a) {
if (dp[s][e][turn] != -1)
return;
if (s == e) {
dp[s][e][turn] = a[s];
dp[s][e][turn ^ 1] = 0;
return;
}
play(s + 1, e, turn ^ 1, a);
play(s, e - 1, turn ^ 1, a);
if (dp[s + 1][e][turn] + a[s] > dp[s][e - 1][turn] + a[e]) {
dp[s][e][turn] = dp[s + 1][e][turn] + a[s];
dp[s][e][turn ^ 1] = dp[s + 1][e][turn ^ 1];
} else {
dp[s][e][turn] = dp[s][e - 1][turn] + a[e];
dp[s][e][turn ^ 1] = dp[s][e - 1][turn ^ 1];
}
return;
}
int32_t main() {
int n;
cin >> n;
int *a = new int[n];
for (int i = 0; i < n; ++i)
cin >> a[i];
memset(dp, -1, sizeof(dp));
play(0, n - 1, 0, a);
cout << dp[0][n - 1][0] - dp[0][n - 1][1] << endl;
return 0;
} | #include <bits/stdc++.h>
#define int long long
#define ld long double
#define MAX 100001
#define MOD 1000000007
using namespace std;
int dp[3001][3001][2];
void play(int s, int e, int turn, int *a) {
if (dp[s][e][turn] != -1)
return;
if (s == e) {
dp[s][e][turn] = a[s];
dp[s][e][turn ^ 1] = 0;
return;
}
play(s + 1, e, turn ^ 1, a);
play(s, e - 1, turn ^ 1, a);
if (dp[s + 1][e][turn] + a[s] > dp[s][e - 1][turn] + a[e]) {
dp[s][e][turn] = dp[s + 1][e][turn] + a[s];
dp[s][e][turn ^ 1] = dp[s + 1][e][turn ^ 1];
} else {
dp[s][e][turn] = dp[s][e - 1][turn] + a[e];
dp[s][e][turn ^ 1] = dp[s][e - 1][turn ^ 1];
}
return;
}
int32_t main() {
int n;
cin >> n;
int *a = new int[n];
for (int i = 0; i < n; ++i)
cin >> a[i];
memset(dp, -1, sizeof(dp));
play(0, n - 1, 0, a);
cout << dp[0][n - 1][0] - dp[0][n - 1][1] << endl;
return 0;
} | replace | 7 | 8 | 7 | 8 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
long long n, a[3001], dp[3001][3001];
int main() {
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == 1)
dp[i][j] = a[j];
else if (i == 2)
dp[i][j] = abs(a[j] - a[j + 1]);
else {
int l = j;
int r = i + j - 1;
long long t1 = a[r] - dp[i - 1][j];
long long t2 = a[l] - dp[i - 1][j + 1];
dp[i][j] = max(t1, t2);
}
}
}
cout << dp[n][1];
} | #include <bits/stdc++.h>
using namespace std;
long long n, a[3001], dp[3001][3001];
int main() {
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i + j - 1 > n)
break;
if (i == 1)
dp[i][j] = a[j];
else if (i == 2)
dp[i][j] = abs(a[j] - a[j + 1]);
else {
int l = j;
int r = i + j - 1;
long long t1 = a[r] - dp[i - 1][j];
long long t2 = a[l] - dp[i - 1][j + 1];
dp[i][j] = max(t1, t2);
}
}
}
cout << dp[n][1];
} | insert | 9 | 9 | 9 | 11 | 0 | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define tani_nachi_ke \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define M_PI 3.14159265358979323846
#define data data_
#define ff first
#define ss second
int const N = 3009;
long long dp[N][N];
long long a[N];
long long fun(int i, int j) {
if (i == j)
return a[i];
else if (j == i + 1)
return (a[i] > a[j] ? (a[i] - a[j]) : (a[j] - a[i]));
else if (dp[i][j] != -1)
return dp[i][j];
else
return max(a[i] + min(-a[i + 1] + fun(i + 2, j), -a[j] + fun(i + 1, j - 1)),
a[j] +
min(-a[i] + fun(i + 1, j - 1), -a[j - 1] + fun(i, j - 2)));
}
int main() {
// clock_t begin = clock();
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
tani_nachi_ke
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
memset(dp, -1, sizeof(dp));
cout << fun(1, n) << endl;
// #ifndef ONLINE_JUDGE
// clock_t end = clock();
// cout<<endl<<double(end - begin) / CLOCKS_PER_SEC*1000<<" ms";
// #endif
// return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define tani_nachi_ke \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define M_PI 3.14159265358979323846
#define data data_
#define ff first
#define ss second
int const N = 3009;
long long dp[N][N];
long long a[N];
long long fun(int i, int j) {
if (i == j)
return a[i];
else if (j == i + 1)
return (a[i] > a[j] ? (a[i] - a[j]) : (a[j] - a[i]));
else if (dp[i][j] != -1)
return dp[i][j];
else
return dp[i][j] = max(
a[i] + min(-a[i + 1] + fun(i + 2, j), -a[j] + fun(i + 1, j - 1)),
a[j] +
min(-a[i] + fun(i + 1, j - 1), -a[j - 1] + fun(i, j - 2)));
}
int main() {
// clock_t begin = clock();
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
tani_nachi_ke
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
memset(dp, -1, sizeof(dp));
cout << fun(1, n) << endl;
// #ifndef ONLINE_JUDGE
// clock_t end = clock();
// cout<<endl<<double(end - begin) / CLOCKS_PER_SEC*1000<<" ms";
// #endif
// return 0;
} | replace | 20 | 21 | 20 | 22 | TLE | |
p03171 | C++ | Time Limit Exceeded | // May this submission get accepted!
#pragma GCC optimize("O3")
#pragma GCC target("tune=native")
#pragma GCC target("avx")
#include <bits/stdc++.h>
// 汎用マクロ
#define ALL_OF(x) (x).begin(), (x).end()
#define REP(i, n) for (long long i = 0, i##_len = (n); i < i##_len; i++)
#define RANGE(i, is, ie) \
for (long long i = (is), i##_end = (ie); i <= i##_end; i++)
#define DSRNG(i, is, ie) \
for (long long i = (is), i##_end = (ie); i >= i##_end; i--)
#define UNIQUE(v) \
do { \
sort((v).begin(), (v).end()); \
(v).erase(unique((v).begin(), (v).end()), (v).end()); \
} while (false)
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
#define INF 0x7FFFFFFF
#define LINF 0x7FFFFFFFFFFFFFFFLL
#define Yes(q) ((q) ? "Yes" : "No")
#define YES(q) ((q) ? "YES" : "NO")
#define Possible(q) ((q) ? "Possible" : "Impossible")
#define POSSIBLE(q) ((q) ? "POSSIBLE" : "IMPOSSIBLE")
#define DUMP(q) \
cerr << "[DEBUG] " #q ": " << (q) << " at " __FILE__ ":" << __LINE__ << endl
#define DUMPALL(q) \
do { \
cerr << "[DEBUG] " #q ": ["; \
REP(i, (q).size()) { cerr << (q)[i] << (i == i_len - 1 ? "" : ", "); } \
cerr << "] at " __FILE__ ":" << __LINE__ << endl; \
} while (false)
template <class T> T gcd(const T &a, const T &b) {
return a < b ? gcd(b, a) : b ? gcd(b, a % b) : a;
}
template <class T> T lcm(const T &a, const T &b) { return a / gcd(a, b) * b; }
// gcc拡張マクロ
#define popcount __builtin_popcount
#define popcountll __builtin_popcountll
// エイリアス
#define DANCE_ long
#define ROBOT_ unsigned
#define HUMAN_ signed
#define CHOKUDAI_ const
using ll = DANCE_ HUMAN_ DANCE_;
using ull = DANCE_ ROBOT_ DANCE_;
using cll = DANCE_ DANCE_ CHOKUDAI_;
using ld = long double;
using namespace std;
// モジュール
ll solve(vector<vector<vector<ll>>> &dp, vector<ll> &a, ll l, ll r, ll i) {
if (l == r)
return 0;
if (dp[l][r][i] != -LINF)
return dp[l][r][i];
const ll sgn = !i ? 1 : -1;
ll c1 = solve(dp, a, l + 1, r, !i) + a[l] * sgn;
ll c2 = solve(dp, a, l, r - 1, !i) + a[r - 1] * sgn;
return !i ? max(c1, c2) : min(c1, c2);
}
// 処理内容
int main() {
ll n;
cin >> n;
vector<ll> a(n);
REP(i, n) cin >> a[i];
// dp[l][r][i] := [l,r)が残っててiの番が来た時に達成できるX-Yの最大値
vector<vector<vector<ll>>> dp(
n, vector<vector<ll>>(n + 1, vector<ll>(2, -LINF)));
cout << solve(dp, a, 0, n, 0) << endl;
} | // May this submission get accepted!
#pragma GCC optimize("O3")
#pragma GCC target("tune=native")
#pragma GCC target("avx")
#include <bits/stdc++.h>
// 汎用マクロ
#define ALL_OF(x) (x).begin(), (x).end()
#define REP(i, n) for (long long i = 0, i##_len = (n); i < i##_len; i++)
#define RANGE(i, is, ie) \
for (long long i = (is), i##_end = (ie); i <= i##_end; i++)
#define DSRNG(i, is, ie) \
for (long long i = (is), i##_end = (ie); i >= i##_end; i--)
#define UNIQUE(v) \
do { \
sort((v).begin(), (v).end()); \
(v).erase(unique((v).begin(), (v).end()), (v).end()); \
} while (false)
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
#define INF 0x7FFFFFFF
#define LINF 0x7FFFFFFFFFFFFFFFLL
#define Yes(q) ((q) ? "Yes" : "No")
#define YES(q) ((q) ? "YES" : "NO")
#define Possible(q) ((q) ? "Possible" : "Impossible")
#define POSSIBLE(q) ((q) ? "POSSIBLE" : "IMPOSSIBLE")
#define DUMP(q) \
cerr << "[DEBUG] " #q ": " << (q) << " at " __FILE__ ":" << __LINE__ << endl
#define DUMPALL(q) \
do { \
cerr << "[DEBUG] " #q ": ["; \
REP(i, (q).size()) { cerr << (q)[i] << (i == i_len - 1 ? "" : ", "); } \
cerr << "] at " __FILE__ ":" << __LINE__ << endl; \
} while (false)
template <class T> T gcd(const T &a, const T &b) {
return a < b ? gcd(b, a) : b ? gcd(b, a % b) : a;
}
template <class T> T lcm(const T &a, const T &b) { return a / gcd(a, b) * b; }
// gcc拡張マクロ
#define popcount __builtin_popcount
#define popcountll __builtin_popcountll
// エイリアス
#define DANCE_ long
#define ROBOT_ unsigned
#define HUMAN_ signed
#define CHOKUDAI_ const
using ll = DANCE_ HUMAN_ DANCE_;
using ull = DANCE_ ROBOT_ DANCE_;
using cll = DANCE_ DANCE_ CHOKUDAI_;
using ld = long double;
using namespace std;
// モジュール
ll solve(vector<vector<vector<ll>>> &dp, vector<ll> &a, ll l, ll r, ll i) {
if (l == r)
return 0;
if (dp[l][r][i] != -LINF)
return dp[l][r][i];
const ll sgn = !i ? 1 : -1;
ll c1 = solve(dp, a, l + 1, r, !i) + a[l] * sgn;
ll c2 = solve(dp, a, l, r - 1, !i) + a[r - 1] * sgn;
return dp[l][r][i] = !i ? max(c1, c2) : min(c1, c2);
}
// 処理内容
int main() {
ll n;
cin >> n;
vector<ll> a(n);
REP(i, n) cin >> a[i];
// dp[l][r][i] := [l,r)が残っててiの番が来た時に達成できるX-Yの最大値
vector<vector<vector<ll>>> dp(
n, vector<vector<ll>>(n + 1, vector<ll>(2, -LINF)));
cout << solve(dp, a, 0, n, 0) << endl;
} | replace | 77 | 78 | 77 | 78 | TLE | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
#define MAX_N 3000
typedef long long int ll;
int n;
ll a[MAX_N];
ll dp[MAX_N + 1][MAX_N + 1];
int main() {
std::cin >> n;
for (int i = 0; i < n; ++i) {
std::cin >> a[i];
}
for (int i = 0; i <= n; ++i) {
dp[i][i] = 0;
}
for (int len = 1; len <= n; ++len) {
for (int i = 0; i < n; ++i) {
int j = i + len;
if ((n - len) % 2 == 0) {
dp[i][j] = std::max(dp[i + 1][j] + a[i], dp[i][j - 1] + a[j - 1]);
} else {
dp[i][j] = std::min(dp[i + 1][j] - a[i], dp[i][j - 1] - a[j - 1]);
}
}
}
std::cout << dp[0][n] << std::endl;
}
| #include <bits/stdc++.h>
#define MAX_N 3000
typedef long long int ll;
int n;
ll a[MAX_N];
ll dp[MAX_N + 1][MAX_N + 1];
int main() {
std::cin >> n;
for (int i = 0; i < n; ++i) {
std::cin >> a[i];
}
for (int i = 0; i <= n; ++i) {
dp[i][i] = 0;
}
for (int len = 1; len <= n; ++len) {
for (int i = 0; i + len <= n; ++i) {
int j = i + len;
if ((n - len) % 2 == 0) {
dp[i][j] = std::max(dp[i + 1][j] + a[i], dp[i][j - 1] + a[j - 1]);
} else {
dp[i][j] = std::min(dp[i + 1][j] - a[i], dp[i][j - 1] - a[j - 1]);
}
}
}
std::cout << dp[0][n] << std::endl;
}
| replace | 20 | 21 | 20 | 21 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define f(i, n) for (ll i = 0; i < n; i++)
#define fz for (ll j = i + 1; j < n; j++)
#define f2(i, n) for (ll i = 1; i <= n; i++)
#define endl "\n"
#define vll vector<ll>
#define sz(a) (ll) a.size()
#define mod 1000000007
#define pb push_back
#define inf 1e13 + 5
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll fexp(ll a, ll b) {
ll ans = 1;
while (b) {
if (b & 1)
ans = ans * a % mod;
b /= 2;
a = a * a % mod;
}
return ans;
}
ll inverse(ll a, ll p) { return fexp(a, p - 2); }
ll dp[303][303];
ll game(ll *arr, ll l, ll r) {
if (l > r)
return 0;
if (l == r)
return arr[l];
if (dp[l][r] != -(ll)1e18)
return dp[l][r];
return (dp[l][r] =
max(arr[l] - game(arr, l + 1, r), arr[r] - game(arr, l, r - 1)));
}
void solve() {
ll n;
cin >> n;
ll a[n];
f(i, n) cin >> a[i];
f(i, n) { f(j, n) dp[i][j] = -(ll)1e18; }
ll ans = game(a, 0, n - 1);
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll t;
t = 1;
// cin>>t;
while (t--) {
solve();
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define f(i, n) for (ll i = 0; i < n; i++)
#define fz for (ll j = i + 1; j < n; j++)
#define f2(i, n) for (ll i = 1; i <= n; i++)
#define endl "\n"
#define vll vector<ll>
#define sz(a) (ll) a.size()
#define mod 1000000007
#define pb push_back
#define inf 1e13 + 5
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll fexp(ll a, ll b) {
ll ans = 1;
while (b) {
if (b & 1)
ans = ans * a % mod;
b /= 2;
a = a * a % mod;
}
return ans;
}
ll inverse(ll a, ll p) { return fexp(a, p - 2); }
ll dp[3003][3003];
ll game(ll *arr, ll l, ll r) {
if (l > r)
return 0;
if (l == r)
return arr[l];
if (dp[l][r] != -(ll)1e18)
return dp[l][r];
return (dp[l][r] =
max(arr[l] - game(arr, l + 1, r), arr[r] - game(arr, l, r - 1)));
}
void solve() {
ll n;
cin >> n;
ll a[n];
f(i, n) cin >> a[i];
f(i, n) { f(j, n) dp[i][j] = -(ll)1e18; }
ll ans = game(a, 0, n - 1);
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll t;
t = 1;
// cin>>t;
while (t--) {
solve();
}
return 0;
} | replace | 31 | 32 | 31 | 32 | 0 | |
p03171 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
#define ALL(g) (g).begin(), (g).end()
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, x, n) for (int i = x; i >= n; i--)
#define rrep(i, n) RREP(i, n, 0)
#define pb push_back
#pragma GCC optimize("-O3")
using namespace std;
// #define DEBUG_IS_VALID
#ifdef DEBUG_IS_VALID
#define DEB 1
#else
#define DEB 0
#endif
#define DUMPOUT cout
#define dump(...) \
if (DEB) \
DUMPOUT << " " << #__VA_ARGS__ << " :[" << __LINE__ << ":" \
<< __FUNCTION__ << "]" << endl \
<< " "; \
if (DEB) \
dump_func(__VA_ARGS__)
template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> p) {
cout << "(" << p.first << ", " << p.second << ")";
return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
os << "{";
for (int i = 0; i < vec.size(); i++)
os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
os << "}";
return os;
}
template <typename T> ostream &operator<<(ostream &os, set<T> &st) {
cout << "{";
for (auto itr = st.begin(); itr != st.end(); itr++)
cout << *itr << (next(itr) != st.end() ? ", " : "");
cout << "}";
return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, map<T1, T2> mp) {
cout << "{";
for (auto itr = mp.begin(); itr != mp.end(); itr++)
cout << "(" << (itr->first) << ", " << (itr->second) << ")"
<< (next(itr) != mp.end() ? "," : "");
cout << "}";
return os;
}
void dump_func() { DUMPOUT << endl; }
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&...tail) {
DUMPOUT << head;
if (sizeof...(Tail) == 0) {
DUMPOUT << " ";
} else {
DUMPOUT << ", ";
}
dump_func(std::move(tail)...);
}
template <class T> inline bool chmax(T &a, T const &b) {
if (a >= b)
return false;
a = b;
return true;
}
template <class T> inline bool chmin(T &a, T const &b) {
if (a <= b)
return false;
a = b;
return true;
}
void _main();
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
_main();
return 0;
}
using ll = long long;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
const int mod = 1e9 + 7, INF = 1 << 29;
const double EPS = 1e-12, PI = 3.1415926535897932384626;
const ll lmod = 1e9 + 7, LINF = 1LL << 59;
template <int MOD> struct ModInt {
static const int Mod = MOD;
unsigned x;
ModInt() : x(0) {}
ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
int get() const { return (int)x; }
ModInt &operator+=(ModInt that) {
if ((x += that.x) >= MOD)
x -= MOD;
return *this;
}
ModInt &operator-=(ModInt that) {
if ((x += MOD - that.x) >= MOD)
x -= MOD;
return *this;
}
ModInt &operator*=(ModInt that) {
x = (unsigned long long)x * that.x % MOD;
return *this;
}
ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
ModInt inverse() const {
long long a = x, b = MOD, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b;
std::swap(a, b);
u -= t * v;
std::swap(u, v);
}
return ModInt(u);
}
bool operator==(ModInt that) const { return x == that.x; }
bool operator!=(ModInt that) const { return x != that.x; }
ModInt operator-() const {
ModInt t;
t.x = x == 0 ? 0 : Mod - x;
return t;
}
};
template <int MOD> ostream &operator<<(ostream &st, const ModInt<MOD> a) {
st << a.get();
return st;
};
template <int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {
ModInt<MOD> r = 1;
while (k) {
if (k & 1)
r *= a;
a *= a;
k >>= 1;
}
return r;
}
template <typename T, int FAC_MAX> struct Comb {
vector<T> fac, ifac;
Comb() {
fac.resize(FAC_MAX, 1);
ifac.resize(FAC_MAX, 1);
for (int i = 1; i < FAC_MAX; i++)
fac[i] = fac[i - 1] * i;
ifac[FAC_MAX - 1] = T(1) / fac[FAC_MAX - 1];
for (int i = FAC_MAX - 2; i >= 1; i--)
ifac[i] = ifac[i + 1] * T(i + 1);
}
T aPb(int a, int b) {
if (b < 0 || a < b)
return T(0);
return fac[a] * ifac[a - b];
}
T aCb(int a, int b) {
if (b < 0 || a < b)
return T(0);
return fac[a] * ifac[a - b] * ifac[b];
}
T nHk(int n, int k) {
if (n == 0 && k == 0)
return T(1);
if (n <= 0 || k < 0)
return 0;
return aCb(n + k - 1, k);
}
}; // nHk = (n+k-1)Ck : n is separator
using mint = ModInt<int(1e9 + 7)>;
using vm = vector<mint>;
using vd = vector<double>;
void _main() {
int N;
cin >> N;
vl a(N);
rep(i, N) cin >> a[i];
vvl dp(N + 1, vl(N + 1, LINF));
function<ll(int, int)> solve = [&](int l, int r) {
if (dp[l][r] != LINF)
return dp[l][r];
if (l == r)
return dp[l][r] = 0;
int turn = N - r + l;
ll res;
if (turn % 2 == 0) {
res = -LINF;
chmax(res, solve(l + 1, r) + a[l]);
chmax(res, solve(l, r - 1) + a[r - 1]);
} else {
res = LINF;
chmin(res, solve(l + 1, r) - a[l]);
chmin(res, solve(l, r - 1) - a[r - 1]);
}
return res;
};
cout << solve(0, N) << endl;
dump(dp);
} | #include "bits/stdc++.h"
#define ALL(g) (g).begin(), (g).end()
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, x, n) for (int i = x; i >= n; i--)
#define rrep(i, n) RREP(i, n, 0)
#define pb push_back
#pragma GCC optimize("-O3")
using namespace std;
// #define DEBUG_IS_VALID
#ifdef DEBUG_IS_VALID
#define DEB 1
#else
#define DEB 0
#endif
#define DUMPOUT cout
#define dump(...) \
if (DEB) \
DUMPOUT << " " << #__VA_ARGS__ << " :[" << __LINE__ << ":" \
<< __FUNCTION__ << "]" << endl \
<< " "; \
if (DEB) \
dump_func(__VA_ARGS__)
template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> p) {
cout << "(" << p.first << ", " << p.second << ")";
return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
os << "{";
for (int i = 0; i < vec.size(); i++)
os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
os << "}";
return os;
}
template <typename T> ostream &operator<<(ostream &os, set<T> &st) {
cout << "{";
for (auto itr = st.begin(); itr != st.end(); itr++)
cout << *itr << (next(itr) != st.end() ? ", " : "");
cout << "}";
return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, map<T1, T2> mp) {
cout << "{";
for (auto itr = mp.begin(); itr != mp.end(); itr++)
cout << "(" << (itr->first) << ", " << (itr->second) << ")"
<< (next(itr) != mp.end() ? "," : "");
cout << "}";
return os;
}
void dump_func() { DUMPOUT << endl; }
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&...tail) {
DUMPOUT << head;
if (sizeof...(Tail) == 0) {
DUMPOUT << " ";
} else {
DUMPOUT << ", ";
}
dump_func(std::move(tail)...);
}
template <class T> inline bool chmax(T &a, T const &b) {
if (a >= b)
return false;
a = b;
return true;
}
template <class T> inline bool chmin(T &a, T const &b) {
if (a <= b)
return false;
a = b;
return true;
}
void _main();
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
_main();
return 0;
}
using ll = long long;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
const int mod = 1e9 + 7, INF = 1 << 29;
const double EPS = 1e-12, PI = 3.1415926535897932384626;
const ll lmod = 1e9 + 7, LINF = 1LL << 59;
template <int MOD> struct ModInt {
static const int Mod = MOD;
unsigned x;
ModInt() : x(0) {}
ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
int get() const { return (int)x; }
ModInt &operator+=(ModInt that) {
if ((x += that.x) >= MOD)
x -= MOD;
return *this;
}
ModInt &operator-=(ModInt that) {
if ((x += MOD - that.x) >= MOD)
x -= MOD;
return *this;
}
ModInt &operator*=(ModInt that) {
x = (unsigned long long)x * that.x % MOD;
return *this;
}
ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
ModInt inverse() const {
long long a = x, b = MOD, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b;
std::swap(a, b);
u -= t * v;
std::swap(u, v);
}
return ModInt(u);
}
bool operator==(ModInt that) const { return x == that.x; }
bool operator!=(ModInt that) const { return x != that.x; }
ModInt operator-() const {
ModInt t;
t.x = x == 0 ? 0 : Mod - x;
return t;
}
};
template <int MOD> ostream &operator<<(ostream &st, const ModInt<MOD> a) {
st << a.get();
return st;
};
template <int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {
ModInt<MOD> r = 1;
while (k) {
if (k & 1)
r *= a;
a *= a;
k >>= 1;
}
return r;
}
template <typename T, int FAC_MAX> struct Comb {
vector<T> fac, ifac;
Comb() {
fac.resize(FAC_MAX, 1);
ifac.resize(FAC_MAX, 1);
for (int i = 1; i < FAC_MAX; i++)
fac[i] = fac[i - 1] * i;
ifac[FAC_MAX - 1] = T(1) / fac[FAC_MAX - 1];
for (int i = FAC_MAX - 2; i >= 1; i--)
ifac[i] = ifac[i + 1] * T(i + 1);
}
T aPb(int a, int b) {
if (b < 0 || a < b)
return T(0);
return fac[a] * ifac[a - b];
}
T aCb(int a, int b) {
if (b < 0 || a < b)
return T(0);
return fac[a] * ifac[a - b] * ifac[b];
}
T nHk(int n, int k) {
if (n == 0 && k == 0)
return T(1);
if (n <= 0 || k < 0)
return 0;
return aCb(n + k - 1, k);
}
}; // nHk = (n+k-1)Ck : n is separator
using mint = ModInt<int(1e9 + 7)>;
using vm = vector<mint>;
using vd = vector<double>;
void _main() {
int N;
cin >> N;
vl a(N);
rep(i, N) cin >> a[i];
vvl dp(N + 1, vl(N + 1, LINF));
function<ll(int, int)> solve = [&](int l, int r) {
if (dp[l][r] != LINF)
return dp[l][r];
if (l == r)
return dp[l][r] = 0;
int turn = N - r + l;
ll res;
if (turn % 2 == 0) {
res = -LINF;
chmax(res, solve(l + 1, r) + a[l]);
chmax(res, solve(l, r - 1) + a[r - 1]);
} else {
res = LINF;
chmin(res, solve(l + 1, r) - a[l]);
chmin(res, solve(l, r - 1) - a[r - 1]);
}
return dp[l][r] = res;
};
cout << solve(0, N) << endl;
dump(dp);
} | replace | 215 | 216 | 215 | 216 | TLE | |
p03171 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <list>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (i = 0; i < (n); i++)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define MOD 1000000007
#define PI 3.14159265358979323846
#define INF 1 << 30
using namespace std;
typedef long long ll;
typedef pair<int, int> pp;
ll Search(vector<vector<ll>> &dp, vector<vector<char>> &ok, const vector<ll> &d,
int l, int r, char p = 0) {
if (l > r)
return 0;
if (ok[l][r])
dp[l][r];
ll a, b;
a = Search(dp, ok, d, l + 1, r, !p);
b = Search(dp, ok, d, l, r - 1, !p);
ok[l][r] = 1;
return dp[l][r] = p ? min(a - d[l], b - d[r]) : max(a + d[l], b + d[r]);
}
int main(void) {
int num, i;
ll ans;
cin >> num;
vector<ll> d(num);
vector<vector<char>> ok(num, vector<char>(num, 0));
vector<vector<ll>> dp(num, vector<ll>(num, 0));
rep(i, num) cin >> d[i];
ans = Search(dp, ok, d, 0, num - 1);
cout << ans << "\n";
return 0;
} | #include <algorithm>
#include <iostream>
#include <list>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (i = 0; i < (n); i++)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define MOD 1000000007
#define PI 3.14159265358979323846
#define INF 1 << 30
using namespace std;
typedef long long ll;
typedef pair<int, int> pp;
ll Search(vector<vector<ll>> &dp, vector<vector<char>> &ok, const vector<ll> &d,
int l, int r, char p = 0) {
if (l > r)
return 0;
if (ok[l][r])
return dp[l][r];
ll a, b;
a = Search(dp, ok, d, l + 1, r, !p);
b = Search(dp, ok, d, l, r - 1, !p);
ok[l][r] = 1;
return dp[l][r] = p ? min(a - d[l], b - d[r]) : max(a + d[l], b + d[r]);
}
int main(void) {
int num, i;
ll ans;
cin >> num;
vector<ll> d(num);
vector<vector<char>> ok(num, vector<char>(num, 0));
vector<vector<ll>> dp(num, vector<ll>(num, 0));
rep(i, num) cin >> d[i];
ans = Search(dp, ok, d, 0, num - 1);
cout << ans << "\n";
return 0;
} | replace | 22 | 23 | 22 | 23 | TLE | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll maxn = 1001;
ll n, a[maxn], memo[3001][3001][2];
ll go(ll i, ll j, ll par) {
if (i > j) {
return 0;
}
ll ans = 0;
if (memo[i][j][par] != -1)
return memo[i][j][par];
if (par == 0) {
ans += max(a[i] + go(i + 1, j, !par), a[j] + go(i, j - 1, !par));
} else {
ans += min(go(i + 1, j, !par) - a[i], go(i, j - 1, !par) - a[j]);
}
return memo[i][j][par] = ans;
}
int main() {
cin >> n;
for (ll i = 0; i < n; i++) {
cin >> a[i];
}
memset(memo, -1, sizeof(memo));
cout << go(0, n - 1, 0);
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll maxn = 3001;
ll n, a[maxn], memo[maxn][maxn][2];
ll go(ll i, ll j, ll par) {
if (i > j) {
return 0;
}
ll ans = 0;
if (memo[i][j][par] != -1)
return memo[i][j][par];
if (par == 0) {
ans += max(a[i] + go(i + 1, j, !par), a[j] + go(i, j - 1, !par));
} else {
ans += min(go(i + 1, j, !par) - a[i], go(i, j - 1, !par) - a[j]);
}
return memo[i][j][par] = ans;
}
int main() {
cin >> n;
for (ll i = 0; i < n; i++) {
cin >> a[i];
}
memset(memo, -1, sizeof(memo));
cout << go(0, n - 1, 0);
} | replace | 3 | 5 | 3 | 5 | -11 | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FASTIO \
cin.tie(0); \
cout.tie(0); \
ios_base::sync_with_stdio(false);
#define loop(i, a, b) for (int i = a; i < b; i++)
#define INF 10000000000099ll
#define mod 1000000007
#define prDouble(x) cout << fixed << setprecision(10) << x
ll dp[3001][3001];
ll solve(vector<int> &arr, int i, int j) {
if (i > j)
return 0;
if (dp[i][j] != -1)
return dp[i][j];
ll opt1 = arr[i] + min(solve(arr, i + 1, j - 1), solve(arr, i + 2, j));
ll opt2 = arr[j] + min(solve(arr, i, j - 2), solve(arr, i + 1, j - 1));
return max(opt1, opt2);
}
int main() {
FASTIO;
int n;
ll sum = 0;
cin >> n;
memset(dp, -1, sizeof dp);
vector<int> arr(n + 1);
loop(i, 1, n + 1) {
cin >> arr[i];
sum += arr[i];
}
ll x = solve(arr, 1, n);
cout << 2 * x - sum;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FASTIO \
cin.tie(0); \
cout.tie(0); \
ios_base::sync_with_stdio(false);
#define loop(i, a, b) for (int i = a; i < b; i++)
#define INF 10000000000099ll
#define mod 1000000007
#define prDouble(x) cout << fixed << setprecision(10) << x
ll dp[3001][3001];
ll solve(vector<int> &arr, int i, int j) {
if (i > j)
return 0;
if (dp[i][j] != -1)
return dp[i][j];
ll opt1 = arr[i] + min(solve(arr, i + 1, j - 1), solve(arr, i + 2, j));
ll opt2 = arr[j] + min(solve(arr, i, j - 2), solve(arr, i + 1, j - 1));
return dp[i][j] = max(opt1, opt2);
}
int main() {
FASTIO;
int n;
ll sum = 0;
cin >> n;
memset(dp, -1, sizeof dp);
vector<int> arr(n + 1);
loop(i, 1, n + 1) {
cin >> arr[i];
sum += arr[i];
}
ll x = solve(arr, 1, n);
cout << 2 * x - sum;
return 0;
}
| replace | 20 | 21 | 20 | 21 | TLE | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define se second
#define fr first
#define int long long int
#define pb push_back
#define inf 1e18
#define all(v) v.begin(), v.end()
#define CHAL_BAAP_KO_MT_SIKHA \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define mod 1000000007
/*
Damn Fast:TheFuckinMastermind
while(How to solve the Question?){
Read the Question Again!!
}
do
Practise
#Motivation::::0/0
*/
const int N = 2e3;
int dp[N + 1][N + 1];
int n, a[N + 1];
int solve(int i, int j) {
if (i > n or j <= 0) {
return 0;
}
if (j < i)
return 0;
if (dp[i][j] != -1)
return dp[i][j];
int ans1 = a[i] + min(solve(i + 2, j), solve(i + 1, j - 1));
int ans2 = a[j] + min(solve(i + 1, j - 1), solve(i, j - 2));
return dp[i][j] = max(ans1, ans2);
}
signed main() {
CHAL_BAAP_KO_MT_SIKHA
cin >> n;
int sum = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum += a[i];
}
memset(dp, -1, sizeof(dp));
int x = solve(1, n);
int y = sum - x;
cout << x - y << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define se second
#define fr first
#define int long long int
#define pb push_back
#define inf 1e18
#define all(v) v.begin(), v.end()
#define CHAL_BAAP_KO_MT_SIKHA \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define mod 1000000007
/*
Damn Fast:TheFuckinMastermind
while(How to solve the Question?){
Read the Question Again!!
}
do
Practise
#Motivation::::0/0
*/
const int N = 4e3;
int dp[N + 1][N + 1];
int n, a[N + 1];
int solve(int i, int j) {
if (i > n or j <= 0) {
return 0;
}
if (j < i)
return 0;
if (dp[i][j] != -1)
return dp[i][j];
int ans1 = a[i] + min(solve(i + 2, j), solve(i + 1, j - 1));
int ans2 = a[j] + min(solve(i + 1, j - 1), solve(i, j - 2));
return dp[i][j] = max(ans1, ans2);
}
signed main() {
CHAL_BAAP_KO_MT_SIKHA
cin >> n;
int sum = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum += a[i];
}
memset(dp, -1, sizeof(dp));
int x = solve(1, n);
int y = sum - x;
cout << x - y << endl;
} | replace | 22 | 23 | 22 | 23 | 0 | |
p03171 | C++ | Runtime Error | #pragma region template 1.0
#pragma region def
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> ii;
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define REP1(i, n) for (ll i = 1; i <= (n); ++i)
#define OUT(x) cout << (x) << endl;
#define OUTA(a) \
REP(i, (a).size()) { cout << (a[i]) << (i == (a).size() - 1 ? "\n" : " "); }
#define ALL(a) (a).begin(), (a).end()
#define SORT(a) sort(ALL(a))
#define RSORT(a) \
SORT(a); \
reverse(ALL(a))
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
#pragma endregion enddef
int main() {
ll N;
cin >> N;
vi a(N);
REP(i, N) { cin >> a[i]; }
vector<vi> dp(N + 1, vi(N + 1));
REP1(i, N) {
REP1(j, i + 1) {
if ((N - j) & 1) {
dp[i][j] = min(dp[i - 1][j - 1] - a[i - 1], dp[i][j - 1] - a[i - j]);
} else {
dp[i][j] = max(dp[i - 1][j - 1] + a[i - 1], dp[i][j - 1] + a[i - j]);
}
}
}
ll ans = dp[N][N];
OUT(ans);
}
#pragma endregion template | #pragma region template 1.0
#pragma region def
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> ii;
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define REP1(i, n) for (ll i = 1; i <= (n); ++i)
#define OUT(x) cout << (x) << endl;
#define OUTA(a) \
REP(i, (a).size()) { cout << (a[i]) << (i == (a).size() - 1 ? "\n" : " "); }
#define ALL(a) (a).begin(), (a).end()
#define SORT(a) sort(ALL(a))
#define RSORT(a) \
SORT(a); \
reverse(ALL(a))
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
#pragma endregion enddef
int main() {
ll N;
cin >> N;
vi a(N);
REP(i, N) { cin >> a[i]; }
vector<vi> dp(N + 1, vi(N + 1));
REP1(i, N) {
REP1(j, i) {
if ((N - j) & 1) {
dp[i][j] = min(dp[i - 1][j - 1] - a[i - 1], dp[i][j - 1] - a[i - j]);
} else {
dp[i][j] = max(dp[i - 1][j - 1] + a[i - 1], dp[i][j - 1] + a[i - j]);
}
}
}
ll ans = dp[N][N];
OUT(ans);
}
#pragma endregion template | replace | 30 | 31 | 30 | 31 | -6 | malloc(): corrupted top size
|
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vl = vector<ll>;
using vvl = vector<vl>;
#define mp make_pair
#define pb push_back
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define rrep(i, n) for (int i = (n - 1); i >= 0; i--)
#define all(v) v.begin(), v.end()
#define sz(v) (int)((v).size())
#define MAX(v) *max_element(all(v))
#define MIN(v) *min_element(all(v))
#define Sort(v) sort(all(v))
#define Rev(v) reverse(all(v))
#define vec(type, name, ...) vector<type> name(__VA_ARGS__)
#define VEC(type, name, size) \
vector<type> name(size); \
in(name)
#define vv(type, name, h, ...) \
vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define VV(type, name, h, w) \
vector<vector<type>> name(h, vector<type>(w)); \
in(name)
void debug_out() { cout << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cout << H << " ";
debug_out(T...);
}
#ifdef _DEBUG
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
template <typename T>
std::ostream &operator<<(std::ostream &os, std::vector<T> vec) {
for (std::size_t i = 0; i < vec.size(); i++)
os << vec[i] << (i + 1 == vec.size() ? "" : " ");
return os;
}
inline void IN(void) { return; }
template <typename First, typename... Rest>
void IN(First &first, Rest &...rest) {
cin >> first;
IN(rest...);
return;
}
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;
}
const ll INF = 1LL << 60;
const ll mod = 1000000007;
const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
ll n;
ll a[3000];
bool flag[3000][3000];
ll dp[3000][3000]; // dp[i][j] = [i, j]が残ってるときから始めた最大スコア
ll dfs(ll l, ll r) {
if (flag[l][r])
return dp[l][r];
if (l == r) {
dp[l][r] = a[l];
flag[l][r] = true;
return a[l];
}
return dp[l][r] = max(a[l] - dfs(l + 1, r), a[r] - dfs(l, r - 1));
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n;
rep(i, n) cin >> a[i];
cout << dfs(0, n - 1) << endl;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vl = vector<ll>;
using vvl = vector<vl>;
#define mp make_pair
#define pb push_back
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define rrep(i, n) for (int i = (n - 1); i >= 0; i--)
#define all(v) v.begin(), v.end()
#define sz(v) (int)((v).size())
#define MAX(v) *max_element(all(v))
#define MIN(v) *min_element(all(v))
#define Sort(v) sort(all(v))
#define Rev(v) reverse(all(v))
#define vec(type, name, ...) vector<type> name(__VA_ARGS__)
#define VEC(type, name, size) \
vector<type> name(size); \
in(name)
#define vv(type, name, h, ...) \
vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define VV(type, name, h, w) \
vector<vector<type>> name(h, vector<type>(w)); \
in(name)
void debug_out() { cout << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cout << H << " ";
debug_out(T...);
}
#ifdef _DEBUG
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
template <typename T>
std::ostream &operator<<(std::ostream &os, std::vector<T> vec) {
for (std::size_t i = 0; i < vec.size(); i++)
os << vec[i] << (i + 1 == vec.size() ? "" : " ");
return os;
}
inline void IN(void) { return; }
template <typename First, typename... Rest>
void IN(First &first, Rest &...rest) {
cin >> first;
IN(rest...);
return;
}
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;
}
const ll INF = 1LL << 60;
const ll mod = 1000000007;
const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
ll n;
ll a[3000];
bool flag[3000][3000];
ll dp[3000][3000]; // dp[i][j] = [i, j]が残ってるときから始めた最大スコア
ll dfs(ll l, ll r) {
if (flag[l][r])
return dp[l][r];
flag[l][r] = true;
if (l == r)
return dp[l][r] = a[l];
return dp[l][r] = max(a[l] - dfs(l + 1, r), a[r] - dfs(l, r - 1));
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n;
rep(i, n) cin >> a[i];
cout << dfs(0, n - 1) << endl;
} | replace | 78 | 83 | 78 | 81 | TLE | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
ll stoneGame(vector<ll> &piles) {
// cout << piles[0] << " ";
ll n = piles.size();
vector<vector<ll>> dp(n, vector<ll>(n, 0));
for (int i = 0; i < n; i++)
dp[i][i] = piles[i];
for (int d = 1; d < n; d++) {
for (int i = 0; i < n - d; i++) {
dp[i][i + d] =
max(piles[i] - dp[i + 1][i + d], piles[i + d] - dp[i][i + d - 1]);
}
}
return dp[0][n - 1];
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int n;
cin >> n;
std::vector<ll> v(n);
for (int i = 0; i < n; ++i) {
cin >> v[i];
}
cout << stoneGame(v) << "\n";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
ll stoneGame(vector<ll> &piles) {
// cout << piles[0] << " ";
ll n = piles.size();
vector<vector<ll>> dp(n, vector<ll>(n, 0));
for (int i = 0; i < n; i++)
dp[i][i] = piles[i];
for (int d = 1; d < n; d++) {
for (int i = 0; i < n - d; i++) {
dp[i][i + d] =
max(piles[i] - dp[i + 1][i + d], piles[i + d] - dp[i][i + d - 1]);
}
}
return dp[0][n - 1];
}
int main() {
int n;
cin >> n;
std::vector<ll> v(n);
for (int i = 0; i < n; ++i) {
cin >> v[i];
}
cout << stoneGame(v) << "\n";
return 0;
}
| delete | 25 | 29 | 25 | 25 | -6 | terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
const int maxn = 3e3 + 5, mod = 1e9 + 7;
int n, c[maxn][maxn], a[maxn];
long long f[maxn][maxn];
long long dp(int l, int r) {
if (l > r)
return 0;
long long &res = f[l][r];
if (c[l][r])
return res;
int turn = (l + (n - r + 1)) % 2;
if (turn) {
res = 1e13;
res = min(dp(l + 1, r) - a[l], res);
res = min(dp(l, r - 1) - a[r], res);
} else {
res = -1e13;
res = max(dp(l + 1, r) + a[l], res);
res = max(dp(l, r - 1) + a[r], res);
}
return res;
}
int main() {
#ifdef NTMA
freopen("abc.inp", "r", stdin);
#endif
// memset(f, 255, sizeof(f));
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> a[i];
cout << dp(1, n);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const int maxn = 3e3 + 5, mod = 1e9 + 7;
int n, c[maxn][maxn], a[maxn];
long long f[maxn][maxn];
long long dp(int l, int r) {
if (l > r)
return 0;
long long &res = f[l][r];
if (c[l][r])
return res;
c[l][r] = true;
int turn = (l + (n - r + 1)) % 2;
if (turn) {
res = 1e13;
res = min(dp(l + 1, r) - a[l], res);
res = min(dp(l, r - 1) - a[r], res);
} else {
res = -1e13;
res = max(dp(l + 1, r) + a[l], res);
res = max(dp(l, r - 1) + a[r], res);
}
return res;
}
int main() {
#ifdef NTMA
freopen("abc.inp", "r", stdin);
#endif
// memset(f, 255, sizeof(f));
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> a[i];
cout << dp(1, n);
return 0;
} | insert | 13 | 13 | 13 | 14 | TLE | |
p03171 | C++ | Runtime Error | //
// Created by vishal on 7/7/20.
//
//
// Created by vishal on 6/14/20.
//
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w \
int x; \
cin >> x; \
while (x--)
#define F10 \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
pbds;
const int mex = 3000;
int arr[mex][mex];
int32_t main() {
int n;
cin >> n;
vi v(n + 2);
v[0] = 0;
for (int i = 1; i <= n; ++i) {
cin >> arr[i][i];
v[i] = v[i - 1] + arr[i][i];
}
for (int dist = 1; dist < n; ++dist) {
for (int i = 1; i <= n - dist; ++i) {
arr[i][i + dist] =
max(arr[i][i] + (v[i + dist] - v[i]) - arr[i + 1][i + dist],
arr[i + dist][i + dist] + (v[i + dist - 1] - v[i - 1]) -
arr[i][i + dist - 1]);
}
}
cout << arr[1][n] * 2 - v[n];
} | //
// Created by vishal on 7/7/20.
//
//
// Created by vishal on 6/14/20.
//
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w \
int x; \
cin >> x; \
while (x--)
#define F10 \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
pbds;
const int mex = 3005;
int arr[mex][mex];
int32_t main() {
int n;
cin >> n;
vi v(n + 2);
v[0] = 0;
for (int i = 1; i <= n; ++i) {
cin >> arr[i][i];
v[i] = v[i - 1] + arr[i][i];
}
for (int dist = 1; dist < n; ++dist) {
for (int i = 1; i <= n - dist; ++i) {
arr[i][i + dist] =
max(arr[i][i] + (v[i + dist] - v[i]) - arr[i + 1][i + dist],
arr[i + dist][i + dist] + (v[i + dist - 1] - v[i - 1]) -
arr[i][i + dist - 1]);
}
}
cout << arr[1][n] * 2 - v[n];
}
| replace | 41 | 42 | 41 | 42 | 0 | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
// #define int ll
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<short> vs;
typedef vector<vs> vvs;
typedef vector<vvs> vvvs;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef vector<ld> vld;
typedef vector<vld> vvld;
typedef vector<vvld> vvvld;
typedef vector<string> vst;
typedef vector<vst> vvst;
typedef pair<ld, ld> pld;
typedef complex<double> base;
#define inmin(a, b) a = min(a, (b))
#define inmax(a, b) a = max(a, (b))
#define mp(a, b) make_pair(a, b)
#define modsum(a, b) ((a) + (b) >= M ? (a) + (b)-M : (a) + (b))
#define modsubtract(a, b) ((a) - (b) < 0 ? (a) - (b) + M : (a) - (b))
#define modmultiply(a, b) (((a) * (b)) % M)
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
#define sqr(x) ((x) * (x))
#define fori(i, n) for (int i = 0; i < int(n); ++i)
#define cint const int &
#define SZ(a) ((int)((a).size()))
#define watch(x) cout << (#x) << " = " << (x) << endl;
const double PI = 2 * acos(0.0);
const string DIGITS = "0123456789";
const string ALPH = "abcdefghijklmnopqrstuvwxyz";
inline void transpose(vector<string> &a) {
int n = a.size();
if (n == 0)
return;
int m = a[0].size();
vector<string> b(m, string(n, ' '));
fori(i, n) fori(j, m) b[j][i] = a[i][j];
a = b;
}
template <class T> inline ostream &operator<<(ostream &out, vector<T> &a) {
out << "{";
fori(i, SZ(a)) out << a[i] << vector<string>{", ", "}"}[i + 1 == SZ(a)];
return out;
}
void smain();
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifdef MAX_HOME
freopen("input.txt", "r", stdin);
clock_t start = clock();
#define cerr cout
#endif
cout << setprecision(12) << fixed;
smain();
#ifdef MAX_HOME
cout << "\n\n\n\n";
cout << "TOTAL EXECUTION TIME: " << float(clock() - start) / CLOCKS_PER_SEC
<< endl;
#endif
}
const int N = 3009;
ll dp[N][N];
char did[N][N];
ll a[N];
ll get_dp(int l, int r) {
if (l > r)
return 0;
if (did[l][r])
return dp[l][r];
did[l][r] = 0;
ll &ret = dp[l][r];
return ret = max(a[l] - get_dp(l + 1, r), a[r] - get_dp(l, r - 1));
}
void smain() {
int n;
cin >> n;
fori(i, n) cin >> a[i];
cout << get_dp(0, n - 1);
} | #include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
// #define int ll
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<short> vs;
typedef vector<vs> vvs;
typedef vector<vvs> vvvs;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef vector<ld> vld;
typedef vector<vld> vvld;
typedef vector<vvld> vvvld;
typedef vector<string> vst;
typedef vector<vst> vvst;
typedef pair<ld, ld> pld;
typedef complex<double> base;
#define inmin(a, b) a = min(a, (b))
#define inmax(a, b) a = max(a, (b))
#define mp(a, b) make_pair(a, b)
#define modsum(a, b) ((a) + (b) >= M ? (a) + (b)-M : (a) + (b))
#define modsubtract(a, b) ((a) - (b) < 0 ? (a) - (b) + M : (a) - (b))
#define modmultiply(a, b) (((a) * (b)) % M)
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
#define sqr(x) ((x) * (x))
#define fori(i, n) for (int i = 0; i < int(n); ++i)
#define cint const int &
#define SZ(a) ((int)((a).size()))
#define watch(x) cout << (#x) << " = " << (x) << endl;
const double PI = 2 * acos(0.0);
const string DIGITS = "0123456789";
const string ALPH = "abcdefghijklmnopqrstuvwxyz";
inline void transpose(vector<string> &a) {
int n = a.size();
if (n == 0)
return;
int m = a[0].size();
vector<string> b(m, string(n, ' '));
fori(i, n) fori(j, m) b[j][i] = a[i][j];
a = b;
}
template <class T> inline ostream &operator<<(ostream &out, vector<T> &a) {
out << "{";
fori(i, SZ(a)) out << a[i] << vector<string>{", ", "}"}[i + 1 == SZ(a)];
return out;
}
void smain();
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifdef MAX_HOME
freopen("input.txt", "r", stdin);
clock_t start = clock();
#define cerr cout
#endif
cout << setprecision(12) << fixed;
smain();
#ifdef MAX_HOME
cout << "\n\n\n\n";
cout << "TOTAL EXECUTION TIME: " << float(clock() - start) / CLOCKS_PER_SEC
<< endl;
#endif
}
const int N = 3009;
ll dp[N][N];
char did[N][N];
ll a[N];
ll get_dp(int l, int r) {
if (l > r)
return 0;
if (did[l][r])
return dp[l][r];
did[l][r] = 1;
ll &ret = dp[l][r];
return ret = max(a[l] - get_dp(l + 1, r), a[r] - get_dp(l, r - 1));
}
void smain() {
int n;
cin >> n;
fori(i, n) cin >> a[i];
cout << get_dp(0, n - 1);
} | replace | 95 | 96 | 95 | 96 | TLE | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
class Debug {
private:
static int NAME_LENGTH, MAX_WIDTH;
static bool IS_MATRIX;
static long long INF;
public:
static void set_NAME_LENGTH(int l) { NAME_LENGTH = l; }
static int get_NAME_LENGTH() { return NAME_LENGTH; }
static void set_MAX_WIDTH(int w) { MAX_WIDTH = w; }
static int get_MAX_WIDTH() { return MAX_WIDTH; }
static void set_IS_MATRIX(bool f) { IS_MATRIX = f; }
static bool get_IS_MATRIX() { return IS_MATRIX; }
static void set_INF(long long i) { INF = i; }
static long long get_INF() { return INF; }
};
int Debug::NAME_LENGTH = 0;
int Debug::MAX_WIDTH = 0;
bool Debug::IS_MATRIX = false;
long long Debug::INF = LLONG_MAX;
// Debug for vector
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << "[";
bool flg = true;
for (auto e : v) {
os << (flg ? "" : ", ");
if (Debug::get_IS_MATRIX()) {
os << right << setw(Debug::get_MAX_WIDTH());
}
if (e == Debug::get_INF())
os << "INF";
else
os << e;
flg = false;
}
os << "]";
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<vector<T>> &v) {
Debug::set_IS_MATRIX(true);
int width = 0;
for (unsigned int i = 0; i < v.size(); ++i) {
for (unsigned int j = 0; j < v[i].size(); ++j) {
if (v[i][j] == Debug::get_INF())
width = max(width, (int)("INF"s.size()));
else
width = max(width, (int)to_string(v[i][j]).size());
}
}
Debug::set_MAX_WIDTH(width);
string space = "";
for (int i = 0; i < Debug::get_NAME_LENGTH() + 7; ++i)
space += " ";
os << "[";
bool flg = true;
for (auto e : v) {
os << (flg ? "" : "\n" + space);
os << e;
flg = false;
}
os << "]";
Debug::set_IS_MATRIX(false);
return os;
}
// Debug for pair
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << "(" << p.first << ", " << p.second << ")";
return os;
}
// Debug for set
template <typename T> ostream &operator<<(ostream &os, const set<T> &s) {
os << "{";
bool flg = true;
for (auto e : s) {
os << (flg ? "" : ", ");
os << e;
flg = false;
}
os << "}";
return os;
}
// Debug for map
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &mp) {
os << "{";
bool flg = true;
for (auto e : mp) {
os << (flg ? "" : ", ");
os << e.first << ": " << e.second;
flg = false;
}
os << "}";
return os;
}
// Debug for queue
template <typename T> ostream &operator<<(ostream &os, const queue<T> &que) {
queue<T> que_cp = que;
os << "[";
bool flg = true;
while (!que_cp.empty()) {
os << (flg ? "" : ", ");
os << que_cp.front();
que_cp.pop();
flg = false;
}
os << "]";
return os;
}
// Debug for deque
template <typename T> ostream &operator<<(ostream &os, const deque<T> &que) {
os << "[";
bool flg = true;
for (auto e : que) {
os << (flg ? "" : ", ");
os << e;
flg = false;
}
os << "]";
return os;
}
// Debug assistant
#define DUMP(...) INTERNAL_DUMP(#__VA_ARGS__, __VA_ARGS__)
string REMOVE_SPACE(string s) {
while (s[0] == ' ') {
s = s.substr(1);
}
while (s[s.size() - 1] == ' ') {
s = s.substr(0, s.size() - 1);
}
return s;
}
pair<string, string> GET_NAME(string s) {
int bracket = 0, idx = 0;
while ((unsigned)idx < s.size() && !(s[idx] == ',' && bracket == 0)) {
if (s[idx] == '(')
++bracket;
else if (s[idx] == ')')
--bracket;
++idx;
}
string other_names;
if ((unsigned)idx == s.size()) {
other_names = "";
} else {
other_names = s.substr(idx + 1);
}
string first_name = s.substr(0, idx);
first_name = REMOVE_SPACE(first_name);
other_names = REMOVE_SPACE(other_names);
return pair<string, string>(first_name, other_names);
}
// Debug main
void INTERNAL_DUMP(string name) { return; }
template <class Head, class... Tail>
void INTERNAL_DUMP(string name, Head &&head, Tail &&...tail) {
pair<string, string> p = GET_NAME(name);
Debug::set_NAME_LENGTH(p.first.size());
cerr << ">> " << p.first << " = " << head << endl;
INTERNAL_DUMP(p.second, move(tail)...);
}
// End of Debug parts
//------------------------------------------------------------------------------
const long long INF = 1LL << 60;
vector<long long> a;
vector<vector<long long>> dp;
bool flip(bool turn) { return turn ? false : true; }
int sign(bool turn) { return turn ? 1 : -1; }
long long dfs(int i, int j, bool turn) {
if (dp[i][j] != INF)
return dp[i][j];
if (turn)
dp[i][j] = max(dfs(i + 1, j, flip(turn)) + sign(turn) * a[i],
dfs(i, j - 1, flip(turn)) + sign(turn) * a[j]);
else
dp[i][j] = min(dfs(i + 1, j, flip(turn)) + sign(turn) * a[i],
dfs(i, j - 1, flip(turn)) + sign(turn) * a[j]);
}
int main() {
Debug::set_INF(INF);
int N;
cin >> N;
a.resize(N);
for (int i = 0; i < N; ++i)
cin >> a[i];
dp.resize(N, vector<long long>(N));
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j)
dp[i][j] = INF;
for (int i = 0; i < N; ++i)
dp[i][i] = sign(N % 2) * a[i];
cout << dfs(0, N - 1, true) << endl;
DUMP(dp);
}
| #include <bits/stdc++.h>
using namespace std;
class Debug {
private:
static int NAME_LENGTH, MAX_WIDTH;
static bool IS_MATRIX;
static long long INF;
public:
static void set_NAME_LENGTH(int l) { NAME_LENGTH = l; }
static int get_NAME_LENGTH() { return NAME_LENGTH; }
static void set_MAX_WIDTH(int w) { MAX_WIDTH = w; }
static int get_MAX_WIDTH() { return MAX_WIDTH; }
static void set_IS_MATRIX(bool f) { IS_MATRIX = f; }
static bool get_IS_MATRIX() { return IS_MATRIX; }
static void set_INF(long long i) { INF = i; }
static long long get_INF() { return INF; }
};
int Debug::NAME_LENGTH = 0;
int Debug::MAX_WIDTH = 0;
bool Debug::IS_MATRIX = false;
long long Debug::INF = LLONG_MAX;
// Debug for vector
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << "[";
bool flg = true;
for (auto e : v) {
os << (flg ? "" : ", ");
if (Debug::get_IS_MATRIX()) {
os << right << setw(Debug::get_MAX_WIDTH());
}
if (e == Debug::get_INF())
os << "INF";
else
os << e;
flg = false;
}
os << "]";
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<vector<T>> &v) {
Debug::set_IS_MATRIX(true);
int width = 0;
for (unsigned int i = 0; i < v.size(); ++i) {
for (unsigned int j = 0; j < v[i].size(); ++j) {
if (v[i][j] == Debug::get_INF())
width = max(width, (int)("INF"s.size()));
else
width = max(width, (int)to_string(v[i][j]).size());
}
}
Debug::set_MAX_WIDTH(width);
string space = "";
for (int i = 0; i < Debug::get_NAME_LENGTH() + 7; ++i)
space += " ";
os << "[";
bool flg = true;
for (auto e : v) {
os << (flg ? "" : "\n" + space);
os << e;
flg = false;
}
os << "]";
Debug::set_IS_MATRIX(false);
return os;
}
// Debug for pair
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << "(" << p.first << ", " << p.second << ")";
return os;
}
// Debug for set
template <typename T> ostream &operator<<(ostream &os, const set<T> &s) {
os << "{";
bool flg = true;
for (auto e : s) {
os << (flg ? "" : ", ");
os << e;
flg = false;
}
os << "}";
return os;
}
// Debug for map
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &mp) {
os << "{";
bool flg = true;
for (auto e : mp) {
os << (flg ? "" : ", ");
os << e.first << ": " << e.second;
flg = false;
}
os << "}";
return os;
}
// Debug for queue
template <typename T> ostream &operator<<(ostream &os, const queue<T> &que) {
queue<T> que_cp = que;
os << "[";
bool flg = true;
while (!que_cp.empty()) {
os << (flg ? "" : ", ");
os << que_cp.front();
que_cp.pop();
flg = false;
}
os << "]";
return os;
}
// Debug for deque
template <typename T> ostream &operator<<(ostream &os, const deque<T> &que) {
os << "[";
bool flg = true;
for (auto e : que) {
os << (flg ? "" : ", ");
os << e;
flg = false;
}
os << "]";
return os;
}
// Debug assistant
#define DUMP(...) INTERNAL_DUMP(#__VA_ARGS__, __VA_ARGS__)
string REMOVE_SPACE(string s) {
while (s[0] == ' ') {
s = s.substr(1);
}
while (s[s.size() - 1] == ' ') {
s = s.substr(0, s.size() - 1);
}
return s;
}
pair<string, string> GET_NAME(string s) {
int bracket = 0, idx = 0;
while ((unsigned)idx < s.size() && !(s[idx] == ',' && bracket == 0)) {
if (s[idx] == '(')
++bracket;
else if (s[idx] == ')')
--bracket;
++idx;
}
string other_names;
if ((unsigned)idx == s.size()) {
other_names = "";
} else {
other_names = s.substr(idx + 1);
}
string first_name = s.substr(0, idx);
first_name = REMOVE_SPACE(first_name);
other_names = REMOVE_SPACE(other_names);
return pair<string, string>(first_name, other_names);
}
// Debug main
void INTERNAL_DUMP(string name) { return; }
template <class Head, class... Tail>
void INTERNAL_DUMP(string name, Head &&head, Tail &&...tail) {
pair<string, string> p = GET_NAME(name);
Debug::set_NAME_LENGTH(p.first.size());
cerr << ">> " << p.first << " = " << head << endl;
INTERNAL_DUMP(p.second, move(tail)...);
}
// End of Debug parts
//------------------------------------------------------------------------------
const long long INF = 1LL << 60;
vector<long long> a;
vector<vector<long long>> dp;
bool flip(bool turn) { return turn ? false : true; }
int sign(bool turn) { return turn ? 1 : -1; }
long long dfs(int i, int j, bool turn) {
if (dp[i][j] != INF)
return dp[i][j];
if (turn)
dp[i][j] = max(dfs(i + 1, j, flip(turn)) + sign(turn) * a[i],
dfs(i, j - 1, flip(turn)) + sign(turn) * a[j]);
else
dp[i][j] = min(dfs(i + 1, j, flip(turn)) + sign(turn) * a[i],
dfs(i, j - 1, flip(turn)) + sign(turn) * a[j]);
}
int main() {
Debug::set_INF(INF);
int N;
cin >> N;
a.resize(N);
for (int i = 0; i < N; ++i)
cin >> a[i];
dp.resize(N, vector<long long>(N));
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j)
dp[i][j] = INF;
for (int i = 0; i < N; ++i)
dp[i][i] = sign(N % 2) * a[i];
cout << dfs(0, N - 1, true) << endl;
// DUMP(dp);
}
| replace | 216 | 218 | 216 | 218 | TLE | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, s, n) for (int i = (s); i < (n); i++)
#define all(v) v.begin(), v.end()
#define outve(v) \
for (auto i : v) \
cout << i << " "; \
cout << endl
#define outmat(v) \
for (auto i : v) { \
for (auto j : i) \
cout << j << " "; \
cout << endl; \
}
#define in(n, v) \
for (int i = 0; i < (n); i++) { \
cin >> v[i]; \
}
#define out(n) cout << (n) << endl
#define fi first
#define se second
#define pb push_back
#define mod 1000000007
#define infi 1900000000
#define infl 1100000000000000000
#define cyes cout << "Yes" << endl
#define cno cout << "No" << endl
#define csp << " " <<
#define outset(n) cout << fixed << setprecision(n);
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vs = vector<string>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vvp = vector<vector<pair<int, int>>>;
template <typename T> using ve = vector<T>;
template <typename T> using pq2 = priority_queue<T>;
template <typename T> using pq1 = priority_queue<T, vector<T>, greater<T>>;
template <typename T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
int main() {
int N;
cin >> N;
vl a(N);
in(N, a);
vvl dp(3100, vl(3100, 0));
reps(aa, 1, N + 1) {
rep(i, N + 1) {
int j = aa + i;
if ((N - (j - i)) % 2 == 0) {
dp[i][j] = max(dp[i + 1][j] + a[i], dp[i][j - 1] + a[j - 1]);
} else {
dp[i][j] = min(dp[i + 1][j] - a[i], dp[i][j - 1] - a[j - 1]);
}
}
}
// outmat(dp);
cout << dp[0][N] << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, s, n) for (int i = (s); i < (n); i++)
#define all(v) v.begin(), v.end()
#define outve(v) \
for (auto i : v) \
cout << i << " "; \
cout << endl
#define outmat(v) \
for (auto i : v) { \
for (auto j : i) \
cout << j << " "; \
cout << endl; \
}
#define in(n, v) \
for (int i = 0; i < (n); i++) { \
cin >> v[i]; \
}
#define out(n) cout << (n) << endl
#define fi first
#define se second
#define pb push_back
#define mod 1000000007
#define infi 1900000000
#define infl 1100000000000000000
#define cyes cout << "Yes" << endl
#define cno cout << "No" << endl
#define csp << " " <<
#define outset(n) cout << fixed << setprecision(n);
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vs = vector<string>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vvp = vector<vector<pair<int, int>>>;
template <typename T> using ve = vector<T>;
template <typename T> using pq2 = priority_queue<T>;
template <typename T> using pq1 = priority_queue<T, vector<T>, greater<T>>;
template <typename T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
int main() {
int N;
cin >> N;
vl a(N);
in(N, a);
vvl dp(7100, vl(7100, 0));
reps(aa, 1, N + 1) {
rep(i, N + 1) {
int j = aa + i;
if ((N - (j - i)) % 2 == 0) {
dp[i][j] = max(dp[i + 1][j] + a[i], dp[i][j - 1] + a[j - 1]);
} else {
dp[i][j] = min(dp[i + 1][j] - a[i], dp[i][j - 1] - a[j - 1]);
}
}
}
// outmat(dp);
cout << dp[0][N] << endl;
return 0;
}
| replace | 65 | 66 | 65 | 66 | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.