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
|
---|---|---|---|---|---|---|---|---|---|---|---|
p03170 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
int main() {
int n, k;
cin >> n >> k;
vector<int> A(n);
rep(i, n) cin >> A[i];
bool dp[101010] = {0};
for (int i = 0; i < k + 1; i++) {
if (dp[i])
continue;
for (int j = 0; j < n; j++) {
dp[i + A[j]] = 1;
}
}
cout << (dp[k] ? "First" : "Second") << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
int main() {
int n, k;
cin >> n >> k;
vector<int> A(n);
rep(i, n) cin >> A[i];
bool dp[201010] = {0};
for (int i = 0; i < k + 1; i++) {
if (dp[i])
continue;
for (int j = 0; j < n; j++) {
dp[i + A[j]] = 1;
}
}
cout << (dp[k] ? "First" : "Second") << endl;
return 0;
} | replace | 10 | 11 | 10 | 11 | 0 | |
p03170 | C++ | Runtime Error | #include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define ll int64_t
#define F first
#define S second
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
using namespace std;
ll modex(ll a, int b, ll p) {
ll res = 1;
a %= p;
while (b > 0) {
if (b & 1)
res = (res * a) % p;
a = (a * a) % p;
b >>= 1;
}
return res;
}
int n;
int a[102];
int dp[2][102];
int win(int t, int k) {
if (dp[t][k] != -1)
return dp[t][k];
int w = 1 - t;
for (int i = 0; i < n; i++)
if (a[i] <= k && win(1 - t, k - a[i]) == t)
w = t;
return dp[t][k] = w;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
memset(dp, -1, sizeof dp);
int k;
cin >> n >> k;
for (int i = 0; i < n; i++)
cin >> a[i];
int ans = win(0, k);
if (ans == 0)
cout << "First\n";
else
cout << "Second\n";
return 0;
}
| #include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define ll int64_t
#define F first
#define S second
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
using namespace std;
ll modex(ll a, int b, ll p) {
ll res = 1;
a %= p;
while (b > 0) {
if (b & 1)
res = (res * a) % p;
a = (a * a) % p;
b >>= 1;
}
return res;
}
int n;
int a[102];
int dp[2][100005];
int win(int t, int k) {
if (dp[t][k] != -1)
return dp[t][k];
int w = 1 - t;
for (int i = 0; i < n; i++)
if (a[i] <= k && win(1 - t, k - a[i]) == t)
w = t;
return dp[t][k] = w;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
memset(dp, -1, sizeof dp);
int k;
cin >> n >> k;
for (int i = 0; i < n; i++)
cin >> a[i];
int ans = win(0, k);
if (ans == 0)
cout << "First\n";
else
cout << "Second\n";
return 0;
}
| replace | 23 | 24 | 23 | 24 | 0 | |
p03170 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
using namespace std;
int N, P, A[109], dp[109];
int main() {
cin >> N >> P;
for (int i = 1; i <= N; i++)
cin >> A[i];
for (int i = 1; i <= P; i++) {
bool used[109];
for (int j = 0; j < 109; j++)
used[j] = false;
for (int j = 1; j <= N; j++) {
if (i >= A[j])
used[dp[i - A[j]]] = true;
}
for (int j = 0; j < 109; j++) {
if (used[j] == false) {
dp[i] = j;
break;
}
}
}
if (dp[P] >= 1)
cout << "First" << endl;
else
cout << "Second" << endl;
return 0;
} | #include <algorithm>
#include <iostream>
using namespace std;
int N, P, A[100009], dp[100009];
int main() {
cin >> N >> P;
for (int i = 1; i <= N; i++)
cin >> A[i];
for (int i = 1; i <= P; i++) {
bool used[109];
for (int j = 0; j < 109; j++)
used[j] = false;
for (int j = 1; j <= N; j++) {
if (i >= A[j])
used[dp[i - A[j]]] = true;
}
for (int j = 0; j < 109; j++) {
if (used[j] == false) {
dp[i] = j;
break;
}
}
}
if (dp[P] >= 1)
cout << "First" << endl;
else
cout << "Second" << endl;
return 0;
} | replace | 4 | 5 | 4 | 5 | 0 | |
p03170 | C++ | Runtime Error | #include <iomanip>
#include <iostream>
using namespace std;
const long long MAXN = 3001;
int dp[MAXN];
int arr[MAXN];
int main() {
int n, k;
cin >> n >> k;
int mine = 1000000000;
for (int i = 0; i < n; i++) {
cin >> arr[i];
mine = min(arr[i], mine);
}
for (int i = mine; i <= k; i++) {
for (int j = 0; j < n; j++) {
if (i - arr[j] >= 0) {
if (!dp[i - arr[j]]) {
dp[i] = 1;
}
}
}
}
if (dp[k]) {
cout << "First";
} else {
cout << "Second";
}
}
| #include <iomanip>
#include <iostream>
using namespace std;
const long long MAXN = 100010;
int dp[MAXN];
int arr[MAXN];
int main() {
int n, k;
cin >> n >> k;
int mine = 1000000000;
for (int i = 0; i < n; i++) {
cin >> arr[i];
mine = min(arr[i], mine);
}
for (int i = mine; i <= k; i++) {
for (int j = 0; j < n; j++) {
if (i - arr[j] >= 0) {
if (!dp[i - arr[j]]) {
dp[i] = 1;
}
}
}
}
if (dp[k]) {
cout << "First";
} else {
cout << "Second";
}
}
| replace | 4 | 5 | 4 | 5 | 0 | |
p03170 | 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 D[100001];
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, n, p, q, r, t, x, y;
// cin>>t;while(t--){ map<ll,ll> M; string s;
cin >> n >> k;
fr(i, n) {
cin >> A[i];
// cout<<A[i];
}
vector<bool> dp(k + 1);
for (int stones = 0; stones <= k; stones++) {
fr(j, n) {
if (stones >= A[j] && dp[stones - A[j]] == false) {
dp[stones] = true;
// cout<<stones<<" ";
}
}
}
if (dp[k])
cout << "First\n";
else
cout << "Second" << 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 D[100001];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll a, b, c, d;
ll k, l, m, n, p, q, r, t, x, y;
// cin>>t;while(t--){ map<ll,ll> M; string s;
cin >> n >> k;
fr(i, n) {
cin >> A[i];
// cout<<A[i];
}
vector<bool> dp(k + 1);
for (int stones = 0; stones <= k; stones++) {
fr(j, n) {
if (stones >= A[j] && dp[stones - A[j]] == false) {
dp[stones] = true;
// cout<<stones<<" ";
}
}
}
if (dp[k])
cout << "First\n";
else
cout << "Second" << endl;
// }
}
| delete | 166 | 170 | 166 | 166 | -6 | terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p03170 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
const int MOD = 1e9 + 7;
#define INF ((1 << 30) - 1)
#define LINF (1LL << 30)
bool dp[110000];
int a[110];
int n, k;
int main() {
cin >> n >> k;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i <= k; i++) {
dp[i] = false;
for (int j = 0; j < n; j++) {
if (i - a[i] >= 0)
dp[i] |= !dp[i - a[j]];
}
}
if (dp[k])
cout << "First" << endl;
else
cout << "Second" << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
const int MOD = 1e9 + 7;
#define INF ((1 << 30) - 1)
#define LINF (1LL << 30)
bool dp[110000];
int a[110];
int n, k;
int main() {
cin >> n >> k;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i <= k; i++) {
dp[i] = false;
for (int j = 0; j < n; j++) {
if (i - a[j] >= 0)
dp[i] |= !dp[i - a[j]];
}
}
if (dp[k])
cout << "First" << endl;
else
cout << "Second" << endl;
return 0;
} | replace | 20 | 21 | 20 | 21 | 0 | |
p03170 | C++ | Runtime Error | #include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
#define PI 3.1415926535897932384626433832795l
#define IOS \
ios ::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0)
#define deci(n) cout << fixed << setprecision(n);
#define F first
#define S second
#define mk make_pair
#define pb push_back
#define ALPHA 27
#define ll long long int
#define ld long double
#define mmset(arr, n) memset(arr, n, sizeof arr)
#define debug(x) cerr << '#' << ' ' << x << '\n'
#define len(s) s.length()
#define ForA(i, a, b) for (int i = a; i < b; i++)
#define ForA1(i, a, b) for (int i = b - 1; i >= a; i--)
#define ForB1(i, a, b) for (int i = b; i > a; i--)
#define ForB(i, a, b) for (int i = a; i <= b; i++)
#define INT_SIZE 18
#define maxn 310
#define Maxn 100010
using namespace std;
const ll INF = (ll)(8e18);
const ll MOD = 1000000007;
int a[maxn], dp[maxn];
int main() {
IOS;
int n, k;
cin >> n >> k;
ForB(i, 1, n) cin >> a[i];
ForB(i, 0, k) {
ForB(j, 1, n) {
if (i >= a[j] && !dp[i - a[j]]) {
dp[i] = true;
}
}
}
cout << (dp[k] ? "First" : "Second") << '\n';
#ifdef LOCAL_DEFINE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}
| #include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
#define PI 3.1415926535897932384626433832795l
#define IOS \
ios ::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0)
#define deci(n) cout << fixed << setprecision(n);
#define F first
#define S second
#define mk make_pair
#define pb push_back
#define ALPHA 27
#define ll long long int
#define ld long double
#define mmset(arr, n) memset(arr, n, sizeof arr)
#define debug(x) cerr << '#' << ' ' << x << '\n'
#define len(s) s.length()
#define ForA(i, a, b) for (int i = a; i < b; i++)
#define ForA1(i, a, b) for (int i = b - 1; i >= a; i--)
#define ForB1(i, a, b) for (int i = b; i > a; i--)
#define ForB(i, a, b) for (int i = a; i <= b; i++)
#define INT_SIZE 18
#define maxn 310
#define Maxn 100010
using namespace std;
const ll INF = (ll)(8e18);
const ll MOD = 1000000007;
int a[maxn], dp[Maxn];
int main() {
IOS;
int n, k;
cin >> n >> k;
ForB(i, 1, n) cin >> a[i];
ForB(i, 0, k) {
ForB(j, 1, n) {
if (i >= a[j] && !dp[i - a[j]]) {
dp[i] = true;
}
}
}
cout << (dp[k] ? "First" : "Second") << '\n';
#ifdef LOCAL_DEFINE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}
| replace | 44 | 45 | 44 | 45 | 0 | |
p03170 | C++ | Runtime Error | #include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
typedef long long int ll;
using namespace std;
#define P 1000000007
#define rep(i, n) for (i = 0; i < n; ++i)
#define repp(i, a, n) for (i = a; i < n; ++i)
#define repr(i, a, n) for (i = a; i >= n; --i)
ll binpowmod(ll a, ll b, ll m) {
a %= m;
ll res = 1;
while (b > 0) {
if (b & 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
ll binpow(ll a, ll b) {
ll res = 1;
while (b > 0) {
if (b & 1)
res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
ll modInverse(ll n, ll p) { return binpowmod(n, p - 2, p); }
ll calc(ll dp[], ll k, ll a[], ll n) {
if (k < a[1])
return -1;
ll i;
for (i = 1; i <= n; ++i) {
if (dp[k - a[i]] == 0)
dp[k - a[i]] = calc(dp, k - a[i], a, n);
if (dp[k - a[i]] == -1)
return 1;
}
return -1;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n, i, j, k;
cin >> n >> k;
ll a[n + 1];
repp(i, 1, n + 1) cin >> a[i];
ll dp[k + 1];
memset(dp, 0, sizeof(dp));
ll ans = calc(dp, k, a, n);
// rep(i,n+1) cout << dp[i] << endl ;
// cout << ans ;
if (ans == 1)
cout << "First";
else
cout << "Second";
return 0;
} | #include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
typedef long long int ll;
using namespace std;
#define P 1000000007
#define rep(i, n) for (i = 0; i < n; ++i)
#define repp(i, a, n) for (i = a; i < n; ++i)
#define repr(i, a, n) for (i = a; i >= n; --i)
ll binpowmod(ll a, ll b, ll m) {
a %= m;
ll res = 1;
while (b > 0) {
if (b & 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
ll binpow(ll a, ll b) {
ll res = 1;
while (b > 0) {
if (b & 1)
res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
ll modInverse(ll n, ll p) { return binpowmod(n, p - 2, p); }
ll calc(ll dp[], ll k, ll a[], ll n) {
if (k < a[1])
return -1;
ll i;
for (i = 1; i <= n && k >= a[i]; ++i) {
if (dp[k - a[i]] == 0)
dp[k - a[i]] = calc(dp, k - a[i], a, n);
if (dp[k - a[i]] == -1)
return 1;
}
return -1;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n, i, j, k;
cin >> n >> k;
ll a[n + 1];
repp(i, 1, n + 1) cin >> a[i];
ll dp[k + 1];
memset(dp, 0, sizeof(dp));
ll ans = calc(dp, k, a, n);
// rep(i,n+1) cout << dp[i] << endl ;
// cout << ans ;
if (ans == 1)
cout << "First";
else
cout << "Second";
return 0;
} | replace | 40 | 41 | 40 | 41 | 0 | |
p03170 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long
ll mod = 1000000007;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll T;
T = 6;
start:
while (T--) {
ll n, m;
cin >> n >> m;
ll a[n];
for (ll i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
ll dp[m + 1];
for (ll i = 0; i < a[0]; i++) {
dp[i] = 2;
}
for (ll i = a[0]; i <= m; i++) {
dp[i] = 2;
for (ll j = 0; j < n; j++) {
if (a[j] <= i) {
ll t = dp[i - a[j]];
if (t == 2) {
dp[i] = 1;
break;
}
} else {
break;
}
}
// cout<<i<<"--"<<dp[i]<<endl;
}
if (dp[m] == 2) {
cout << "Second" << endl;
} else {
cout << "First" << endl;
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
ll mod = 1000000007;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll T;
T = 1;
start:
while (T--) {
ll n, m;
cin >> n >> m;
ll a[n];
for (ll i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
ll dp[m + 1];
for (ll i = 0; i < a[0]; i++) {
dp[i] = 2;
}
for (ll i = a[0]; i <= m; i++) {
dp[i] = 2;
for (ll j = 0; j < n; j++) {
if (a[j] <= i) {
ll t = dp[i - a[j]];
if (t == 2) {
dp[i] = 1;
break;
}
} else {
break;
}
}
// cout<<i<<"--"<<dp[i]<<endl;
}
if (dp[m] == 2) {
cout << "Second" << endl;
} else {
cout << "First" << endl;
}
}
return 0;
} | replace | 11 | 12 | 11 | 12 | 0 | |
p03170 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int n, k;
const int MAXN = 1e5 + 5;
int vis[MAXN];
vector<int> arr;
int main() {
cin >> n >> k;
arr.resize(n);
for (int i = 0; i < n; i++)
cin >> arr[i];
for (int i = 0; i <= k; i++)
for (auto val : arr)
if (!vis[i - val] && i - val >= 0)
vis[i] = true;
puts(vis[k] ? "First\n" : "Second\n");
}
| #include <bits/stdc++.h>
using namespace std;
int n, k;
const int MAXN = 1e5 + 5;
int vis[MAXN];
vector<int> arr;
int main() {
cin >> n >> k;
arr.resize(n);
for (int i = 0; i < n; i++)
cin >> arr[i];
for (int i = 0; i <= k; i++)
for (auto val : arr)
if (i - val >= 0 && !vis[i - val])
vis[i] = true;
puts(vis[k] ? "First\n" : "Second\n");
}
| replace | 13 | 14 | 13 | 14 | 0 | |
p03170 | C++ | Time Limit Exceeded | #include <iostream>
using namespace std;
int N, K;
int a[101];
bool dp[2][100010], vis[101][100010];
// true -> sente, false -> gote
bool rec(int cur, int k) {
if (vis[cur][k])
return dp[cur][k];
if (k == K) {
return cur % 2 == 1;
}
if (cur % 2 == 0) {
bool sente = false;
for (int i = 0; i < N; ++i) {
if (k + a[i] > K)
break;
sente |= rec((cur + 1) % 2, k + a[i]);
}
return dp[cur][k] = sente;
} else {
bool sente = true;
for (int i = 0; i < N; ++i) {
if (k + a[i] > K)
break;
sente &= rec((cur + 1) % 2, k + a[i]);
}
return dp[cur][k] = sente;
}
}
signed main() {
cin >> N >> K;
for (int i = 0; i < N; ++i)
cin >> a[i];
if (rec(0, 0)) {
cout << "First" << endl;
} else {
cout << "Second" << endl;
}
}
| #include <iostream>
using namespace std;
int N, K;
int a[101];
bool dp[2][100010], vis[101][100010];
// true -> sente, false -> gote
bool rec(int cur, int k) {
if (vis[cur][k])
return dp[cur][k];
if (k == K) {
return cur % 2 == 1;
}
vis[cur][k] = true;
if (cur % 2 == 0) {
bool sente = false;
for (int i = 0; i < N; ++i) {
if (k + a[i] > K)
break;
sente |= rec((cur + 1) % 2, k + a[i]);
}
return dp[cur][k] = sente;
} else {
bool sente = true;
for (int i = 0; i < N; ++i) {
if (k + a[i] > K)
break;
sente &= rec((cur + 1) % 2, k + a[i]);
}
return dp[cur][k] = sente;
}
}
signed main() {
cin >> N >> K;
for (int i = 0; i < N; ++i)
cin >> a[i];
if (rec(0, 0)) {
cout << "First" << endl;
} else {
cout << "Second" << endl;
}
}
| insert | 15 | 15 | 15 | 17 | TLE | |
p03170 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define _USE_MATH_DEFINES
#include <cmath>
long long MOD = 1000000007;
// vector<long long > t(4*100001);
long long binary_exp(long long a, long long b) {
long long ans = 1;
while (b > 0) {
if (b & 1) {
ans = (ans * a) % MOD;
}
a = (a * a) % MOD;
b = b >> 1;
}
return ans;
}
// long long gcd(long long a,long long b)
// {
// if(b>a)
// {
// return gcd(b,a);
// }
// if(b==0)
// {
// return a;
// }
// else
// {
// return gcd(b,a%b);
// }
// }
class comp {
public:
bool operator()(pair<long long, long long> a, pair<long long, long long> b) {
if (a.first > b.first) {
return false;
} else if (a.first == b.first && a.second < b.second) {
return false;
} else {
return true;
}
}
};
// void buildseg(long long x,long long l,long long r,vector<long long >
// &a,long long modi)
// {
// if(l==r)
// {
// t[x]=a[l]%modi;
// }
// else
// {
// long long mid=(l+r)/2;
// buildseg(2*x+1,l,mid,a,modi);
// buildseg(2*x+2,mid+1,r,a,modi);
// t[x]=(t[2*x+1]+t[2*x+2])%modi;
// }
// }
// long long sum(long long x,long long l1,long long r1,long long l,long
// long r,long long modi)
// {
// if(l>r)
// {
// return 0;
// }
// else if(l1==l && r1==r)
// {
// return t[x];
// }
// else
// {
// long long mid=(l1+r1)/2;
// return
// (sum(2*x+1,l1,mid,l,min(r,mid),modi)+sum(2*x+2,mid+1,r1,max(l,mid+1),r,modi))%modi;
// }
// }
// void update(long long x,long long l,long long r,long long value,long long
// pos,long long modi)
// {
// if(l==r)
// {
// t[x]=value%modi;
// }
// else
// {
// long long mid=(l+r)/2;
// if(mid>=pos)
// {
// update(2*x+1,l,mid,value,pos,modi);
// }
// else
// {
// update(2*x+2,mid+1,r,value,pos,modi);
// }
// t[x]=(t[2*x+1]+t[2*x+2])%modi;
// }
// }
// void prdoubleseg(long long n)
// {
// for(long long i=0;i<4*n;i++)
// {
// cout<<t[i]<<" ";
// }
// cout<<endl;
// }
// void dfs(double x, vector<vector<double> > &adj,vector<set<double> >
// &v,double &count,vector<double> &vis)
// {
// v[x].insert(count);
// vis[x]=1;
// for(auto i : adj[x])
// {
// if(vis[i]==0)
// {
// vis[i]=1;
// dfs(i,adj,v,count,vis);
// }
// }
// }
void bfs(long long s, vector<vector<int>> adj, vector<int> &vis,
vector<int> &pred) {
queue<int> q;
q.push(s);
vis[s] = 1;
while (!q.empty()) {
long long j = q.front();
q.pop();
// cout<<j<<" ";
for (auto i : adj[j]) {
if (vis[i] == 0) {
q.push(i);
pred[i] = j;
vis[i] = 1;
}
}
}
}
// long long diff(long long a ,long long b,long long c)
// {
// long long q=min(a,b);
// q=min(q,c);
// long long w=max(a,b);
// w=max(w,c);
// return (w-q);
// }
map<pair<int, int>, int> mp;
vector<int> ans(100009, -1);
bool dp(vector<int> &a, int n, int k, int mini) {
if (k < mini) {
ans[k] = 0;
return false;
}
if (ans[k] != -1) {
return ans[k];
}
for (int i = 0; i < n; i++) {
if (dp(a, n, k - a[i], mini) == false) {
ans[k] = 1;
return true;
}
}
ans[k] = 0;
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long T;
T = 1;
while (T--) {
int n, k;
cin >> n >> k;
vector<int> a(n);
int mini = INT_MAX;
for (int i = 0; i < n; i++) {
cin >> a[i];
mini = min(mini, a[i]);
}
bool w = dp(a, n, k, mini);
if (w)
cout << "First" << endl;
else
cout << "Second" << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define _USE_MATH_DEFINES
#include <cmath>
long long MOD = 1000000007;
// vector<long long > t(4*100001);
long long binary_exp(long long a, long long b) {
long long ans = 1;
while (b > 0) {
if (b & 1) {
ans = (ans * a) % MOD;
}
a = (a * a) % MOD;
b = b >> 1;
}
return ans;
}
// long long gcd(long long a,long long b)
// {
// if(b>a)
// {
// return gcd(b,a);
// }
// if(b==0)
// {
// return a;
// }
// else
// {
// return gcd(b,a%b);
// }
// }
class comp {
public:
bool operator()(pair<long long, long long> a, pair<long long, long long> b) {
if (a.first > b.first) {
return false;
} else if (a.first == b.first && a.second < b.second) {
return false;
} else {
return true;
}
}
};
// void buildseg(long long x,long long l,long long r,vector<long long >
// &a,long long modi)
// {
// if(l==r)
// {
// t[x]=a[l]%modi;
// }
// else
// {
// long long mid=(l+r)/2;
// buildseg(2*x+1,l,mid,a,modi);
// buildseg(2*x+2,mid+1,r,a,modi);
// t[x]=(t[2*x+1]+t[2*x+2])%modi;
// }
// }
// long long sum(long long x,long long l1,long long r1,long long l,long
// long r,long long modi)
// {
// if(l>r)
// {
// return 0;
// }
// else if(l1==l && r1==r)
// {
// return t[x];
// }
// else
// {
// long long mid=(l1+r1)/2;
// return
// (sum(2*x+1,l1,mid,l,min(r,mid),modi)+sum(2*x+2,mid+1,r1,max(l,mid+1),r,modi))%modi;
// }
// }
// void update(long long x,long long l,long long r,long long value,long long
// pos,long long modi)
// {
// if(l==r)
// {
// t[x]=value%modi;
// }
// else
// {
// long long mid=(l+r)/2;
// if(mid>=pos)
// {
// update(2*x+1,l,mid,value,pos,modi);
// }
// else
// {
// update(2*x+2,mid+1,r,value,pos,modi);
// }
// t[x]=(t[2*x+1]+t[2*x+2])%modi;
// }
// }
// void prdoubleseg(long long n)
// {
// for(long long i=0;i<4*n;i++)
// {
// cout<<t[i]<<" ";
// }
// cout<<endl;
// }
// void dfs(double x, vector<vector<double> > &adj,vector<set<double> >
// &v,double &count,vector<double> &vis)
// {
// v[x].insert(count);
// vis[x]=1;
// for(auto i : adj[x])
// {
// if(vis[i]==0)
// {
// vis[i]=1;
// dfs(i,adj,v,count,vis);
// }
// }
// }
void bfs(long long s, vector<vector<int>> adj, vector<int> &vis,
vector<int> &pred) {
queue<int> q;
q.push(s);
vis[s] = 1;
while (!q.empty()) {
long long j = q.front();
q.pop();
// cout<<j<<" ";
for (auto i : adj[j]) {
if (vis[i] == 0) {
q.push(i);
pred[i] = j;
vis[i] = 1;
}
}
}
}
// long long diff(long long a ,long long b,long long c)
// {
// long long q=min(a,b);
// q=min(q,c);
// long long w=max(a,b);
// w=max(w,c);
// return (w-q);
// }
map<pair<int, int>, int> mp;
vector<int> ans(100009, -1);
bool dp(vector<int> &a, int n, int k, int mini) {
if (k < mini) {
ans[k] = 0;
return false;
}
if (ans[k] != -1) {
return ans[k];
}
for (int i = 0; i < n; i++) {
if (k - a[i] >= 0) {
if (dp(a, n, k - a[i], mini) == false) {
ans[k] = 1;
return true;
}
}
}
ans[k] = 0;
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long T;
T = 1;
while (T--) {
int n, k;
cin >> n >> k;
vector<int> a(n);
int mini = INT_MAX;
for (int i = 0; i < n; i++) {
cin >> a[i];
mini = min(mini, a[i]);
}
bool w = dp(a, n, k, mini);
if (w)
cout << "First" << endl;
else
cout << "Second" << endl;
}
return 0;
}
| replace | 157 | 160 | 157 | 162 | 0 | |
p03170 | C++ | Runtime Error | #include <bits/stdc++.h>
#define int long long int
#define len length
#define pb push_back
#define F first
#define S second
#define all(a) a.begin(), a.end()
#define scnarr(a, n) \
for (int i = 0; i < n; ++i) \
cin >> a[i]
#define vi vector<int>
#define pii pair<int, int>
#define mii map<int, int>
#define faster \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
using namespace std;
// Constants
const int MOD = 998244353; // 1e9 + 7
const int N = 1000001; // 1e6 +1
/* -------------------------------Solution
* Sarted--------------------------------------*/
int memo[100005][2], n;
vi a;
bool solve(int k, bool turn) {
int i = 0;
if (memo[k][turn] != -1)
return memo[k][turn];
if (!turn) {
bool res = 0;
for (; i < n; ++i) {
res = 0;
if (k - a[i] >= 0) {
res = res or (solve(k - a[i], !turn));
} else {
break;
}
if (res == 0)
return (memo[k][turn] = 0);
}
return (memo[k][turn] = 1);
} else {
bool res = 1;
for (; i < n; ++i) {
res = 1;
if (k - a[i] >= 0) {
res = res and (solve(k - a[i], !turn));
} else {
break;
}
if (res == 1)
return (memo[k][turn] = 1);
}
return (memo[k][turn] = 0);
}
}
int32_t main() {
faster;
// #ifndef ONLINE_JUDGE
// freopen("ip.txt", "r", stdin);
// freopen("op.txt", "w", stdout);
// #endif
int t;
cin >> t;
while (t--) {
int k;
cin >> n >> k;
a.resize(n);
scnarr(a, n);
memset(memo, -1, sizeof(memo));
sort(all(a));
(solve(k, 0) ? puts("Second") : puts("First"));
}
return 0;
}
// Author : Ankit Raj
// InSearchForMeanings
// Problem Link :
| #include <bits/stdc++.h>
#define int long long int
#define len length
#define pb push_back
#define F first
#define S second
#define all(a) a.begin(), a.end()
#define scnarr(a, n) \
for (int i = 0; i < n; ++i) \
cin >> a[i]
#define vi vector<int>
#define pii pair<int, int>
#define mii map<int, int>
#define faster \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
using namespace std;
// Constants
const int MOD = 998244353; // 1e9 + 7
const int N = 1000001; // 1e6 +1
/* -------------------------------Solution
* Sarted--------------------------------------*/
int memo[100005][2], n;
vi a;
bool solve(int k, bool turn) {
int i = 0;
if (memo[k][turn] != -1)
return memo[k][turn];
if (!turn) {
bool res = 0;
for (; i < n; ++i) {
res = 0;
if (k - a[i] >= 0) {
res = res or (solve(k - a[i], !turn));
} else {
break;
}
if (res == 0)
return (memo[k][turn] = 0);
}
return (memo[k][turn] = 1);
} else {
bool res = 1;
for (; i < n; ++i) {
res = 1;
if (k - a[i] >= 0) {
res = res and (solve(k - a[i], !turn));
} else {
break;
}
if (res == 1)
return (memo[k][turn] = 1);
}
return (memo[k][turn] = 0);
}
}
int32_t main() {
faster;
// #ifndef ONLINE_JUDGE
// freopen("ip.txt", "r", stdin);
// freopen("op.txt", "w", stdout);
// #endif
// int t; cin >> t; while(t--)
{
int k;
cin >> n >> k;
a.resize(n);
scnarr(a, n);
memset(memo, -1, sizeof(memo));
sort(all(a));
(solve(k, 0) ? puts("Second") : puts("First"));
}
return 0;
}
// Author : Ankit Raj
// InSearchForMeanings
// Problem Link :
| replace | 71 | 74 | 71 | 73 | -11 | |
p03170 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ld long double
#define ull unsigned long long int
#define ll long long int
#define SPEED \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define mod 1000000007ll
#define rep(i, L, R) for (ll i = L; i < R; i++)
#define f(n) rep(i, 0, n)
#define pll pair<ll, ll>
#define DEBUG
#ifdef DEBUG
#define deb2(args...) \
{ \
dbg, args; \
cerr << endl; \
}
#else
#define deb2(args...)
#endif
struct debb {
template <typename T> debb &operator,(const T &v) {
cerr << v << " ";
return *this;
}
} dbg;
#define deb1(args...) \
do { \
cerr << #args << " : "; \
dbg, args; \
cerr << "\n"; \
} while (0)
#define printa(a, L, R) \
rep(i, L, R) cout << a[i] << ' '; \
cout << "\n";
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag,
tree_order_statistics_node_update>
indexed_set;
template <typename T> T gcd(T a, T b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
ll power(ll x, ll y, ll p) {
ll res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = ((res % p) * (x % p)) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
ll pow1(ll x, ll y) {
ll res = 1;
while (y > 0) {
if (y & 1)
res = (res * x);
y = y >> 1;
x = x * x;
}
return res;
}
#define sq(X) ((X) * (X))
#define PI 3.141592653589
const ll INF = 1e18l + 10, N = 1e5 + 1, MAX = 2e5 + 10;
// A B C D H F E I G K L M N P S O Q R J U Z Y X T V W
int main() {
int n, k, i, j;
cin >> n >> k;
bitset<10001> dp;
int a[n];
f(n) cin >> a[i];
for (i = 0; i <= k; i++) {
for (j = 0; j < n; j++) {
if (i + a[j] > k || dp[i])
break;
dp[i + a[j]] = 1;
}
}
if (dp[k])
cout << "First\n";
else
cout << "Second\n";
} | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ld long double
#define ull unsigned long long int
#define ll long long int
#define SPEED \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define mod 1000000007ll
#define rep(i, L, R) for (ll i = L; i < R; i++)
#define f(n) rep(i, 0, n)
#define pll pair<ll, ll>
#define DEBUG
#ifdef DEBUG
#define deb2(args...) \
{ \
dbg, args; \
cerr << endl; \
}
#else
#define deb2(args...)
#endif
struct debb {
template <typename T> debb &operator,(const T &v) {
cerr << v << " ";
return *this;
}
} dbg;
#define deb1(args...) \
do { \
cerr << #args << " : "; \
dbg, args; \
cerr << "\n"; \
} while (0)
#define printa(a, L, R) \
rep(i, L, R) cout << a[i] << ' '; \
cout << "\n";
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag,
tree_order_statistics_node_update>
indexed_set;
template <typename T> T gcd(T a, T b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
ll power(ll x, ll y, ll p) {
ll res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = ((res % p) * (x % p)) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
ll pow1(ll x, ll y) {
ll res = 1;
while (y > 0) {
if (y & 1)
res = (res * x);
y = y >> 1;
x = x * x;
}
return res;
}
#define sq(X) ((X) * (X))
#define PI 3.141592653589
const ll INF = 1e18l + 10, N = 1e5 + 1, MAX = 2e5 + 10;
// A B C D H F E I G K L M N P S O Q R J U Z Y X T V W
int main() {
int n, k, i, j;
cin >> n >> k;
bitset<100001> dp;
int a[n];
f(n) cin >> a[i];
for (i = 0; i <= k; i++) {
for (j = 0; j < n; j++) {
if (i + a[j] > k || dp[i])
break;
dp[i + a[j]] = 1;
}
}
if (dp[k])
cout << "First\n";
else
cout << "Second\n";
} | replace | 79 | 80 | 79 | 80 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef int integer;
#define FIN \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define ll long long
#define ld long double
#define pb push_back
#define bs binary_search
#define up upper_bound
#define lw lower_bound
#define fs first
#define sd second
#define mp make_pair
#define fore(i, a, b) for (ll i = a; i < b; i++)
#define rfore(i, a, b) for (ll i = a - 1; i >= b; i--)
#define forev(a, b) fore(i, 0, b) cout << a[i] << " ";
#define mm(a, b) memset(a, b, sizeof a)
#define ALL(a) a.begin(), a.end()
#define SZ(n) ((ll)(n).size())
#define infi 2e9
#define int long long
#define endl '\n'
const ld pi = 3.14159265358979323846;
const ll mod = 1e9 + 7;
const ll INF = 1e10;
const ld EPS = 1e-8;
typedef pair<ll, ll> pll;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); }
const int MAXN = 2000;
const int K = 20;
int n, m;
int a[MAXN];
int dp[MAXN][MAXN][2];
int solve(int i, int j, int t) {
if (i > j)
return 0;
int &r = dp[i][j][t];
if (r != -1)
return r;
r = 0;
if (t == 0) {
r = max(a[i] + solve(i + 1, j, 1), a[j] + solve(i, j - 1, 1));
} else {
r = min(solve(i + 1, j, 0) - a[i], solve(i, j - 1, 0) - a[j]);
}
return r;
}
integer main() {
cin >> n;
mm(dp, -1);
fore(i, 0, n) cin >> a[i];
int r = solve(0, n - 1, 0);
cout << r;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef int integer;
#define FIN \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define ll long long
#define ld long double
#define pb push_back
#define bs binary_search
#define up upper_bound
#define lw lower_bound
#define fs first
#define sd second
#define mp make_pair
#define fore(i, a, b) for (ll i = a; i < b; i++)
#define rfore(i, a, b) for (ll i = a - 1; i >= b; i--)
#define forev(a, b) fore(i, 0, b) cout << a[i] << " ";
#define mm(a, b) memset(a, b, sizeof a)
#define ALL(a) a.begin(), a.end()
#define SZ(n) ((ll)(n).size())
#define infi 2e9
#define int long long
#define endl '\n'
const ld pi = 3.14159265358979323846;
const ll mod = 1e9 + 7;
const ll INF = 1e10;
const ld EPS = 1e-8;
typedef pair<ll, ll> pll;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); }
const int MAXN = 3001;
const int K = 20;
int n, m;
int a[MAXN];
int dp[MAXN][MAXN][2];
int solve(int i, int j, int t) {
if (i > j)
return 0;
int &r = dp[i][j][t];
if (r != -1)
return r;
r = 0;
if (t == 0) {
r = max(a[i] + solve(i + 1, j, 1), a[j] + solve(i, j - 1, 1));
} else {
r = min(solve(i + 1, j, 0) - a[i], solve(i, j - 1, 0) - a[j]);
}
return r;
}
integer main() {
cin >> n;
mm(dp, -1);
fore(i, 0, n) cin >> a[i];
int r = solve(0, n - 1, 0);
cout << r;
return 0;
}
| replace | 39 | 40 | 39 | 40 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace std::chrono;
using namespace __gnu_pbds;
#define fastio \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define fi first
#define se second
#define int long long
#define pb push_back
#define emp emplace_back
#define vv(x) vector<x>
#define mp(x, y) map<x, y>
#define dq(x) deque<x>
#define pql(x) priority_queue<x>
#define pqs(x) priority_queue<x, vv(x), greater<x>>
#define M 1000000007
#define forf(i, a, b) for (int i = a; i < b; i++)
#define it(x) x::iterator
#define ll long long
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define time__(d) \
for (long blockTime = 0; \
(blockTime == 0 ? (blockTime = clock()) != 0 : false); \
debug("%s time : %.4fs", d, \
(double)(clock() - blockTime) / CLOCKS_PER_SEC))
#define vii vector<int>
#define big 3e18
#define sm -2e9
#define mkr make_pair
#define vpi vector<pair<int, int>>
#define pii pair<int, int>
#define rng 500005
#define sz(x) (int)x.size()
#define rv(x) reverse(x.begin(), x.end())
#define out(x) cout << x.fi << " " << x.se << endl;
// #define ordered_set tree<pii, null_type,less<pii>,
// rb_tree_tag,tree_order_statistics_node_update>
void pr_init() {
#ifndef ONLINE_JUDGE
freopen("gin.txt", "r", stdin);
freopen("gout.txt", "w", stdout);
#endif
}
int a[301], dp[3001][3001][2], n;
int recur(int i, int j, int x) {
if (i == j) {
return (x % 2 ? -1 : 1) * a[i];
}
if (dp[i][j][x] != -1)
return dp[i][j][x];
if (!x) {
dp[i][j][x] = max(a[i] + recur(i + 1, j, (x + 1) % 2),
a[j] + recur(i, j - 1, (x + 1) % 2));
} else {
dp[i][j][x] = min(recur(i + 1, j, (x + 1) % 2) - a[i],
recur(i, j - 1, (x + 1) % 2) - a[j]);
}
return dp[i][j][x];
}
void solve() {
cin >> n;
forf(i, 0, 3001) { forf(j, 0, 3001) dp[i][j][0] = dp[i][j][1] = -1; }
forf(i, 1, n + 1) cin >> a[i];
cout << recur(1, n, 0);
}
int32_t main() {
pr_init();
// fastio;
auto start = high_resolution_clock::now();
solve();
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
// cout << "Time taken by function: "
// << duration.count() << " microseconds" << endl;
}
| #include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace std::chrono;
using namespace __gnu_pbds;
#define fastio \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define fi first
#define se second
#define int long long
#define pb push_back
#define emp emplace_back
#define vv(x) vector<x>
#define mp(x, y) map<x, y>
#define dq(x) deque<x>
#define pql(x) priority_queue<x>
#define pqs(x) priority_queue<x, vv(x), greater<x>>
#define M 1000000007
#define forf(i, a, b) for (int i = a; i < b; i++)
#define it(x) x::iterator
#define ll long long
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define time__(d) \
for (long blockTime = 0; \
(blockTime == 0 ? (blockTime = clock()) != 0 : false); \
debug("%s time : %.4fs", d, \
(double)(clock() - blockTime) / CLOCKS_PER_SEC))
#define vii vector<int>
#define big 3e18
#define sm -2e9
#define mkr make_pair
#define vpi vector<pair<int, int>>
#define pii pair<int, int>
#define rng 500005
#define sz(x) (int)x.size()
#define rv(x) reverse(x.begin(), x.end())
#define out(x) cout << x.fi << " " << x.se << endl;
// #define ordered_set tree<pii, null_type,less<pii>,
// rb_tree_tag,tree_order_statistics_node_update>
void pr_init() {
#ifndef ONLINE_JUDGE
freopen("gin.txt", "r", stdin);
freopen("gout.txt", "w", stdout);
#endif
}
int a[3001], dp[3001][3001][2], n;
int recur(int i, int j, int x) {
if (i == j) {
return (x % 2 ? -1 : 1) * a[i];
}
if (dp[i][j][x] != -1)
return dp[i][j][x];
if (!x) {
dp[i][j][x] = max(a[i] + recur(i + 1, j, (x + 1) % 2),
a[j] + recur(i, j - 1, (x + 1) % 2));
} else {
dp[i][j][x] = min(recur(i + 1, j, (x + 1) % 2) - a[i],
recur(i, j - 1, (x + 1) % 2) - a[j]);
}
return dp[i][j][x];
}
void solve() {
cin >> n;
forf(i, 0, 3001) { forf(j, 0, 3001) dp[i][j][0] = dp[i][j][1] = -1; }
forf(i, 1, n + 1) cin >> a[i];
cout << recur(1, n, 0);
}
int32_t main() {
pr_init();
// fastio;
auto start = high_resolution_clock::now();
solve();
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
// cout << "Time taken by function: "
// << duration.count() << " microseconds" << endl;
}
| replace | 54 | 55 | 54 | 55 | -11 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dp1[3001][3001], dp2[3001][3001];
ll a[3001];
ll f(ll i, ll j, ll y) {
if (y == -1) {
if (dp2[i][j] != -1) {
return dp2[i][j];
}
if (i == j) {
return dp2[i][j] = y * a[i];
}
return dp2[i][j] =
min(y * a[i] + f(i + 1, j, -y), y * a[j] + f(i, j - 1, -y));
}
if (y == 1) {
if (dp1[i][j] != -1) {
return dp1[i][j];
}
if (i == j) {
return dp1[i][j] = y * a[i];
}
return dp1[i][j] =
max(y * a[i] + f(i + 1, j, -y), y * a[j] + f(i, j - 1, -y));
}
}
signed main() {
memset(dp1, -1, sizeof dp1);
memset(dp2, -1, sizeof dp2);
ll n;
cin >> n;
for (ll i = 0; i < n; i++)
cin >> a[i];
cout << max(a[0] + f(1, n - 1, -1), a[n - 1] + f(0, n - 2, -1));
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dp1[3001][3001], dp2[3001][3001];
ll a[3001];
ll f(ll i, ll j, ll y) {
if (y == -1) {
if (dp2[i][j] != -1) {
return dp2[i][j];
}
if (i == j) {
return dp2[i][j] = y * a[i];
}
return dp2[i][j] =
min(y * a[i] + f(i + 1, j, -y), y * a[j] + f(i, j - 1, -y));
}
if (y == 1) {
if (dp1[i][j] != -1) {
return dp1[i][j];
}
if (i == j) {
return dp1[i][j] = y * a[i];
}
return dp1[i][j] =
max(y * a[i] + f(i + 1, j, -y), y * a[j] + f(i, j - 1, -y));
}
}
signed main() {
memset(dp1, -1, sizeof dp1);
memset(dp2, -1, sizeof dp2);
ll n;
cin >> n;
for (ll i = 0; i < n; i++)
cin >> a[i];
if (n == 1) {
cout << a[0] << '\n';
return 0;
}
cout << max(a[0] + f(1, n - 1, -1), a[n - 1] + f(0, n - 2, -1));
return 0;
}
| insert | 34 | 34 | 34 | 38 | -11 | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
// def
#define debug(x) cout << #x << ": " << x << endl
#define out(x) cout << x << endl
#define repeat(i, a, b) for (int i = (a); i < (b); i++)
#define revrepeat(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define revrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define CYES cout << "Yes" << endl
#define CNO cout << "No" << endl
#define SPC(x) cout << fixed << setprecision(x)
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int>> vii;
typedef vector<ll> vl;
typedef vector<vector<ll>> vll;
typedef pair<int, int> P;
typedef complex<ld> Point;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const int MAX_V = 1e5 + 5;
const int MAX_N = 1e5 + 5;
const double PI = acos(-1);
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;
}
ll a[3005];
ll dp[3005][3005];
ll f(int l, int r) {
if (l == r)
return a[l];
dp[l][r] = max(a[l] - f(l + 1, r), a[r] - f(l, r - 1));
return dp[l][r];
}
int main() {
int n;
cin >> n;
rep(i, n) cin >> a[i];
out(f(0, n - 1));
}
| #include <bits/stdc++.h>
using namespace std;
// def
#define debug(x) cout << #x << ": " << x << endl
#define out(x) cout << x << endl
#define repeat(i, a, b) for (int i = (a); i < (b); i++)
#define revrepeat(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define revrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define CYES cout << "Yes" << endl
#define CNO cout << "No" << endl
#define SPC(x) cout << fixed << setprecision(x)
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int>> vii;
typedef vector<ll> vl;
typedef vector<vector<ll>> vll;
typedef pair<int, int> P;
typedef complex<ld> Point;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const int MAX_V = 1e5 + 5;
const int MAX_N = 1e5 + 5;
const double PI = acos(-1);
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;
}
ll a[3005];
ll dp[3005][3005];
ll f(int l, int r) {
if (dp[l][r])
return dp[l][r];
if (l == r)
return a[l];
dp[l][r] = max(a[l] - f(l + 1, r), a[r] - f(l, r - 1));
return dp[l][r];
}
int main() {
int n;
cin >> n;
rep(i, n) cin >> a[i];
out(f(0, n - 1));
}
| insert | 49 | 49 | 49 | 51 | TLE | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define int long long
#define pb push_back
#define mod 1000000007
#define setbits(x) __builtin_popcountll(x)
#define zerobits(x) __builtin_ctzll(x)
#define inf 1e18
#define pre(x, y) fixed << setprecision(y) << x
#define pq priority_queue<int>
#define mpq priority_queue<int, vector<int>, greater<int>>
#define gcd(x, y) __gcd(x, y)
#define mp make_pair
int solveGame(int a[], vector<vector<int>> dp, int i, int j, int n) {
if (i > j)
return 0;
if (i == j)
return dp[i][i] = a[i];
else if (dp[i][j] != -1)
return dp[i][j];
else {
int x = a[i] + min(solveGame(a, dp, i + 1, j - 1, n),
solveGame(a, dp, i + 2, j, n));
int y = a[j] + min(solveGame(a, dp, i + 1, j - 1, n),
solveGame(a, dp, i, j - 2, n));
return dp[i][j] = max(x, y);
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, i, j, k;
cin >> n;
int a[n];
for (i = 0; i < n; i++)
cin >> a[i];
vector<vector<int>> dp(n, vector<int>(n));
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)
dp[i][j] = -1;
}
k = solveGame(a, dp, 0, n - 1, n);
int sum = 0;
for (i = 0; i < n; i++)
sum += a[i];
cout << k - (sum - k) << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define int long long
#define pb push_back
#define mod 1000000007
#define setbits(x) __builtin_popcountll(x)
#define zerobits(x) __builtin_ctzll(x)
#define inf 1e18
#define pre(x, y) fixed << setprecision(y) << x
#define pq priority_queue<int>
#define mpq priority_queue<int, vector<int>, greater<int>>
#define gcd(x, y) __gcd(x, y)
#define mp make_pair
int solveGame(int a[], vector<vector<int>> &dp, int i, int j, int n) {
if (i > j)
return 0;
if (i == j)
return dp[i][i] = a[i];
else if (dp[i][j] != -1)
return dp[i][j];
else {
int x = a[i] + min(solveGame(a, dp, i + 1, j - 1, n),
solveGame(a, dp, i + 2, j, n));
int y = a[j] + min(solveGame(a, dp, i + 1, j - 1, n),
solveGame(a, dp, i, j - 2, n));
return dp[i][j] = max(x, y);
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, i, j, k;
cin >> n;
int a[n];
for (i = 0; i < n; i++)
cin >> a[i];
vector<vector<int>> dp(n, vector<int>(n));
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)
dp[i][j] = -1;
}
k = solveGame(a, dp, 0, n - 1, n);
int sum = 0;
for (i = 0; i < n; i++)
sum += a[i];
cout << k - (sum - k) << endl;
return 0;
}
| replace | 15 | 16 | 15 | 16 | TLE | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#include <cmath>
#define SZ(x) ((int)x.size())
#include <map>
#include <set>
#include <vector>
#define ll long long
#define lli long long int
#define REP(i, a, b) for (ll i = a; i < b; i++)
#define pb push_back
#include <cctype>
#include <queue>
#include <string>
#define ii pair<int, int>
#include <sstream>
#define lli long long int
#define pairs pair<int, int>
#define mod 1000000007
const double PI = 3.141592653589793238460;
typedef std::complex<double> Complex;
typedef std::valarray<Complex> CArray;
using namespace std;
ll pows(ll a, ll n, ll m) {
ll res = 1;
while (n) {
if (n % 2 != 0) {
res = (res * a) % m;
n--;
} else {
a = (a * a) % m;
n = n / 2;
}
}
return res % m;
}
ll gcd(ll a, ll b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
bool isprime(ll n) {
if (n == 1) {
return false;
}
for (ll i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
bool istrue(string s) {
int i = 0;
int j = s.size() - 1;
while (i < j) {
if (s[i] == s[j]) {
i++;
j--;
} else {
return false;
}
}
return true;
}
ll n;
ll a[3005];
ll dp[3005][3005][2];
ll getans(ll find, ll flast, ll play) {
if (find > flast) {
return 0;
}
if (dp[find][flast][play] != -1) {
return dp[find][flast][play];
}
ll ans = 0;
if (play == 0) {
ans = max(a[find] + getans(find + 1, flast, 1),
a[flast] + getans(find, flast - 1, 1));
} else {
ans = min(-a[find] + getans(find + 1, flast, 0),
-a[flast] + getans(find, flast - 1, 0));
}
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
memset(dp, -1, sizeof(dp));
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
cout << getans(0, n - 1, 0) << endl;
}
| #include <bits/stdc++.h>
#include <cmath>
#define SZ(x) ((int)x.size())
#include <map>
#include <set>
#include <vector>
#define ll long long
#define lli long long int
#define REP(i, a, b) for (ll i = a; i < b; i++)
#define pb push_back
#include <cctype>
#include <queue>
#include <string>
#define ii pair<int, int>
#include <sstream>
#define lli long long int
#define pairs pair<int, int>
#define mod 1000000007
const double PI = 3.141592653589793238460;
typedef std::complex<double> Complex;
typedef std::valarray<Complex> CArray;
using namespace std;
ll pows(ll a, ll n, ll m) {
ll res = 1;
while (n) {
if (n % 2 != 0) {
res = (res * a) % m;
n--;
} else {
a = (a * a) % m;
n = n / 2;
}
}
return res % m;
}
ll gcd(ll a, ll b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
bool isprime(ll n) {
if (n == 1) {
return false;
}
for (ll i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
bool istrue(string s) {
int i = 0;
int j = s.size() - 1;
while (i < j) {
if (s[i] == s[j]) {
i++;
j--;
} else {
return false;
}
}
return true;
}
ll n;
ll a[3005];
ll dp[3005][3005][2];
ll getans(ll find, ll flast, ll play) {
if (find > flast) {
return 0;
}
if (dp[find][flast][play] != -1) {
return dp[find][flast][play];
}
ll ans = 0;
if (play == 0) {
ans = max(a[find] + getans(find + 1, flast, 1),
a[flast] + getans(find, flast - 1, 1));
} else {
ans = min(-a[find] + getans(find + 1, flast, 0),
-a[flast] + getans(find, flast - 1, 0));
}
return dp[find][flast][play] = ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
memset(dp, -1, sizeof(dp));
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
cout << getans(0, n - 1, 0) << endl;
}
| replace | 83 | 84 | 83 | 84 | TLE | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
typedef long long ll;
const ll mod = 1e9 + 7;
const ll inf = 9223372036854775807;
ll arr[2000], memo[2000][2000], bl[2000][2000], maxi, n, ans, dif, sum;
ll dp(ll x, ll y) {
ll depan = 0;
ll belakang = 0;
if (x == y) {
return arr[x];
}
if (x > y) {
return 0;
}
if (bl[x][y] == 1) {
return memo[x][y];
}
bl[x][y] = 1;
depan = arr[x] + min(dp(x + 2, y), dp(x + 1, y - 1));
belakang = arr[y] + min(dp(x, y - 2), dp(x + 1, y - 1));
memo[x][y] = max(depan, belakang);
return memo[x][y];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie();
cout.tie();
cin >> n;
for (ll i = 1; i <= n; i++) {
cin >> arr[i];
sum += arr[i];
}
ans = dp(1, n);
dif = sum - ans;
dif = ans - dif;
cout << dif << '\n';
}
| #include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
typedef long long ll;
const ll mod = 1e9 + 7;
const ll inf = 9223372036854775807;
ll arr[3000], memo[3000][3000], bl[3000][3000], maxi, n, ans, dif, sum;
ll dp(ll x, ll y) {
ll depan = 0;
ll belakang = 0;
if (x == y) {
return arr[x];
}
if (x > y) {
return 0;
}
if (bl[x][y] == 1) {
return memo[x][y];
}
bl[x][y] = 1;
depan = arr[x] + min(dp(x + 2, y), dp(x + 1, y - 1));
belakang = arr[y] + min(dp(x, y - 2), dp(x + 1, y - 1));
memo[x][y] = max(depan, belakang);
return memo[x][y];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie();
cout.tie();
cin >> n;
for (ll i = 1; i <= n; i++) {
cin >> arr[i];
sum += arr[i];
}
ans = dp(1, n);
dif = sum - ans;
dif = ans - dif;
cout << dif << '\n';
}
| replace | 9 | 10 | 9 | 10 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
// Macros for easier access
#define ll long long
#define endl '\n'
#define umap unordered_map
#define uset unordered_set
#define test \
ll cases; \
cin >> cases; \
for (ll testCase = 1; testCase <= cases; testCase++) // test cases
#define rep(i, begin, end, upd) \
for (ll i = begin; ((begin < end && upd > 0 && i < end) || \
(begin > end && upd < 0 && i >= end)); \
i += upd)
#define fill(name, val) memset(name, val, sizeof(name));
#define mop(a, op, b) (a % mod op b % mod) % mod
#define error(x) \
fixed << setprecision(x) // cout<<error(5)<<someDouble -> example - 3.14159
#define vll vector<ll>
#define vvll vector<vll>
#define pll pair<ll, ll>
#define get(a, n) \
for (ll i = 0; i < n; i++) \
cin >> a[i];
#define boost \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define debug(x) cerr << #x << " : " << (x) << endl
// Constants
#define MX 100001
#define mod 1000000007LL
#define inf 1000000000000000000LL
void init() {
boost;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
/* =======TL;DR=======
Author : zenolus
TreeNode (binary tree)[snippet key: tree]:
TreeNode(v) => create new node with val v
Graph (Adjacency List representation)[snippet key: graph]:
Graph(nodes, weighted = false, directed = false) => create a
graph with n nodes, specify if weighted addEdge(u, v, weight = 1) => add an
edge in graph, specify weight if necessary printEdges() => display the
adjacency list dijkstra(src) => single src all dest shortest distance.
returns a map <node, dist> Ot(ElogV) Os(V) use AUTO flowar() => All
pair shortest distance. returns 2D map <u, <v, d>> Ot(V^3) Os(V^2) use
AUTO MST() => Returns the edge list containing N - 1 edges forming the
Kruskal's MST Ot(ElogE) Os(V+E) use AUTO toposort() => returns dfs
based topological sorted vector for DAGs Ot(V+E) Os(V) use AUTO
SegTree (Segment Tree)[snippet key: segtree]:
SegTree(arrayLength, array, queryBase) => create the segment
tree update(index, value) => point update query(left, right) => query
segment operation on [l, r) Algorithms [snippet key: algo]: gcd(a, b) =>
returns gcd of a and b power(x, n) => return x raised to power n in O(log n)
time, if required, specify modulo fermatInv(x) => returns modular
multiplicative inverse of x with mod primeSieve(n = MX) => returns a vector
of all the primes till n factorize(n) => returns a vector of factors of n
modMult(a, b) => returns (a*b)%mod
idMatrix(matrix) => returns an identity matrix of dimensions
similar to passed matrix matrixMult(A, B) => returns (A.B)%mod
matrixExpo(A, n) => returns A power n % mod
nCr(n, r) => returns nCr value % mod using Fermat's theorem.
O(n + log p) nCrDP(n, r, p) => returns nCr % p for small n, r, p Ot(n*r),
Os(n) smallnCr(n, r, p) => returns nCr % p for small p using Lucas'
theorem. Ot(p^2 + log n base p), Os(p) linearRec(f, c, n) => returns
nth term of linear recurrence. F(n) = c1F(n-1) + c2F(n-2) + ... + ckF(n-k)
Ex: linearRec({1, 2, 3},
{3, 4, 5}, 8) => starting terms 1, 2, 3. c1 = 5, c2 = 5, c3 = 3. k = 3
*/
ll n;
ll a[3001];
ll dp[3001][3001][2];
ll solve(ll l, ll r, bool taro) {
if (l > r || l == n || r < 0)
return 0;
ll *x = &dp[l][r][taro];
if (*x != -1)
return *x;
ll op1 = taro * a[l] - (!taro) * a[l] + solve(l + 1, r, !taro);
ll op2 = taro * a[r] - (!taro) * a[r] + solve(l, r - 1, !taro);
return *x = taro ? max(op1, op2) : min(op1, op2);
}
int main() {
init();
cin >> n;
get(a, n);
fill(dp, -1);
cout << solve(0, n - 1, 1);
} | #include <bits/stdc++.h>
using namespace std;
// Macros for easier access
#define ll long long
#define endl '\n'
#define umap unordered_map
#define uset unordered_set
#define test \
ll cases; \
cin >> cases; \
for (ll testCase = 1; testCase <= cases; testCase++) // test cases
#define rep(i, begin, end, upd) \
for (ll i = begin; ((begin < end && upd > 0 && i < end) || \
(begin > end && upd < 0 && i >= end)); \
i += upd)
#define fill(name, val) memset(name, val, sizeof(name));
#define mop(a, op, b) (a % mod op b % mod) % mod
#define error(x) \
fixed << setprecision(x) // cout<<error(5)<<someDouble -> example - 3.14159
#define vll vector<ll>
#define vvll vector<vll>
#define pll pair<ll, ll>
#define get(a, n) \
for (ll i = 0; i < n; i++) \
cin >> a[i];
#define boost \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define debug(x) cerr << #x << " : " << (x) << endl
// Constants
#define MX 100001
#define mod 1000000007LL
#define inf 1000000000000000000LL
void init() {
boost;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
/* =======TL;DR=======
Author : zenolus
TreeNode (binary tree)[snippet key: tree]:
TreeNode(v) => create new node with val v
Graph (Adjacency List representation)[snippet key: graph]:
Graph(nodes, weighted = false, directed = false) => create a
graph with n nodes, specify if weighted addEdge(u, v, weight = 1) => add an
edge in graph, specify weight if necessary printEdges() => display the
adjacency list dijkstra(src) => single src all dest shortest distance.
returns a map <node, dist> Ot(ElogV) Os(V) use AUTO flowar() => All
pair shortest distance. returns 2D map <u, <v, d>> Ot(V^3) Os(V^2) use
AUTO MST() => Returns the edge list containing N - 1 edges forming the
Kruskal's MST Ot(ElogE) Os(V+E) use AUTO toposort() => returns dfs
based topological sorted vector for DAGs Ot(V+E) Os(V) use AUTO
SegTree (Segment Tree)[snippet key: segtree]:
SegTree(arrayLength, array, queryBase) => create the segment
tree update(index, value) => point update query(left, right) => query
segment operation on [l, r) Algorithms [snippet key: algo]: gcd(a, b) =>
returns gcd of a and b power(x, n) => return x raised to power n in O(log n)
time, if required, specify modulo fermatInv(x) => returns modular
multiplicative inverse of x with mod primeSieve(n = MX) => returns a vector
of all the primes till n factorize(n) => returns a vector of factors of n
modMult(a, b) => returns (a*b)%mod
idMatrix(matrix) => returns an identity matrix of dimensions
similar to passed matrix matrixMult(A, B) => returns (A.B)%mod
matrixExpo(A, n) => returns A power n % mod
nCr(n, r) => returns nCr value % mod using Fermat's theorem.
O(n + log p) nCrDP(n, r, p) => returns nCr % p for small n, r, p Ot(n*r),
Os(n) smallnCr(n, r, p) => returns nCr % p for small p using Lucas'
theorem. Ot(p^2 + log n base p), Os(p) linearRec(f, c, n) => returns
nth term of linear recurrence. F(n) = c1F(n-1) + c2F(n-2) + ... + ckF(n-k)
Ex: linearRec({1, 2, 3},
{3, 4, 5}, 8) => starting terms 1, 2, 3. c1 = 5, c2 = 5, c3 = 3. k = 3
*/
ll n;
ll a[3001];
ll dp[3001][3001][2];
ll solve(ll l, ll r, bool taro) {
if (l > r || l == n || r < 0)
return 0;
ll *x = &dp[l][r][taro];
if (*x != -1)
return *x;
ll op1 = taro * a[l] - (!taro) * a[l] + solve(l + 1, r, !taro);
ll op2 = taro * a[r] - (!taro) * a[r] + solve(l, r - 1, !taro);
return *x = taro ? max(op1, op2) : min(op1, op2);
}
int main() {
// init();
cin >> n;
get(a, n);
fill(dp, -1);
cout << solve(0, n - 1, 1);
} | replace | 89 | 90 | 89 | 90 | -11 | |
p03171 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define exrep(i, a, b) for (ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1000000007;
const ll INF = 1e16;
const ll MAX_N = 100010;
int main() {
ll n;
cin >> n;
vl a(n);
rep(i, n) { cin >> a[i]; }
vvl dp(
n,
vl(n + 1)); // dp[i][j] :
// 区間a[i]~a[j-1]から始めて二人が最適に行動したときのx-yの値
exrep(len, 1, n) {
for (ll i = 0; i + len <= n; i++) {
ll 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]);
}
}
}
out(dp[0][n]);
re0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define exrep(i, a, b) for (ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1000000007;
const ll INF = 1e16;
const ll MAX_N = 100010;
int main() {
ll n;
cin >> n;
vl a(n);
rep(i, n) { cin >> a[i]; }
vvl dp(
n + 1,
vl(n + 1)); // dp[i][j] :
// 区間a[i]~a[j-1]から始めて二人が最適に行動したときのx-yの値
exrep(len, 1, n) {
for (ll i = 0; i + len <= n; i++) {
ll 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]);
}
}
}
out(dp[0][n]);
re0;
} | replace | 36 | 37 | 36 | 37 | -11 | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#include <string.h>
using namespace std;
#define li long long int
#define mod 1000000000 + 7
li n, k;
li a[3005];
li dp[3005][3005];
li f(li i, li j) {
if (i == j) {
return a[i];
}
if (i == j - 1) {
return max(a[i], a[j]);
}
li op1 = a[i] + min(f(i + 2, j), f(i + 1, j - 1));
li op2 = a[j] + min(f(i + 1, j - 1), f(i, j - 2));
li ans = max(op1, op2);
return dp[i][j] = ans;
}
int main() {
cin >> n;
memset(dp, -1, sizeof(dp));
li i, s = 0;
for (i = 0; i < n; i++) {
cin >> a[i];
s += a[i];
}
cout << 2 * f(0, n - 1) - s;
return 0;
}
| #include <bits/stdc++.h>
#include <string.h>
using namespace std;
#define li long long int
#define mod 1000000000 + 7
li n, k;
li a[3005];
li dp[3005][3005];
li f(li i, li j) {
if (i == j) {
return a[i];
}
if (i == j - 1) {
return max(a[i], a[j]);
}
if (dp[i][j] != -1) {
return dp[i][j];
}
li op1 = a[i] + min(f(i + 2, j), f(i + 1, j - 1));
li op2 = a[j] + min(f(i + 1, j - 1), f(i, j - 2));
li ans = max(op1, op2);
return dp[i][j] = ans;
}
int main() {
cin >> n;
memset(dp, -1, sizeof(dp));
li i, s = 0;
for (i = 0; i < n; i++) {
cin >> a[i];
s += a[i];
}
cout << 2 * f(0, n - 1) - s;
return 0;
}
| insert | 14 | 14 | 14 | 17 | TLE | |
p03171 | C++ | Runtime Error | /*
!!!!!!
!!!!!!!!!!
!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!
!!!!!!!!!!!!
!!!!!!!!!!!!
!!!!!!!!!!!!
!!!!!!!!
!!!!!!!!!!
!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!! !!!!!
!!!!!!!!!!!!!!!!!!! !!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!! ! !!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !! !!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !! !!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!
!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !
!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!
!!!!!!!!!!!!
!!!!!!!!!!!!
!!!!!!!!!!!!
!!!!!!!!
!!!!!!
!!!!
*/
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define lli long long int
lli a[3001];
lli INF = 1e18;
lli dp[3001][3001][2];
lli f(lli i, lli j, lli c) {
// cout<<i<<" "<<j<<endl;
if (i == j) {
if (c == 1)
return (-a[i]);
else
return a[i];
}
if (dp[i][j][c] != INF)
return dp[i][j][c];
if (c == 0)
return dp[i][j][c] = (max(a[i] + f(i + 1, j, 1), a[j] + f(i, j - 1, 1)));
else
return dp[i][j][c] = (min(-a[i] + f(i + 1, j, 0), -a[j] + f(i, j - 1, 0)));
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
lli n;
cin >> n;
for (lli i = 0; i <= n; i++)
for (lli j = 0; j <= n; j++)
dp[i][j][0] = INF, dp[i][j][1] = INF;
for (lli i = 0; i < n; i++)
cin >> a[i];
cout << f(0, n - 1, 0);
} | /*
!!!!!!
!!!!!!!!!!
!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!
!!!!!!!!!!!!
!!!!!!!!!!!!
!!!!!!!!!!!!
!!!!!!!!
!!!!!!!!!!
!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!! !!!!!
!!!!!!!!!!!!!!!!!!! !!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!! ! !!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !! !!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !! !!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!
!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !
!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!
!!!!!!!!!!!!
!!!!!!!!!!!!
!!!!!!!!!!!!
!!!!!!!!
!!!!!!
!!!!
*/
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define lli long long int
lli a[3001];
lli INF = 1e18;
lli dp[3001][3001][2];
lli f(lli i, lli j, lli c) {
// cout<<i<<" "<<j<<endl;
if (i == j) {
if (c == 1)
return (-a[i]);
else
return a[i];
}
if (dp[i][j][c] != INF)
return dp[i][j][c];
if (c == 0)
return dp[i][j][c] = (max(a[i] + f(i + 1, j, 1), a[j] + f(i, j - 1, 1)));
else
return dp[i][j][c] = (min(-a[i] + f(i + 1, j, 0), -a[j] + f(i, j - 1, 0)));
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
lli n;
cin >> n;
for (lli i = 0; i <= n; i++)
for (lli j = 0; j <= n; j++)
dp[i][j][0] = INF, dp[i][j][1] = INF;
for (lli i = 0; i < n; i++)
cin >> a[i];
cout << f(0, n - 1, 0);
} | delete | 75 | 80 | 75 | 75 | -11 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
int N;
cin >> N;
vector<int> A(N);
rep(i, N) cin >> A[i];
vector<vector<ll>> dp(N + 100, vector<ll>(N + 100));
for (int width = 1; width <= N; width++) {
for (int l = 0; l < N; l++) {
int r = l + width;
if ((N & 1) ^ (width & 1)) {
// 後攻
dp[l][r] = min(dp[l + 1][r] - A[l], dp[l][r - 1] - A[r - 1]);
} else {
// 先行
dp[l][r] = max(dp[l + 1][r] + A[l], dp[l][r - 1] + A[r - 1]);
}
}
}
cout << dp[0][N] << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
int N;
cin >> N;
vector<int> A(N);
rep(i, N) cin >> A[i];
vector<vector<ll>> dp(N + 100, vector<ll>(N + 100));
for (int width = 1; width <= N; width++) {
for (int l = 0; l < N; l++) {
int r = l + width;
if (r > N)
continue;
if ((N & 1) ^ (width & 1)) {
// 後攻
dp[l][r] = min(dp[l + 1][r] - A[l], dp[l][r - 1] - A[r - 1]);
} else {
// 先行
dp[l][r] = max(dp[l + 1][r] + A[l], dp[l][r - 1] + A[r - 1]);
}
}
}
cout << dp[0][N] << endl;
return 0;
}
| insert | 15 | 15 | 15 | 17 | 0 | |
p03171 | C++ | Time Limit Exceeded | #define INPUT "./io/test.in"
#define OUTPUT "./io/test.out"
#ifdef NARUT_LOCAL
#include "local.hpp"
#include "reopen.hpp"
#else
#pragma GCC optimize("-O2", "-ftree-vectorize")
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <exception>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>
using namespace std;
#define DBG(...)
#define DBG2(...)
#define Debug(...)
#define TM_(...) template <class __VA_ARGS__>
#define ENABLE(...) TM_(T) static enable_if_t<__VA_ARGS__>
#define SFINAE(name, ...) \
TM_(, class = void) struct name##_s : false_type {}; \
TM_(T) struct name##_s<T, decltype(void(__VA_ARGS__))> : true_type {}; \
TM_(T) constexpr auto name = name##_s<T>::value;
namespace narut {
TM_(T) using TupSz = tuple_size<decay_t<T>>;
SFINAE(Iterable, begin(declval<T &>()))
SFINAE(IsTuple, TupSz<T>{})
SFINAE(Coutable, cout << declval<T &>())
SFINAE(Cinable, cin >> declval<T &>())
TM_(, class = void) struct Spc {
static const char val = '\n';
};
TM_(T) struct Spc<T, enable_if_t<Coutable<T>>> {
static const char val = ' ';
};
struct Ctx {
ENABLE(Coutable<T>) Write(T const &t) { cout << t; }
ENABLE(!Coutable<T> and Iterable<T>) Write(T const &t) {
auto it = begin(t), e = end(t);
if (it != e)
Write(*it++);
for (; it != e; Write(*it++))
cout << Spc<decltype(*it)>::val;
}
TM_(T, size_t... Is)
static void WriteTup(T const &t, index_sequence<Is...>) {
(void)(int[]){
0, ((Is == 0) ? 0 : ((cout << ' '), 0), Write(get<Is>(t)), 0)...};
}
ENABLE(!Coutable<T> and !Iterable<T> and IsTuple<T>) Write(T const &t) {
WriteTup(t, make_index_sequence<TupSz<T>::value>{});
}
ENABLE(Cinable<T>) Read(T &t) { cin >> t; }
ENABLE(!Cinable<T> and Iterable<T>) Read(T &t) {
for (auto &&x : t)
Read(x);
}
TM_(T, size_t... Is) static void ReadTup(T &t, index_sequence<Is...>) {
(void)(int[]){0, (Read(get<Is>(t)), 0)...};
}
ENABLE(!Cinable<T> and !Iterable<T> and IsTuple<T>) Read(T &t) {
ReadTup(t, make_index_sequence<TupSz<T>::value>{});
}
};
void Cout() { cout << '\n'; }
TM_(F, class... Ts) void Cout(F const &f, Ts const &...ts) {
Ctx::Write(f);
(void)(int[]){0, (cout << ' ', Ctx::Write(ts), 0)...};
cout << '\n';
}
TM_(T) T Cin() {
T t;
Ctx::Read(t);
return t;
}
TM_(T) void Cin(T &t) { Ctx::Read(t); }
TM_(T) void Cin(T &t, int n) {
t.resize(n);
Ctx::Read(t);
}
TM_(T) struct Big {
constexpr operator T() const {
T x = 0, mask = 0x3f;
for (int i = 0; i < sizeof(T); i++)
x |= mask << 8 * i;
return x;
}
};
} // namespace narut
using narut::Big;
using narut::Cin;
using narut::Cout;
using ll = long long;
using uint = unsigned int;
#endif
template <class T, uint D> struct MultiVec {
array<uint, D> dim = {};
vector<T> arr;
template <class... Ts> decltype(auto) operator()(Ts... ts) const {
return arr[index<0>(0, ts...)];
}
template <class... Ts> decltype(auto) operator()(Ts... ts) {
return arr[index<0>(0, ts...)];
}
void Fill(T const &t) { fill(arr.begin(), arr.end(), t); }
uint Size() const { return arr.size(); }
template <class... Ts> void Resize(Ts... ts) {
static_assert(sizeof...(ts) == D, "wrong reset sizes");
dim = {static_cast<uint>(ts)...};
arr.resize(index<0>(0, (ts - 1)...) + 1);
}
explicit MultiVec() = default;
template <class... Ts>
explicit MultiVec(Ts... ts)
: dim{static_cast<uint>(ts)...}, arr(index<0>(0, (ts - 1)...) + 1) {
static_assert(sizeof...(ts) == D, "dimensions don't match");
}
auto begin() { return arr.begin(); }
auto begin() const { return arr.begin(); }
auto end() { return arr.end(); }
auto end() const { return arr.end(); }
auto rbegin() { return arr.rbegin(); }
auto rbegin() const { return arr.rbegin(); }
auto rend() { return arr.rend(); }
auto rend() const { return arr.rend(); }
template <uint d, class C, class F, class... Ts>
uint index(C c, F f, Ts... ts) {
#ifdef _GLIBCXX_DEBUG
if (static_cast<uint>(f) >= dim[d])
throw out_of_range(to_string(d + 1) + "th dimension indexing " +
to_string(f) + " but size = " + to_string(dim[d]));
#endif
return index<d + 1>(static_cast<uint>(c) * dim[d] + static_cast<uint>(f),
ts...);
}
template <uint d> uint index(uint c) {
static_assert(d == D, "Wrong Indexing Dimension");
return c;
}
friend std::ostream &operator<<(std::ostream &os, MultiVec const &v) {
for (uint i = 0; i < v.Size(); i++) {
os << v.arr[i] << ' ';
uint tmp = i + 1;
for (uint d = D - 1; d > 0; d--) {
if (tmp % v.dim[d])
break;
os << '\n';
tmp /= v.dim[d];
}
}
return os;
}
};
template <class T, class... Ts> auto MakeMultiVec(Ts... ts) {
return MultiVec<T, sizeof...(Ts)>(ts...);
}
int n;
vector<int> A;
MultiVec<ll, 2> dp;
ll DP(int i, int j) {
if (i >= j)
return 0;
if (dp(i, j - 1) != -Big<ll>{})
return dp(i, j - 1);
ll f = A[i] - DP(i + 1, j);
ll s = A[j - 1] - DP(i, j - 1);
return max(f, s);
}
auto Run() {
cin >> n;
Cin(A, n);
dp.Resize(n, n);
dp.Fill(-Big<ll>{});
cout << DP(0, n);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(10);
Run();
}
| #define INPUT "./io/test.in"
#define OUTPUT "./io/test.out"
#ifdef NARUT_LOCAL
#include "local.hpp"
#include "reopen.hpp"
#else
#pragma GCC optimize("-O2", "-ftree-vectorize")
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <exception>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>
using namespace std;
#define DBG(...)
#define DBG2(...)
#define Debug(...)
#define TM_(...) template <class __VA_ARGS__>
#define ENABLE(...) TM_(T) static enable_if_t<__VA_ARGS__>
#define SFINAE(name, ...) \
TM_(, class = void) struct name##_s : false_type {}; \
TM_(T) struct name##_s<T, decltype(void(__VA_ARGS__))> : true_type {}; \
TM_(T) constexpr auto name = name##_s<T>::value;
namespace narut {
TM_(T) using TupSz = tuple_size<decay_t<T>>;
SFINAE(Iterable, begin(declval<T &>()))
SFINAE(IsTuple, TupSz<T>{})
SFINAE(Coutable, cout << declval<T &>())
SFINAE(Cinable, cin >> declval<T &>())
TM_(, class = void) struct Spc {
static const char val = '\n';
};
TM_(T) struct Spc<T, enable_if_t<Coutable<T>>> {
static const char val = ' ';
};
struct Ctx {
ENABLE(Coutable<T>) Write(T const &t) { cout << t; }
ENABLE(!Coutable<T> and Iterable<T>) Write(T const &t) {
auto it = begin(t), e = end(t);
if (it != e)
Write(*it++);
for (; it != e; Write(*it++))
cout << Spc<decltype(*it)>::val;
}
TM_(T, size_t... Is)
static void WriteTup(T const &t, index_sequence<Is...>) {
(void)(int[]){
0, ((Is == 0) ? 0 : ((cout << ' '), 0), Write(get<Is>(t)), 0)...};
}
ENABLE(!Coutable<T> and !Iterable<T> and IsTuple<T>) Write(T const &t) {
WriteTup(t, make_index_sequence<TupSz<T>::value>{});
}
ENABLE(Cinable<T>) Read(T &t) { cin >> t; }
ENABLE(!Cinable<T> and Iterable<T>) Read(T &t) {
for (auto &&x : t)
Read(x);
}
TM_(T, size_t... Is) static void ReadTup(T &t, index_sequence<Is...>) {
(void)(int[]){0, (Read(get<Is>(t)), 0)...};
}
ENABLE(!Cinable<T> and !Iterable<T> and IsTuple<T>) Read(T &t) {
ReadTup(t, make_index_sequence<TupSz<T>::value>{});
}
};
void Cout() { cout << '\n'; }
TM_(F, class... Ts) void Cout(F const &f, Ts const &...ts) {
Ctx::Write(f);
(void)(int[]){0, (cout << ' ', Ctx::Write(ts), 0)...};
cout << '\n';
}
TM_(T) T Cin() {
T t;
Ctx::Read(t);
return t;
}
TM_(T) void Cin(T &t) { Ctx::Read(t); }
TM_(T) void Cin(T &t, int n) {
t.resize(n);
Ctx::Read(t);
}
TM_(T) struct Big {
constexpr operator T() const {
T x = 0, mask = 0x3f;
for (int i = 0; i < sizeof(T); i++)
x |= mask << 8 * i;
return x;
}
};
} // namespace narut
using narut::Big;
using narut::Cin;
using narut::Cout;
using ll = long long;
using uint = unsigned int;
#endif
template <class T, uint D> struct MultiVec {
array<uint, D> dim = {};
vector<T> arr;
template <class... Ts> decltype(auto) operator()(Ts... ts) const {
return arr[index<0>(0, ts...)];
}
template <class... Ts> decltype(auto) operator()(Ts... ts) {
return arr[index<0>(0, ts...)];
}
void Fill(T const &t) { fill(arr.begin(), arr.end(), t); }
uint Size() const { return arr.size(); }
template <class... Ts> void Resize(Ts... ts) {
static_assert(sizeof...(ts) == D, "wrong reset sizes");
dim = {static_cast<uint>(ts)...};
arr.resize(index<0>(0, (ts - 1)...) + 1);
}
explicit MultiVec() = default;
template <class... Ts>
explicit MultiVec(Ts... ts)
: dim{static_cast<uint>(ts)...}, arr(index<0>(0, (ts - 1)...) + 1) {
static_assert(sizeof...(ts) == D, "dimensions don't match");
}
auto begin() { return arr.begin(); }
auto begin() const { return arr.begin(); }
auto end() { return arr.end(); }
auto end() const { return arr.end(); }
auto rbegin() { return arr.rbegin(); }
auto rbegin() const { return arr.rbegin(); }
auto rend() { return arr.rend(); }
auto rend() const { return arr.rend(); }
template <uint d, class C, class F, class... Ts>
uint index(C c, F f, Ts... ts) {
#ifdef _GLIBCXX_DEBUG
if (static_cast<uint>(f) >= dim[d])
throw out_of_range(to_string(d + 1) + "th dimension indexing " +
to_string(f) + " but size = " + to_string(dim[d]));
#endif
return index<d + 1>(static_cast<uint>(c) * dim[d] + static_cast<uint>(f),
ts...);
}
template <uint d> uint index(uint c) {
static_assert(d == D, "Wrong Indexing Dimension");
return c;
}
friend std::ostream &operator<<(std::ostream &os, MultiVec const &v) {
for (uint i = 0; i < v.Size(); i++) {
os << v.arr[i] << ' ';
uint tmp = i + 1;
for (uint d = D - 1; d > 0; d--) {
if (tmp % v.dim[d])
break;
os << '\n';
tmp /= v.dim[d];
}
}
return os;
}
};
template <class T, class... Ts> auto MakeMultiVec(Ts... ts) {
return MultiVec<T, sizeof...(Ts)>(ts...);
}
int n;
vector<int> A;
MultiVec<ll, 2> dp;
ll DP(int i, int j) {
if (i >= j)
return 0;
if (dp(i, j - 1) != -Big<ll>{})
return dp(i, j - 1);
ll f = A[i] - DP(i + 1, j);
ll s = A[j - 1] - DP(i, j - 1);
return dp(i, j - 1) = max(f, s);
}
auto Run() {
cin >> n;
Cin(A, n);
dp.Resize(n, n);
dp.Fill(-Big<ll>{});
cout << DP(0, n);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(10);
Run();
}
| replace | 208 | 209 | 208 | 209 | TLE | |
p03171 | C++ | Runtime Error | /*
*#*#*#* Author GaryMr *#*#*#*
########################################################################
是否可以简化问题:
抓住数据范围哪里小
dfs or bfs
是否可以化具体为抽象
字符串问题是否可以用数学模拟递归过程(取c[i]或不取来算结果)
二分?左闭右开?
long long?
逆向思维?
dp?
贪心?
树的直径:随便找一点再走到它最远的点A,再从A最远的点走到最远B的点。AB就是树的直径
匹配括号 左减右>=0
字符串赋值字符加单引号
double> printf("%lf")输出小数点6位,%.16lf输出后十六位
多组数据清空?
dp优化用g?
线段树:大小为n的8倍
线段树:左闭右开!
线段树:find: if(l>=b||r<=a) return 0x3f3f3f3f; if(r<=b&&l>=a) return
tree[k]; 线段树:满二叉树 循环跳出条件 代数设出每个值寻找关系?
优先队列:priority_queue<类型> q;
priority_queue<类,vector<类>, less<类> > pq1; // 使用递减
priority_queue<类,vector<类>, greater<类> > pq2; // 使用递增
判断括号,判断是否长度为偶数
1<<ceil(log2(double(n)))
*/
#include <bits/stdc++.h>
#define rb(a, b, c) for (int a = b; a <= c; a++)
#define rl(a, b, c) for (int a = b; a >= c; a--)
#define niv vector<int>
#define LL long long
#define IT iterator
#define FIR first
#define SEC second
using namespace std;
const int MAX = 0x3f3f3f3f;
typedef pair<int, int> mp;
typedef pair<mp, mp> superpair;
LL n, a[3003];
LL flag[3003][3003], MP[3003][3003];
LL find_maximum(int l, int r) {
if (l < 1 || r > n)
return 0;
if (flag[l][r]) {
return MP[l][r];
}
if (l == r)
return a[l];
flag[l][r] = 1;
return MP[l][r] =
max(a[l] - find_maximum(l + 1, r), a[r] - find_maximum(l, r - 1));
}
int main() {
freopen("out.out", "r", stdin);
scanf("%lld", &n);
rb(i, 1, n) scanf("%lld", &a[i]);
printf("%lld\n", find_maximum(1, n));
return 0;
} | /*
*#*#*#* Author GaryMr *#*#*#*
########################################################################
是否可以简化问题:
抓住数据范围哪里小
dfs or bfs
是否可以化具体为抽象
字符串问题是否可以用数学模拟递归过程(取c[i]或不取来算结果)
二分?左闭右开?
long long?
逆向思维?
dp?
贪心?
树的直径:随便找一点再走到它最远的点A,再从A最远的点走到最远B的点。AB就是树的直径
匹配括号 左减右>=0
字符串赋值字符加单引号
double> printf("%lf")输出小数点6位,%.16lf输出后十六位
多组数据清空?
dp优化用g?
线段树:大小为n的8倍
线段树:左闭右开!
线段树:find: if(l>=b||r<=a) return 0x3f3f3f3f; if(r<=b&&l>=a) return
tree[k]; 线段树:满二叉树 循环跳出条件 代数设出每个值寻找关系?
优先队列:priority_queue<类型> q;
priority_queue<类,vector<类>, less<类> > pq1; // 使用递减
priority_queue<类,vector<类>, greater<类> > pq2; // 使用递增
判断括号,判断是否长度为偶数
1<<ceil(log2(double(n)))
*/
#include <bits/stdc++.h>
#define rb(a, b, c) for (int a = b; a <= c; a++)
#define rl(a, b, c) for (int a = b; a >= c; a--)
#define niv vector<int>
#define LL long long
#define IT iterator
#define FIR first
#define SEC second
using namespace std;
const int MAX = 0x3f3f3f3f;
typedef pair<int, int> mp;
typedef pair<mp, mp> superpair;
LL n, a[3003];
LL flag[3003][3003], MP[3003][3003];
LL find_maximum(int l, int r) {
if (l < 1 || r > n)
return 0;
if (flag[l][r]) {
return MP[l][r];
}
if (l == r)
return a[l];
flag[l][r] = 1;
return MP[l][r] =
max(a[l] - find_maximum(l + 1, r), a[r] - find_maximum(l, r - 1));
}
int main() {
// freopen("out.out","r",stdin);
scanf("%lld", &n);
rb(i, 1, n) scanf("%lld", &a[i]);
printf("%lld\n", find_maximum(1, n));
return 0;
} | replace | 56 | 57 | 56 | 57 | -11 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double D;
typedef pair<ll, ll> P;
#define M 1000000007
#define F first
#define S second
#define PB push_back
#define INF 100000000000000000
ll n, a[3005], dp[3005][3005][2];
int main(void) {
scanf("%lld", &n);
for (int i = 1; i <= n; i++)
scanf("%lld", a + i);
for (int k = 0; k < n; k++) {
for (int i = 1; i <= n; i++) {
int l = i, r = i + k;
dp[l][r][0] = max(dp[l + 1][r][1] + a[l], dp[l][r - 1][1] + a[r]);
dp[l][r][1] = min(dp[l + 1][r][0] - a[l], dp[l][r - 1][0] - a[r]);
}
}
printf("%lld\n", dp[1][n][0]);
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double D;
typedef pair<ll, ll> P;
#define M 1000000007
#define F first
#define S second
#define PB push_back
#define INF 100000000000000000
ll n, a[3005], dp[3005][3005][2];
int main(void) {
scanf("%lld", &n);
for (int i = 1; i <= n; i++)
scanf("%lld", a + i);
for (int k = 0; k < n; k++) {
for (int i = 1; i + k <= n; i++) {
int l = i, r = i + k;
dp[l][r][0] = max(dp[l + 1][r][1] + a[l], dp[l][r - 1][1] + a[r]);
dp[l][r][1] = min(dp[l + 1][r][0] - a[l], dp[l][r - 1][0] - a[r]);
}
}
printf("%lld\n", dp[1][n][0]);
}
| replace | 16 | 17 | 16 | 17 | -11 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define y1 as214
#define ii pair<int, int>
#define iii pair<int, ii>
#define iv pair<ii, ii>
#define fi first
#define se second
#define fr front()
#define pb push_back
#define FOR(i, x, n) for (int i = x; i <= n; ++i)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FORD(i, x, n) for (int i = x; i >= n; --i)
#define ll long long
#define oo INT_MAX
const int N = 1e3 + 5;
ll n, res;
ll a[N];
ll dp[N][N];
/// dp[i][j] là số điểm lớn nhất của người chơi thứ 1 có thẻ nhận được nếu khởi
/// đầu trong đoạn [i , j]
int main() {
// freopen("L.inp","r",stdin);
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
FOR(i, 1, n) {
cin >> a[i];
res += a[i];
}
FOR(i, 1, n)
dp[i][i] = a[i];
FOR(i, 1, n - 1)
dp[i][i + 1] = max(a[i], a[i + 1]);
FOR(r, 3, n)
FORD(l, r - 2, 1)
dp[l][r] = max(min(dp[l + 2][r], dp[l + 1][r - 1]) + a[l],
min(dp[l][r - 2], dp[l + 1][r - 1]) + a[r]);
cout << dp[1][n] - (res - dp[1][n]) << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define y1 as214
#define ii pair<int, int>
#define iii pair<int, ii>
#define iv pair<ii, ii>
#define fi first
#define se second
#define fr front()
#define pb push_back
#define FOR(i, x, n) for (int i = x; i <= n; ++i)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FORD(i, x, n) for (int i = x; i >= n; --i)
#define ll long long
#define oo INT_MAX
const int N = 3e3 + 5;
ll n, res;
ll a[N];
ll dp[N][N];
/// dp[i][j] là số điểm lớn nhất của người chơi thứ 1 có thẻ nhận được nếu khởi
/// đầu trong đoạn [i , j]
int main() {
// freopen("L.inp","r",stdin);
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
FOR(i, 1, n) {
cin >> a[i];
res += a[i];
}
FOR(i, 1, n)
dp[i][i] = a[i];
FOR(i, 1, n - 1)
dp[i][i + 1] = max(a[i], a[i + 1]);
FOR(r, 3, n)
FORD(l, r - 2, 1)
dp[l][r] = max(min(dp[l + 2][r], dp[l + 1][r - 1]) + a[l],
min(dp[l][r - 2], dp[l + 1][r - 1]) + a[r]);
cout << dp[1][n] - (res - dp[1][n]) << endl;
}
| replace | 20 | 21 | 20 | 21 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <string.h>
using namespace std;
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define endl "\n"
#define Max(x, y, z) max(x, max(y, z))
#define Min(x, y, z) min(x, min(y, z))
#define fr(i, s, e) for (i = s; i < e; i++)
#define rf(i, s, e) for (i = s - 1; i >= e; i--)
#define pb push_back
#define eb emblace_back
#define mp make_pair
#define ff first
#define ss second
#define int long long
#define ll long long
#define trace1(x) cerr << #x << ": " << x << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z) \
cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": " \
<< z << endl
#define trace4(a, b, c, d) \
cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e) \
cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define trace6(a, b, c, d, e, f) \
cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " \
<< #f << ": " << f << endl
#define vl vector<long long>
#define inf (1LL << 51)
#define vi vector<int>
#define vii vector<vector<int>>
#define vll vector<vector<long long>>
#define vpi vector<pair<int, int>>
#define vpl vector<pair<ll, ll>>
#define viii vector<vector<vector<int>>>
typedef pair<pair<int, int>, int> P;
#define cd complex<double>
#define PI 4 * atan(1)
#define mod 1000000007
#define pi 3.14159265358979323846 // 20 decimals
#define ld long double
#define all(c) (c).begin(), (c).end()
#define N 3000
int n, k;
int a[100];
int dp[3001][3001][2];
bool vis[3001][3001][2];
int rec(int be, int en, int ch) {
if (be > en)
return 0;
if (vis[be][en][ch] == 0) {
vis[be][en][ch] = 1;
int ans = 0;
if (ch) {
ans = -a[be] + rec(be + 1, en, !ch);
ans = min(ans, -a[en] + rec(be, en - 1, !ch));
} else {
ans = a[be] + rec(be + 1, en, !ch);
ans = max(ans, a[en] + rec(be, en - 1, !ch));
}
dp[be][en][ch] = ans;
return ans;
} else
return dp[be][en][ch];
}
int32_t main() {
IOS;
memset(dp, -1, sizeof(dp));
memset(vis, 0, sizeof(vis));
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
cout << rec(0, n - 1, 0);
return 0;
} | #include <bits/stdc++.h>
#include <string.h>
using namespace std;
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define endl "\n"
#define Max(x, y, z) max(x, max(y, z))
#define Min(x, y, z) min(x, min(y, z))
#define fr(i, s, e) for (i = s; i < e; i++)
#define rf(i, s, e) for (i = s - 1; i >= e; i--)
#define pb push_back
#define eb emblace_back
#define mp make_pair
#define ff first
#define ss second
#define int long long
#define ll long long
#define trace1(x) cerr << #x << ": " << x << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z) \
cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": " \
<< z << endl
#define trace4(a, b, c, d) \
cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e) \
cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define trace6(a, b, c, d, e, f) \
cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " \
<< #f << ": " << f << endl
#define vl vector<long long>
#define inf (1LL << 51)
#define vi vector<int>
#define vii vector<vector<int>>
#define vll vector<vector<long long>>
#define vpi vector<pair<int, int>>
#define vpl vector<pair<ll, ll>>
#define viii vector<vector<vector<int>>>
typedef pair<pair<int, int>, int> P;
#define cd complex<double>
#define PI 4 * atan(1)
#define mod 1000000007
#define pi 3.14159265358979323846 // 20 decimals
#define ld long double
#define all(c) (c).begin(), (c).end()
#define N 3000
int n, k;
int a[N];
int dp[3001][3001][2];
bool vis[3001][3001][2];
int rec(int be, int en, int ch) {
if (be > en)
return 0;
if (vis[be][en][ch] == 0) {
vis[be][en][ch] = 1;
int ans = 0;
if (ch) {
ans = -a[be] + rec(be + 1, en, !ch);
ans = min(ans, -a[en] + rec(be, en - 1, !ch));
} else {
ans = a[be] + rec(be + 1, en, !ch);
ans = max(ans, a[en] + rec(be, en - 1, !ch));
}
dp[be][en][ch] = ans;
return ans;
} else
return dp[be][en][ch];
}
int32_t main() {
IOS;
memset(dp, -1, sizeof(dp));
memset(vis, 0, sizeof(vis));
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
cout << rec(0, n - 1, 0);
return 0;
} | replace | 51 | 52 | 51 | 52 | -11 | |
p03171 | C++ | Runtime Error | //
// ROIGold.cpp
// Main calisma
//
// Created by Rakhman on 05/02/2019.
// Copyright © 2019 Rakhman. All rights reserved.
//
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
#define ios ios_base::sync_with_stdio(0), cout.tie(0), cin.tie(0);
#define S second
#define F first
#define pb push_back
#define nl '\n'
#define NL cout << '\n';
#define EX exit(0)
#define all(s) s.begin(), s.end()
#define no_answer return cout << "NO", 0;
#define FOR(i, start, finish, k) for (int i = start; i <= finish; i += k)
const int MXN = 4e5 + 200;
const long long MNN = 4e2 + 200;
const long long MOD = 1e9 + 7;
const long long INF = 1e13;
const int OO = 1e9 + 500;
typedef long long llong;
typedef unsigned long long ullong;
using namespace std;
int n, k, a[MXN];
llong dp[MNN][MNN][2];
llong get(llong action, llong l, llong r) {
if (dp[l][r][action] != -INF)
return dp[l][r][action];
if (l == r) {
if (action == 1)
return a[l];
else
return -a[l];
}
if (action == 1) {
llong choice1 = get(0, l + 1, r) + a[l];
llong choice2 = get(0, l, r - 1) + a[r];
return dp[l][r][1] = max(choice1, choice2);
} else {
llong choice1 = get(1, l + 1, r) - a[l];
llong choice2 = get(1, l, r - 1) - a[r];
return dp[l][r][0] = min(choice1, choice2);
}
}
int main() {
ios;
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j++) {
dp[i][j][0] = dp[i][j][1] = -INF;
}
}
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
cout << get(1, 1, n);
}
| //
// ROIGold.cpp
// Main calisma
//
// Created by Rakhman on 05/02/2019.
// Copyright © 2019 Rakhman. All rights reserved.
//
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
#define ios ios_base::sync_with_stdio(0), cout.tie(0), cin.tie(0);
#define S second
#define F first
#define pb push_back
#define nl '\n'
#define NL cout << '\n';
#define EX exit(0)
#define all(s) s.begin(), s.end()
#define no_answer return cout << "NO", 0;
#define FOR(i, start, finish, k) for (int i = start; i <= finish; i += k)
const int MXN = 4e5 + 200;
const long long MNN = 3e3 + 200;
const long long MOD = 1e9 + 7;
const long long INF = 1e13;
const int OO = 1e9 + 500;
typedef long long llong;
typedef unsigned long long ullong;
using namespace std;
int n, k, a[MXN];
llong dp[MNN][MNN][2];
llong get(llong action, llong l, llong r) {
if (dp[l][r][action] != -INF)
return dp[l][r][action];
if (l == r) {
if (action == 1)
return a[l];
else
return -a[l];
}
if (action == 1) {
llong choice1 = get(0, l + 1, r) + a[l];
llong choice2 = get(0, l, r - 1) + a[r];
return dp[l][r][1] = max(choice1, choice2);
} else {
llong choice1 = get(1, l + 1, r) - a[l];
llong choice2 = get(1, l, r - 1) - a[r];
return dp[l][r][0] = min(choice1, choice2);
}
}
int main() {
ios;
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j++) {
dp[i][j][0] = dp[i][j][1] = -INF;
}
}
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
cout << get(1, 1, n);
}
| replace | 43 | 44 | 43 | 44 | 0 | |
p03171 | C++ | Time Limit Exceeded | // Rishabh Agarwal
#include <bits/stdc++.h>
#define F first
#define S second
#define MAX 10000003
using namespace std;
typedef long long int ll;
const ll mod = 1e9 + 7;
const ll INF = 1e18;
long double PI = 3.1415926;
ll power(ll a, ll b) {
if (b == 0) {
return 1;
}
ll temp = power(a, b / 2) % mod;
if (b % 2 == 0) {
return (temp * temp) % mod;
} else {
return ((a * temp) % mod * temp) % mod;
}
}
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
vector<ll> v;
vector<ll>::iterator it;
map<ll, ll> mp;
void primeFactors(ll n) {
while (n % 2 == 0) {
mp[2]++;
n = n / 2;
}
for (ll i = 3; i <= sqrt(n); i = i + 2) {
while (n % i == 0) {
mp[i]++;
n = n / i;
}
}
if (n > 2) {
mp[n]++;
}
}
ll dp[3000][3000];
ll dp1(ll s, ll e, ll a[]) {
if (s > e) {
return 0;
} else if (dp[s][e] != -1) {
return dp[s][e];
} else {
return max(a[s] - dp1(s + 1, e, a), a[e] - dp1(s, e - 1, a));
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll t;
// cin>>t;
t = 1;
// ll c=1;
while (t--) {
ll n;
cin >> n;
ll a[n];
for (ll x = 0; x < n; x++) {
cin >> a[x];
}
memset(dp, -1, sizeof(dp));
cout << dp1(0, n - 1, a) << "\n";
mp.clear();
v.clear();
// cout<<"Case #"<<c<<": "<<ans<<"\n";
// c++;
}
return 0;
} | // Rishabh Agarwal
#include <bits/stdc++.h>
#define F first
#define S second
#define MAX 10000003
using namespace std;
typedef long long int ll;
const ll mod = 1e9 + 7;
const ll INF = 1e18;
long double PI = 3.1415926;
ll power(ll a, ll b) {
if (b == 0) {
return 1;
}
ll temp = power(a, b / 2) % mod;
if (b % 2 == 0) {
return (temp * temp) % mod;
} else {
return ((a * temp) % mod * temp) % mod;
}
}
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
vector<ll> v;
vector<ll>::iterator it;
map<ll, ll> mp;
void primeFactors(ll n) {
while (n % 2 == 0) {
mp[2]++;
n = n / 2;
}
for (ll i = 3; i <= sqrt(n); i = i + 2) {
while (n % i == 0) {
mp[i]++;
n = n / i;
}
}
if (n > 2) {
mp[n]++;
}
}
ll dp[3000][3000];
ll dp1(ll s, ll e, ll a[]) {
if (s > e) {
return 0;
} else if (dp[s][e] != -1) {
return dp[s][e];
} else {
dp[s][e] = max(a[s] - dp1(s + 1, e, a), a[e] - dp1(s, e - 1, a));
return dp[s][e];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll t;
// cin>>t;
t = 1;
// ll c=1;
while (t--) {
ll n;
cin >> n;
ll a[n];
for (ll x = 0; x < n; x++) {
cin >> a[x];
}
memset(dp, -1, sizeof(dp));
cout << dp1(0, n - 1, a) << "\n";
mp.clear();
v.clear();
// cout<<"Case #"<<c<<": "<<ans<<"\n";
// c++;
}
return 0;
} | replace | 58 | 59 | 58 | 60 | TLE | |
p03171 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
// #include <map>
#include <cmath>
#include <cstring>
#include <iomanip>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long LL;
typedef long double LD;
typedef unsigned int uii;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
typedef vector<int> vii;
const int maxn = 3333;
LL d[maxn][maxn], vis[maxn][maxn], a[maxn];
LL dp(int L, int R, int h) {
if (vis[L][R])
return d[L][R];
if (L == R) {
return d[L][R] = h * a[L];
}
if (h == 1) {
d[L][R] = max(dp(L + 1, R, -h) + h * a[L], dp(L, R - 1, -h) + h * a[R]);
} else {
d[L][R] = min(dp(L + 1, R, -h) + h * a[L], dp(L, R - 1, -h) + h * a[R]);
}
return d[L][R];
}
int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
dp(1, n, 1);
cout << d[1][n] << endl;
return 0;
} | #include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
// #include <map>
#include <cmath>
#include <cstring>
#include <iomanip>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long LL;
typedef long double LD;
typedef unsigned int uii;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
typedef vector<int> vii;
const int maxn = 3333;
LL d[maxn][maxn], vis[maxn][maxn], a[maxn];
LL dp(int L, int R, int h) {
if (vis[L][R])
return d[L][R];
vis[L][R] = 1;
if (L == R) {
return d[L][R] = h * a[L];
}
if (h == 1) {
d[L][R] = max(dp(L + 1, R, -h) + h * a[L], dp(L, R - 1, -h) + h * a[R]);
} else {
d[L][R] = min(dp(L + 1, R, -h) + h * a[L], dp(L, R - 1, -h) + h * a[R]);
}
return d[L][R];
}
int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
dp(1, n, 1);
cout << d[1][n] << endl;
return 0;
} | insert | 24 | 24 | 24 | 25 | TLE | |
p03171 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#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 <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
constexpr int maxn = 3000;
constexpr ll inf = (ll)1000000000000000;
ll dp[maxn + 1][maxn + 1][2];
int main() {
int n;
cin >> n;
vector<int> v(n);
for (int &e : v)
cin >> e;
for (int i = 0; i <= n; i++)
dp[i][i][0] = dp[i][i][1] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < n; j++) {
int b = j, e = j + i;
for (int k = 0; k < 2; k++) {
ll &ans = dp[b][e][k];
ans = (k == 0) ? -inf : inf;
if (k == 0) {
ans = max(ans, v[b] + dp[b + 1][e][!k]);
ans = max(ans, v[e - 1] + dp[b][e - 1][!k]);
} else {
ans = min(ans, dp[b + 1][e][!k] - v[b]);
ans = min(ans, dp[b][e - 1][!k] - v[e - 1]);
}
}
}
}
cout << dp[0][n][0] << endl;
return 0;
}
| #include <algorithm>
#include <cmath>
#include <cstdio>
#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 <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
constexpr int maxn = 3000;
constexpr ll inf = (ll)1000000000000000;
ll dp[maxn + 1][maxn + 1][2];
int main() {
int n;
cin >> n;
vector<int> v(n);
for (int &e : v)
cin >> e;
for (int i = 0; i <= n; i++)
dp[i][i][0] = dp[i][i][1] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 0; j + i <= n; j++) {
int b = j, e = j + i;
for (int k = 0; k < 2; k++) {
ll &ans = dp[b][e][k];
ans = (k == 0) ? -inf : inf;
if (k == 0) {
ans = max(ans, v[b] + dp[b + 1][e][!k]);
ans = max(ans, v[e - 1] + dp[b][e - 1][!k]);
} else {
ans = min(ans, dp[b + 1][e][!k] - v[b]);
ans = min(ans, dp[b][e - 1][!k] - v[e - 1]);
}
}
}
}
cout << dp[0][n][0] << endl;
return 0;
}
| replace | 40 | 41 | 40 | 41 | -11 | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
ll n;
cin >> n;
ll a[n];
ll sum = 0;
ll dp[3000][3000][2];
for (ll i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
dp[i][i][0] = a[i];
dp[i][i][1] = 0;
}
for (ll rml = 1; rml < n; rml++) {
for (ll L = 0; L < rml + L; L++) {
int R = rml + L;
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]);
}
}
ll X = dp[0][n - 1][0];
cout << 2 * X - sum;
}
| #include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
ll n;
cin >> n;
ll a[n];
ll sum = 0;
ll dp[3000][3000][2];
for (ll i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
dp[i][i][0] = a[i];
dp[i][i][1] = 0;
}
for (ll rml = 1; rml < n; rml++) {
for (ll L = 0; L + rml < n; L++) {
int R = rml + L;
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]);
}
}
ll X = dp[0][n - 1][0];
cout << 2 * X - sum;
}
| replace | 16 | 17 | 16 | 17 | TLE | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
int solve(vector<int> &coins, int l, int r, vector<vector<int>> &dp) {
if (l > r)
return 0;
if (dp[l][r] != -1)
return dp[l][r];
int a1 = dp[l + 2][r] != -1 ? dp[l + 2][r] : solve(coins, l + 2, r, dp);
int a2 = dp[l + 1][r - 1] != -1 ? dp[l + 1][r - 1]
: solve(coins, l + 1, r - 1, dp);
int a = coins[l] + min(a1, a2);
int b1 = dp[l + 1][r - 1] != -1 ? dp[l + 1][r - 1]
: solve(coins, l + 1, r - 1, dp);
int b2 = dp[l][r - 2] != -1 ? dp[l][r - 2] : solve(coins, l, r - 2, dp);
int b = coins[r] + min(b1, b2);
return dp[l][r] = max(a, b);
}
signed main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int N;
cin >> N;
int sum = 0;
vector<int> coins(N);
for (int i = 0; i < N; i++) {
cin >> coins[i];
sum += coins[i];
}
vector<vector<int>> dp(N + 1, vector<int>(N + 1, -1));
int X = solve(coins, 0, N - 1, dp);
int Y = sum - X;
cout << X - Y << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
int solve(vector<int> &coins, int l, int r, vector<vector<int>> &dp) {
if (l > r)
return 0;
if (dp[l][r] != -1)
return dp[l][r];
return dp[l][r] = max(coins[l] + min(solve(coins, l + 2, r, dp),
solve(coins, l + 1, r - 1, dp)),
coins[r] + min(solve(coins, l + 1, r - 1, dp),
solve(coins, l, r - 2, dp)));
}
signed main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int N;
cin >> N;
int sum = 0;
vector<int> coins(N);
for (int i = 0; i < N; i++) {
cin >> coins[i];
sum += coins[i];
}
vector<vector<int>> dp(N + 1, vector<int>(N + 1, -1));
int X = solve(coins, 0, N - 1, dp);
int Y = sum - X;
cout << X - Y << endl;
return 0;
}
| replace | 10 | 23 | 10 | 14 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define endl '\n'
#define PI acos(-1)
typedef long long ll;
// Table is the complete reverse of dp
// Always think of the pattern (LCM, GCD)
// Be careful of unsigned
// double epsilon = FLT_EPSILON;
// Think of a solution that gets TLE and optimize it
int n, *x;
ll **dp[2], flag;
ll solve(bool turn = 0, int i = 0, int j = n - 1) {
if (i == j)
return dp[turn][i][j] = (turn ? -1 : 1) * x[i];
ll &ret = dp[turn][i][j];
if (ret != flag)
return ret;
if (!turn)
return ret = max(x[i] + solve(!turn, i + 1, j),
x[j] + solve(!turn, i, j - 1));
else
return ret = min(-x[i] + solve(!turn, i + 1, j),
-x[j] + solve(!turn, i, j - 1));
}
int main() {
cin.tie(nullptr);
cout.tie(nullptr);
ios_base::sync_with_stdio(false);
cin >> n;
memset(&flag, -127, n * 8);
x = new int[n];
dp[0] = new ll *[n];
dp[1] = new ll *[n];
for (int i = 0; i < n; i++) {
cin >> x[i];
dp[0][i] = new ll[n];
dp[1][i] = new ll[n];
memset(dp[0][i], -127, n * 8);
memset(dp[1][i], -127, n * 8);
}
cout << solve();
}
| #include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define endl '\n'
#define PI acos(-1)
typedef long long ll;
// Table is the complete reverse of dp
// Always think of the pattern (LCM, GCD)
// Be careful of unsigned
// double epsilon = FLT_EPSILON;
// Think of a solution that gets TLE and optimize it
int n, *x;
ll **dp[2], flag;
ll solve(bool turn = 0, int i = 0, int j = n - 1) {
if (i == j)
return dp[turn][i][j] = (turn ? -1 : 1) * x[i];
ll &ret = dp[turn][i][j];
if (ret != flag)
return ret;
if (!turn)
return ret = max(x[i] + solve(!turn, i + 1, j),
x[j] + solve(!turn, i, j - 1));
else
return ret = min(-x[i] + solve(!turn, i + 1, j),
-x[j] + solve(!turn, i, j - 1));
}
int main() {
cin.tie(nullptr);
cout.tie(nullptr);
ios_base::sync_with_stdio(false);
cin >> n;
memset(&flag, -127, 8);
x = new int[n];
dp[0] = new ll *[n];
dp[1] = new ll *[n];
for (int i = 0; i < n; i++) {
cin >> x[i];
dp[0][i] = new ll[n];
dp[1][i] = new ll[n];
memset(dp[0][i], -127, n * 8);
memset(dp[1][i], -127, n * 8);
}
cout << solve();
}
| replace | 32 | 33 | 32 | 33 | 0 | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, s, t) for (int i = (s); i < (t); i++)
int N;
ll A[3005], DP[3005][3005];
ll sol = 0;
// 0 - Taro's turn
// 1 - Jiro's turn
ll rek(int l, int r, int turn) {
if (l > r)
return 0;
if (l == r) {
return DP[l][r] = A[l] * (1 - turn);
}
ll sol1 = rek(l + 1, r, 1 - turn) + A[l];
ll sol2 = rek(l, r - 1, 1 - turn) + A[r];
if (turn == 0) {
DP[l][r] = max(sol1, sol2);
} else {
DP[l][r] = min(sol1 - A[l], sol2 - A[r]);
}
return DP[l][r];
}
int main() {
scanf("%d", &N);
ll sum = 0;
REP(i, 0, N) {
scanf("%lld", &A[i]);
sum += A[i];
}
ll x = rek(0, N - 1, 0);
printf("%lld\n", x - (sum - x));
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, s, t) for (int i = (s); i < (t); i++)
int N;
ll A[3005], DP[3005][3005];
ll sol = 0;
// 0 - Taro's turn
// 1 - Jiro's turn
ll rek(int l, int r, int turn) {
if (l > r)
return 0;
if (l == r) {
return DP[l][r] = A[l] * (1 - turn);
}
if (DP[l][r] != 0)
return DP[l][r];
ll sol1 = rek(l + 1, r, 1 - turn) + A[l];
ll sol2 = rek(l, r - 1, 1 - turn) + A[r];
if (turn == 0) {
DP[l][r] = max(sol1, sol2);
} else {
DP[l][r] = min(sol1 - A[l], sol2 - A[r]);
}
return DP[l][r];
}
int main() {
scanf("%d", &N);
ll sum = 0;
REP(i, 0, N) {
scanf("%lld", &A[i]);
sum += A[i];
}
ll x = rek(0, N - 1, 0);
printf("%lld\n", x - (sum - x));
return 0;
}
| insert | 19 | 19 | 19 | 21 | TLE | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
inline void fileio(const char *in, const char *out) {
freopen(in, "r", stdin);
freopen(out, "w", stdout);
}
#define TD typedef
#define forx(i, n) for (int i = 0; i < (n); ++i)
#define forxr(i, n) for (int i = (n)-1; i >= 0; --i)
TD long long ll;
TD long double ld;
TD pair<int, int> pii;
TD pair<ll, ll> pll;
TD vector<int> VI;
TD vector<bool> VB;
TD vector<ll> VL;
const char nl = '\n';
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;
const ld EPS = 1e-9, PI = acos(-1);
#define ff first
#define ss second
#define all(c) c.begin(), c.end()
const int N = 3005;
bool vis[N][N];
ll dp[N][N];
ll a[N], n;
ll solve(int l, int r) {
if (l == r)
return 0;
if (!vis[l][r]) {
dp[l][r] = max(a[r - 1] - solve(l, r - 1), a[l] - solve(l + 1, r));
}
return dp[l][r];
}
int main() { // Emily <3
atexit([]() { cerr << "Time: " << (ld)clock() / CLOCKS_PER_SEC << nl; });
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << fixed << setprecision(12);
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
cout << solve(0, n) << nl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
inline void fileio(const char *in, const char *out) {
freopen(in, "r", stdin);
freopen(out, "w", stdout);
}
#define TD typedef
#define forx(i, n) for (int i = 0; i < (n); ++i)
#define forxr(i, n) for (int i = (n)-1; i >= 0; --i)
TD long long ll;
TD long double ld;
TD pair<int, int> pii;
TD pair<ll, ll> pll;
TD vector<int> VI;
TD vector<bool> VB;
TD vector<ll> VL;
const char nl = '\n';
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;
const ld EPS = 1e-9, PI = acos(-1);
#define ff first
#define ss second
#define all(c) c.begin(), c.end()
const int N = 3005;
bool vis[N][N];
ll dp[N][N];
ll a[N], n;
ll solve(int l, int r) {
if (l == r)
return 0;
if (!vis[l][r]) {
dp[l][r] = max(a[r - 1] - solve(l, r - 1), a[l] - solve(l + 1, r));
vis[l][r] = 1;
}
return dp[l][r];
}
int main() { // Emily <3
atexit([]() { cerr << "Time: " << (ld)clock() / CLOCKS_PER_SEC << nl; });
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << fixed << setprecision(12);
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
cout << solve(0, n) << nl;
return 0;
}
| insert | 34 | 34 | 34 | 35 | TLE | |
p03171 | C++ | Runtime Error | #include "bits/stdc++.h"
#define fastread \
std::ios::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define endl '\n'
#define pb push_back
#define mp make_pair
#define ceel(x, y) (x + y - 1) / y
#define print(a) \
for (auto x : a) \
cout << x << ' '; \
cout << endl;
#define printM(b) \
for (auto y : b) { \
print(y); \
}
#define printP(p) cout << p.first << ' ' << p.second << endl;
#define inp(ar, l, r) \
for (int i = l; i < r; ++i) \
cin >> ar[i];
#define inpar(ar) \
for (int i = 0; i < n; ++i) \
cin >> ar[i];
#define debug(ar, n) \
for (int i = 0; i < n; ++i) \
cout << ar[i] << ' '; \
cout << endl;
#define debugar(ar) \
for (int i = 0; i < n; ++i) \
cout << ar[i] << ' '; \
cout << endl;
#define qwer cout << endl;
#define MOD 1000000007
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef unsigned long long ull;
#define pll pair<ll, ll>
const int N = 3001;
ll dp[2][N + 1][N + 1];
ll ar[N + 1], n;
ll calc(int left, int right, bool player, ll score1, ll score2) {
if (left > right)
return 0;
if (dp[player][left][right] != -1)
return dp[player][left][right];
if (player % 2 == 0) {
ll temp1 = calc(left + 1, right, player ^ 1, score1 + ar[left], score2);
ll temp2 = calc(left, right - 1, player ^ 1, score1 + ar[right], score2);
dp[player][left][right] = max(ar[left] + temp1, ar[right] + temp2);
} else {
ll temp1 = calc(left + 1, right, player ^ 1, score1, score2 + ar[left]);
ll temp2 = calc(left, right - 1, player ^ 1, score1, score2 + ar[right]);
dp[player][left][right] = min(temp1 - ar[left], temp2 - ar[right]);
}
return dp[player][left][right];
}
void solve() {
cin >> n;
for (int i = 0; i < n; ++i)
cin >> ar[i];
memset(dp, -1, sizeof(dp));
cout << calc(0, n - 1, 0, 0, 0) << endl;
}
void preCompute() {}
int main() {
fastread;
preCompute();
ll t = 1, tc;
cin >> t;
for (tc = 1; tc <= t; ++tc) {
solve();
}
return 0;
} | #include "bits/stdc++.h"
#define fastread \
std::ios::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define endl '\n'
#define pb push_back
#define mp make_pair
#define ceel(x, y) (x + y - 1) / y
#define print(a) \
for (auto x : a) \
cout << x << ' '; \
cout << endl;
#define printM(b) \
for (auto y : b) { \
print(y); \
}
#define printP(p) cout << p.first << ' ' << p.second << endl;
#define inp(ar, l, r) \
for (int i = l; i < r; ++i) \
cin >> ar[i];
#define inpar(ar) \
for (int i = 0; i < n; ++i) \
cin >> ar[i];
#define debug(ar, n) \
for (int i = 0; i < n; ++i) \
cout << ar[i] << ' '; \
cout << endl;
#define debugar(ar) \
for (int i = 0; i < n; ++i) \
cout << ar[i] << ' '; \
cout << endl;
#define qwer cout << endl;
#define MOD 1000000007
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef unsigned long long ull;
#define pll pair<ll, ll>
const int N = 3001;
ll dp[2][N + 1][N + 1];
ll ar[N + 1], n;
ll calc(int left, int right, bool player, ll score1, ll score2) {
if (left > right)
return 0;
if (dp[player][left][right] != -1)
return dp[player][left][right];
if (player % 2 == 0) {
ll temp1 = calc(left + 1, right, player ^ 1, score1 + ar[left], score2);
ll temp2 = calc(left, right - 1, player ^ 1, score1 + ar[right], score2);
dp[player][left][right] = max(ar[left] + temp1, ar[right] + temp2);
} else {
ll temp1 = calc(left + 1, right, player ^ 1, score1, score2 + ar[left]);
ll temp2 = calc(left, right - 1, player ^ 1, score1, score2 + ar[right]);
dp[player][left][right] = min(temp1 - ar[left], temp2 - ar[right]);
}
return dp[player][left][right];
}
void solve() {
cin >> n;
for (int i = 0; i < n; ++i)
cin >> ar[i];
memset(dp, -1, sizeof(dp));
cout << calc(0, n - 1, 0, 0, 0) << endl;
}
void preCompute() {}
int main() {
fastread;
preCompute();
ll t = 1, tc;
// cin>>t;
for (tc = 1; tc <= t; ++tc) {
solve();
}
return 0;
} | replace | 89 | 90 | 89 | 90 | -11 | |
p03171 | C++ | Memory Limit Exceeded | // #undef _DEBUG
// #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace std::chrono;
#define int long long // todo 消したら動かない intの代わりにsignedを使う
#define ll long long
auto start_time = system_clock::now();
#define debugName(VariableName) #VariableName
/*@formatter:off*/
//[-n, n)にアクセスできる
// また、外部関数resizeに渡せる
// sizeは[-n, n)でnを返す
template <class T> class mvec {
vector<T> v;
int n;
public:
mvec() : n(0), v(0) {}
mvec(int n) : n(n), v(n * 2) {}
mvec(int n, T val) : n(n), v(n * 2, val) {}
auto &operator[](int i) { return v[i + n]; }
auto size() { return n; }
void resize(int sn) {
assert(n == 0);
n = sn;
v.resize(sn * 2);
}
auto begin() { return v.begin(); }
auto rbegin() { return v.rbegin(); }
auto end() { return v.end(); }
auto rend() { return v.rend(); }
};
//[]でboolは参照を返さないため特殊化が必要
template <> struct mvec<bool> {
vector<bool> v;
int n;
mvec() : n(0), v(0) {}
mvec(int n) : n(n), v(n * 2) {}
mvec(int n, bool val) : n(n), v(n * 2, val) {}
auto operator[](int i) { return v[i + n]; }
auto size() { return v.size(); }
void resize(int sn) {
assert(n == 0);
n = sn;
v.resize(sn * 2);
}
auto begin() { return v.begin(); }
auto rbegin() { return v.rbegin(); }
auto end() { return v.end(); }
auto rend() { return v.rend(); }
};
template <class T> ostream &operator<<(ostream &os, mvec<T> &a) {
int spa = 3;
for (auto &&v : a) {
spa = max(spa, (int)(to_string(v).size()) + 1);
}
int n = (int)a.size();
os << endl;
for (int i = -n; i < n; i++) {
int need = spa - ((int)to_string(i).size());
if (i == -n) {
need -= min(need, spa - ((int)to_string(a[i]).size()));
}
while (need--) {
os << " ";
}
os << i;
}
os << endl;
int i = -n;
for (auto &&v : a) {
int need = spa - ((int)to_string(v).size());
if (i == -n) {
need -= min(need, spa - ((int)to_string(i).size()));
}
while (need--) {
os << " ";
}
os << v;
i++;
}
return os;
}
#define mv mvec
#define MV mvec
using mvi = mvec<ll>;
using mvb = mvec<bool>;
using mvs = mvec<string>;
using mvd = mvec<double>;
using mvc = mvec<char>;
#define o_mvvt(o1, o2, o3, o4, name, ...) name
#define mvvt0(t) mvec<mvec<t>>
#define mvvt1(t, a) mvec<mvec<t>> a
#define mvvt2(t, a, b) mvec<mvec<t>> a(b)
#define mvvt3(t, a, b, c) mvec<mvec<t>> a(b, mvec<t>(c))
#define mvvt4(t, a, b, c, d) mvec<mvec<t>> a(b, mvec<t>(c, d))
#define mvvi(...) \
o_mvvt(__VA_ARGS__, mvvt4, mvvt3, mvvt2, mvvt1, mvvt0)(ll, __VA_ARGS__)
template <typename T> mvec<T> make_mv(size_t a) { return mvec<T>(a); }
template <typename T, typename... Ts> auto make_mv(size_t a, Ts... ts) {
return mvec<decltype(make_mv<T>(ts...))>(a, make_mv<T>(ts...));
}
#define mvni(name, ...) auto name = make_mv<ll>(__VA_ARGS__)
#ifdef _DEBUG
// https://marycore.jp/prog/cpp/class-extension-methods/ 違うかも
template <class T, class A = std::allocator<T>>
struct debtor : std::vector<T, A> {
using std::vector<T, A>::vector;
template <class U> int deb_v(U a, int v) { return v; }
template <class U> int deb_v(debtor<U> &a, int v = 0) {
cerr << a.size() << " ";
return deb_v(a.at(0), v + 1);
}
template <class U> void deb_o(U a) { cerr << a << " "; }
template <class U> void deb_o(debtor<U> &a) {
for (int i = 0; i < min((int)a.size(), 15ll); i++) {
deb_o(a[i]);
}
if ((int)a.size() > 15) {
cerr << "...";
}
cerr << endl;
}
typename std::vector<T>::reference
operator[](typename std::vector<T>::size_type n) {
if (n < 0 || n >= (int)this->size()) {
int siz = (int)this->size();
cerr << "vector size = ";
int dim = deb_v((*this));
cerr << endl;
cerr << "out index at " << n << endl;
cerr << endl;
if (dim <= 2) {
deb_o((*this));
}
exit(0);
}
return this->at(n);
}
};
#define vector debtor
#endif
#ifdef _DEBUG
// 区間削除は出来ない
template <class T> struct my_pbds_tree {
set<T> s;
auto begin() { return s.begin(); }
auto end() { return s.end(); }
auto rbegin() { return s.rbegin(); }
auto rend() { return s.rend(); }
auto empty() { return s.empty(); }
auto size() { return s.size(); }
void clear() { s.clear(); }
template <class U> void insert(U v) { s.insert(v); }
template <class U> void operator+=(U v) { insert(v); }
template <class F> auto erase(F v) { return s.erase(v); }
template <class U> auto find(U v) { return s.find(v); }
template <class U> auto lower_bound(U v) { return s.lower_bound(v); }
template <class U> auto upper_bound(U v) { return s.upper_bound(v); }
auto find_by_order(ll k) {
auto it = s.begin();
for (ll i = 0; i < k; i++)
it++;
return it;
}
auto order_of_key(ll v) {
auto it = s.begin();
ll i = 0;
for (; it != s.end() && *it < v; i++)
it++;
return i;
}
};
#define pbds(T) my_pbds_tree<T>
// gp_hash_tableでcountを使えないようにするため
template <class T, class U> struct my_unordered_map {
unordered_map<T, U> m;
my_unordered_map(){};
auto begin() { return m.begin(); }
auto end() { return m.end(); }
auto cbegin() { return m.cbegin(); }
auto cend() { return m.cend(); }
template <class V> auto erase(V v) { return m.erase(v); }
void clear() {
m.clear();
} /*countは gp_hash_tableに存在しない*/ /*!= m.end()*/
template <class V> auto find(V v) { return m.find(v); }
template <class V> auto &operator[](V n) { return m[n]; }
};
#define unordered_map my_unordered_map
#define umapi unordered_map<ll, ll>
#define umapp unordered_map<P, ll>
#define umapip unordered_map<ll, P>
#else
// umapはunorderd_mapになる
// umapiはgp_hash_table
// find_by_order(k) k番目のイテレーター
// order_of_key(k) k以上が前から何番目か
#define pbds(U) \
__gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, \
__gnu_pbds::tree_order_statistics_node_update>
#define umapi __gnu_pbds::gp_hash_table<ll, ll, xorshift>
#define umapp __gnu_pbds::gp_hash_table<P, ll, xorshift>
#define umapip __gnu_pbds::gp_hash_table<ll, P, xorshift>
#endif
struct xorshift {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
size_t operator()(std::pair<ll, ll> x) const {
ll v = ((x.first) << 32) | x.second;
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(v + FIXED_RANDOM);
}
};
template <class U, class L>
void operator+=(
__gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update> &s,
L v) {
s.insert(v);
}
// 衝突対策
#define ws ws_
template <class A, class B, class C> struct T2 {
A f;
B s;
C t;
T2() { f = 0, s = 0, t = 0; }
T2(A f, B s, C t) : f(f), s(s), t(t) {}
bool operator<(const T2 &r) const {
return f != r.f ? f < r.f
: s != r.s ? s < r.s
: t < r.t; /*return f != r.f ? f > r.f : s != r.s ?n s >
r.s : t > r.t; 大きい順 */
}
bool operator>(const T2 &r) const {
return f != r.f ? f > r.f
: s != r.s ? s > r.s
: t > r.t; /*return f != r.f ? f > r.f : s != r.s ? s >
r.s : t > r.t; 小さい順 */
}
bool operator==(const T2 &r) const {
return f == r.f && s == r.s && t == r.t;
}
bool operator!=(const T2 &r) const {
return f != r.f || s != r.s || t != r.t;
}
};
template <class A, class B, class C, class D> struct F2 {
A a;
B b;
C c;
D d;
F2() { a = 0, b = 0, c = 0, d = 0; }
F2(A a, B b, C c, D d) : a(a), b(b), c(c), d(d) {}
bool operator<(const F2 &r) const {
return a != r.a ? a < r.a
: b != r.b ? b < r.b
: c != r.c ? c < r.c
: d < r.d; /* return a != r.a ? a > r.a : b != r.b ? b
> r.b : c != r.c ? c > r.c : d > r.d;*/
}
bool operator>(const F2 &r) const {
return a != r.a ? a > r.a
: b != r.b ? b > r.b
: c != r.c ? c > r.c
: d > r.d; /* return a != r.a ? a < r.a : b != r.b
? b < r.b : c != r.c ? c < r.c : d < r.d;*/
}
bool operator==(const F2 &r) const {
return a == r.a && b == r.b && c == r.c && d == r.d;
}
bool operator!=(const F2 &r) const {
return a != r.a || b != r.b || c != r.c || d != r.d;
}
ll operator[](ll i) {
assert(i < 4);
return i == 0 ? a : i == 1 ? b : i == 2 ? c : d;
}
};
typedef T2<ll, ll, ll> T;
typedef F2<ll, ll, ll, ll> F;
T mt(ll a, ll b, ll c) { return T(a, b, c); }
F mf(ll a, ll b, ll c, ll d) { return F(a, b, c, d); }
//@マクロ省略系 型,構造
#define double long double
#define pow powl
#define ull unsigned long long
using dou = double;
using itn = int;
using str = string;
using bo = bool;
#define au auto
using P = pair<ll, ll>;
using mvp = mvec<P>;
using mvt = mvec<T>;
#define MIN(a) numeric_limits<a>::min()
#define MAX(a) numeric_limits<a>::max()
#define fi first
#define se second
#define beg begin
#define rbeg rbegin
#define con continue
#define bre break
#define brk break
#define is ==
#define el else
#define elf else if
#define upd update
#define sstream stringstream
#define maxq 1
#define minq -1
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
#define MALLOC(type, len) (type *)malloc((len) * sizeof(type))
#define lam(right) [&](auto &p) { return p right; }
// マクロ省略系 コンテナ
using vi = vector<ll>;
using vb = vector<bool>;
using vs = vector<string>;
using vd = vector<double>;
using vc = vector<char>;
using vp = vector<P>;
using vt = vector<T>;
#define V vector
#define o_vvt(o1, o2, o3, o4, name, ...) name
#define vvt0(t) vector<vector<t>>
#define vvt1(t, a) vector<vector<t>> a
#define vvt2(t, a, b) vector<vector<t>> a(b)
#define vvt3(t, a, b, c) vector<vector<t>> a(b, vector<t>(c))
#define vvt4(t, a, b, c, d) vector<vector<t>> a(b, vector<t>(c, d))
#define vvi(...) \
o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(ll, __VA_ARGS__)
#define vvb(...) \
o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(bool, __VA_ARGS__)
#define vvs(...) \
o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(string, __VA_ARGS__)
#define vvd(...) \
o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(double, __VA_ARGS__)
#define vvc(...) \
o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(char, __VA_ARGS__)
#define vvp(...) \
o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(P, __VA_ARGS__)
#define vvt(...) \
o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(T, __VA_ARGS__)
#define vv(type, ...) \
o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(type, __VA_ARGS__)
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
#define vni(name, ...) auto name = make_v<ll>(__VA_ARGS__)
#define vnb(name, ...) auto name = make_v<bool>(__VA_ARGS__)
#define vns(name, ...) auto name = make_v<string>(__VA_ARGS__)
#define vnd(name, ...) auto name = make_v<double>(__VA_ARGS__)
#define vnc(name, ...) auto name = make_v<char>(__VA_ARGS__)
#define vnp(name, ...) auto name = make_v<P>(__VA_ARGS__)
#define vn(type, name, ...) auto name = make_v<type>(__VA_ARGS__)
#define PQ priority_queue<ll, vector<ll>, greater<ll>>
#define tos to_string
using mapi = map<ll, ll>;
using mapp = map<P, ll>;
using mapd = map<dou, ll>;
using mapc = map<char, ll>;
using maps = map<str, ll>;
using seti = set<ll>;
using setd = set<dou>;
using setc = set<char>;
using sets = set<str>;
using qui = queue<ll>;
#define uset unordered_set
#define useti unordered_set<ll, xorshift>
#define mset multiset
#define mseti multiset<ll>
#define umap unordered_map
#define mmap multimap
// 任意のマクロサポート用 使う度に初期化する
int index_, v1_, v2_, v3_;
template <class T> struct pq {
priority_queue<T, vector<T>, greater<T>> q; /*小さい順*/
T su = 0;
void clear() {
q = priority_queue<T, vector<T>, greater<T>>();
su = 0;
}
void operator+=(T v) {
su += v;
q.push(v);
}
T sum() { return su; }
T top() { return q.top(); }
void pop() {
su -= q.top();
q.pop();
}
T poll() {
T ret = q.top();
su -= ret;
q.pop();
return ret;
}
ll size() { return q.size(); }
};
template <class T> struct pqg {
priority_queue<T> q; /*大きい順*/
T su = 0;
void clear() {
q = priority_queue<T>();
su = 0;
}
void operator+=(T v) {
su += v;
q.push(v);
}
T sum() { return su; }
T top() { return q.top(); }
void pop() {
su -= q.top();
q.pop();
}
T poll() {
T ret = q.top();
su -= ret;
q.pop();
return ret;
}
ll size() { return q.size(); }
};
#define pqi pq<ll>
#define pqgi pqg<ll>
// マクロ 繰り返し
// ↓@オーバーロード隔離
#define o_rep(o1, o2, o3, o4, name, ...) name
#define rep1(n) for (ll rep1i = 0, rep1lim = n; rep1i < rep1lim; ++rep1i)
#define rep2(i, n) for (ll i = 0, rep2lim = n; i < rep2lim; ++i)
#define rep3(i, m, n) for (ll i = m, rep3lim = n; i < rep3lim; ++i)
#define rep4(i, m, n, ad) for (ll i = m, rep4lim = n; i < rep4lim; i += ad)
// 逆順 閉区間
#define rer2(i, n) for (ll i = n; i >= 0; i--)
#define rer3(i, m, n) for (ll i = m, rer3lim = n; i >= rer3lim; i--)
#define rer4(i, m, n, dec) for (ll i = m, rer4lim = n; i >= rer4lim; i -= dec)
// ループを一つにまとめないとフォーマットで汚くなるため
#define nex_ind1(i) i++
#define nex_ind2(i, j, J) \
i = (j + 1 == J) ? i + 1 : i, j = (j + 1 == J ? 0 : j + 1)
#define nex_ind3(i, j, k, J, K) \
i = (j + 1 == J && k + 1 == K) ? i + 1 : i, \
j = (k + 1 == K) ? (j + 1 == J ? 0 : j + 1) : j, \
k = (k + 1 == K ? 0 : k + 1)
#define nex_ind4(i, j, k, l, J, K, L) \
i = (j + 1 == J && k + 1 == K && l + 1 == L) ? i + 1 : i, \
j = (k + 1 == K && l + 1 == L) ? (j + 1 == J ? 0 : j + 1) : j, \
k = (l + 1 == L ? (k + 1 == K ? 0 : k + 1) : k), l = l + 1 == L ? 0 : l + 1
#define nex_ind5(i, j, k, l, m, J, K, L, M) \
i = (j + 1 == J && k + 1 == K && l + 1 == L && m + 1 == M) ? i + 1 : i, \
j = (k + 1 == K && l + 1 == L && m + 1 == M) ? (j + 1 == J ? 0 : j + 1) : j, \
k = (l + 1 == L && m + 1 == M ? (k + 1 == K ? 0 : k + 1) : k), \
l = m + 1 == M ? l + 1 == L ? 0 : l + 1 : l, m = m + 1 == M ? 0 : m + 1
#define repss1(i, I) for (int i = 0; i < I; i++)
#define repss2(i, j, I, J) \
for (int i = (J ? 0 : I), j = 0; i < I; nex_ind2(i, j, J))
#define repss3(i, j, k, I, J, K) \
for (int i = (J && K ? 0 : I), j = 0, k = 0; i < I; nex_ind3(i, j, k, J, K))
#define repss4(i, j, k, l, I, J, K, L) \
for (int i = (J && K && L ? 0 : I), j = 0, k = 0, l = 0; i < I; \
nex_ind4(i, j, k, l, J, K, L))
#define repss5(i, j, k, l, m, I, J, K, L, M) \
for (int i = (J && K && L && M ? 0 : I), j = 0, k = 0, l = 0, m = 0; i < I; \
nex_ind5(i, j, k, l, m, J, K, L, M))
#define o_repss(o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, name, ...) name
// i,j,k...をnまで見る
#define reps1(i, n) repss1(i, n)
#define reps2(i, j, n) repss2(i, j, n, n)
#define reps3(i, j, k, n) repss3(i, j, k, n, n, n)
#define reps4(i, j, k, l, n) repss4(i, j, k, l, n, n, n, n)
#define o_reps(o1, o2, o3, o4, o5, name, ...) name
template <class T>
void nex_repv2(int &i, int &j, int &I, int &J, vector<vector<T>> &s) {
while (1) {
j++;
if (j >= J) {
j = 0;
i++;
if (i < I) {
J = (int)s[i].size();
}
}
if (i >= I || J)
return;
}
}
template <class T>
void nex_repv3(int &i, int &j, int &k, int &I, int &J, int &K,
vector<vector<vector<T>>> &s) {
while (1) {
k++;
if (k >= K) {
k = 0;
j++;
if (j >= J) {
j = 0;
i++;
if (i >= I)
return;
}
}
J = (int)s[i].size();
K = (int)s[i][j].size();
if (J && K)
return;
}
}
#define repv_1(i, a) repss1(i, sz(a))
// 正方形である必要はない
// 直前を持つのとどっちが早いか
#define repv_2(i, j, a) \
for (int I = (int)a.size(), J = (int)a[0].size(), i = 0, j = 0; i < I; \
nex_repv2(i, j, I, J, a))
// 箱状になっている事が要求される つまり[i] 次元目の要素数は一定
#define repv_3(i, j, k, a) \
for (int I = (int)a.size(), J = (int)a[0].size(), K = (int)a[0][0].size(), \
i = 0, j = 0, k = 0; \
i < I; nex_repv3(i, j, k, I, J, K, a))
#define repv_4(i, j, k, l, a) \
repss4(i, j, k, l, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0]))
#define repv_5(i, j, k, l, m, a) \
repss5(i, j, k, l, m, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0]), \
sz(a[0][0][0][0]))
#define o_repv(o1, o2, o3, o4, o5, o6, name, ...) name
template <typename T> struct has_rbegin_rend {
private:
template <typename U>
static auto check(U &&obj)
-> decltype(std::rbegin(obj), std::rend(obj), std::true_type{});
static std::false_type check(...);
public:
static constexpr bool value = decltype(check(std::declval<T>()))::value;
};
template <typename T>
constexpr bool has_rbegin_rend_v = has_rbegin_rend<T>::value;
template <typename Iterator> class Range {
public:
Range(Iterator &&begin, Iterator &&end) noexcept
: m_begin(std::forward<Iterator>(begin)),
m_end(std::forward<Iterator>(end)) {}
Iterator begin() const noexcept { return m_begin; }
Iterator end() const noexcept { return m_end; }
private:
const Iterator m_begin;
const Iterator m_end;
};
template <typename Iterator>
static inline Range<Iterator> makeRange(Iterator &&begin,
Iterator &&end) noexcept {
return Range<Iterator>{std::forward<Iterator>(begin),
std::forward<Iterator>(end)};
}
template <typename T>
static inline decltype(auto)
makeReversedRange(const std::initializer_list<T> &iniList) noexcept {
return makeRange(std::rbegin(iniList), std::rend(iniList));
}
template <typename T, typename std::enable_if_t<has_rbegin_rend_v<T>,
std::nullptr_t> = nullptr>
static inline decltype(auto) makeReversedRange(T &&c) noexcept {
return makeRange(std::rbegin(c), std::rend(c));
} /* rbegin(),
rend()を持たないものはこっちに分岐させて,エラーメッセージを少なくする*/
template <typename T, typename std::enable_if<!has_rbegin_rend<T>::value,
std::nullptr_t>::type = nullptr>
static inline void makeReversedRange(T &&) noexcept {
static_assert(has_rbegin_rend<T>::value,
"Specified argument doesn't have reverse iterator.");
}
#define form1(st) \
for (auto &&form_it = st.begin(); form_it != st.end(); ++form_it)
#define form3(k, v, st) \
for (auto &&form_it = st.begin(); form_it != st.end(); ++form_it)
#define form4(k, v, st, r) \
for (auto &&form_it = st.begin(); form_it != st.end() && (*form_it).fi < r; \
++form_it)
#define form5(k, v, st, l, r) \
for (auto &&form_it = st.lower_bound(l); \
form_it != st.end() && (*form_it).fi < r; ++form_it)
#define o_form(a, b, c, d, e, name, ...) name
template <class U> vector<U> to1d(vector<U> &a) { return a; }
template <class U> vector<U> to1d(V<V<U>> &a) {
vector<U> res;
for (auto &&a1 : a)
for (auto &&a2 : a1)
res.push_back(a2);
return res;
}
template <class U> vector<U> to1d(V<V<V<U>>> &a) {
vector<U> res;
for (auto &&a1 : a)
for (auto &&a2 : a1)
for (auto &&a3 : a2)
res.push_back(a3);
return res;
}
template <class U> vector<U> to1d(V<V<V<V<U>>>> &a) {
vector<U> res;
for (auto &&a1 : a)
for (auto &&a2 : a1)
for (auto &&a3 : a2)
for (auto &&a4 : a3)
res.push_back(a4);
return res;
}
template <class U> vector<U> to1d(V<V<V<V<V<U>>>>> &a) {
vector<U> res;
for (auto &&a1 : a)
for (auto &&a2 : a1)
for (auto &&a3 : a2)
for (auto &&a4 : a3)
for (auto &&a5 : a4)
res.push_back(a5);
return res;
}
template <class U> vector<U> to1d(V<V<V<V<V<V<U>>>>>> &a) {
vector<U> res;
for (auto &&a1 : a)
for (auto &&a2 : a1)
for (auto &&a3 : a2)
for (auto &&a4 : a3)
for (auto &&a5 : a4)
for (auto &&a6 : a5)
res.push_back(a6);
return res;
}
#define fora_init_2(a, A) ;
#define fora_init_4(a, b, A, B) \
auto &&a = A[fora_i]; \
auto &&b = B[fora_i];
#define fora_init_6(a, b, c, A, B, C) \
auto &&a = A[fora_i]; \
auto &&b = B[fora_i]; \
auto &&c = C[fora_i];
#define fora_init_8(a, b, c, d, A, B, C, D) \
auto &&a = A[fora_i]; \
auto &&b = B[fora_i]; \
auto &&c = C[fora_i]; \
auto &&d = D[fora_i];
#define o_fora_init(a, b, c, d, e, f, g, h, name, ...) name
#define fora_init(...) \
o_fora_init(__VA_ARGS__, fora_init_8, a, fora_init_6, a, fora_init_4, a, \
fora_init_2)(__VA_ARGS__)
#define forr_init_2(a, A) auto &&a = A[forr_i];
#define forr_init_4(a, b, A, B) \
auto &&a = A[forr_i]; \
auto &&b = B[forr_i];
#define forr_init_6(a, b, c, A, B, C) \
auto &&a = A[forr_i]; \
auto &&b = B[forr_i]; \
auto &&c = C[forr_i];
#define forr_init_8(a, b, c, d, A, B, C, D) \
auto &&a = A[forr_i]; \
auto &&b = B[forr_i]; \
auto &&c = C[forr_i]; \
auto &&d = D[forr_i];
#define o_forr_init(a, b, c, d, e, f, g, h, name, ...) name
#define forr_init(...) \
o_forr_init(__VA_ARGS__, forr_init_8, a, forr_init_6, a, forr_init_4, a, \
forr_init_2)(__VA_ARGS__)
#define forp_init(k, v, ...) \
auto &&k = (*forp_it).fi; \
auto &&v = (*forp_it).se;
#define form_init(k, v, ...) \
auto &&k = (*form_it).fi; \
auto &&v = (*form_it).se;
// ↑@オーバーロード隔離
// rep系はインデックス、for系は中身
#define rep(...) o_rep(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rer(...) o_rep(__VA_ARGS__, rer4, rer3, rer2, )(__VA_ARGS__)
// char用のrep
#define repc(i, m, n) for (char i = m, repc3lim = n; i < repc3lim; ++i)
// i,j,k...をnまで見る
#define reps(...) o_reps(__VA_ARGS__, reps4, reps3, reps2, reps1, )(__VA_ARGS__)
#define repss(...) \
o_repss(__VA_ARGS__, repss5, a, repss4, a, repss3, a, repss2, a, \
repss1)(__VA_ARGS__)
// vectorのindexを走査する
// repv(i,j,vvi)
#define repv(...) \
o_repv(__VA_ARGS__, repv_5, repv_4, repv_3, repv_2, repv_1, )(__VA_ARGS__)
#define rerv(i, A) for (int i = sz(A) - 1; i >= 0; i--)
// repvn(dp) nは次元
#define repv1(a) repv(i, a)
#define repv2(a) repv(i, j, a)
#define repv3(a) repv(i, j, k, a)
#define repv4(a) repv(i, j, k, l, a)
#define fora_2(a, A) for (auto &&a : A)
#define fora_4(a, b, A, B) rep(fora_i, sz(A))
#define fora_6(a, b, c, A, B, C) rep(fora_i, sz(A))
#define fora_8(a, b, c, d, A, B, C, D) rep(fora_i, sz(A))
#define o_fora(a, b, c, d, e, f, g, h, name, ...) name
#define fora(...) \
o_fora(__VA_ARGS__, fora_8, a, fora_6, a, fora_4, a, fora_2)(__VA_ARGS__)
#define forr_2(a, A) rer(forr_i, sz(A) - 1)
#define forr_4(a, b, A, B) rer(forr_i, sz(A) - 1)
#define forr_6(a, b, c, A, B, C) rer(forr_i, sz(A) - 1)
#define forr_8(a, b, c, d, A, B, C, D) rer(forr_i, sz(A) - 1)
#define o_forr(a, b, c, d, e, f, g, h, name, ...) name
#define forr(...) \
o_forr(__VA_ARGS__, forr_8, a, forr_6, a, forr_4, a, forr_2)(__VA_ARGS__)
// #define forr(v, a) for(auto&& v : makeReversedRange(a))
// 参照を取らない
#define forv(a, b) for (auto a : to1d(b))
// インデックスを前後含めて走査
#define ring(i, s, len) \
for (int i = s, prev = (s == 0) ? len - 1 : s - 1, \
next = (s == len - 1) ? 0 : s + 1, cou = 0; \
cou < len; \
cou++, prev = i, i = next, next = (next == len - 1) ? 0 : next + 1)
// 値と前後を見る
#define ringv(v, d) \
index_ = 0; \
for (auto prev = d[sz(d) - 1], next = (int)d.size() > 1 ? d[1] : d[0], \
v = d[0]; \
index_ < sz(d); index_++, prev = v, v = next, \
next = (index_ >= sz(d) - 1 ? d[0] : d[index_ + 1]))
// 0の左と nの右
#define forlr(v, d) \
for (auto prev = -1_, next = (int)d.size() > 1 ? d[1] : d[0], v = d[0]; \
index_ < sz(d); index_++, prev = v, v = next, \
next = (index_ >= sz(d) - 1 ? -1_ : d[index_ + 1]))
#define form(...) \
o_form(__VA_ARGS__, form5, form4, form3, form2, form1)(__VA_ARGS__)
#define forp(k, v, st) \
for (auto &&forp_it = st.begin(); forp_it != st.end(); ++forp_it)
// マクロ 定数
#define k3 1010
#define k4 10101
#define k5 101010
#define k6 1010101
#define k7 10101010
const ll inf = (ll)1e9 + 100;
const ll linf = (ll)1e18 + 100;
const char infc = '{';
const string infs = "{";
const double eps = 1e-9;
const double PI = 3.1415926535897932384626433832795029L;
// マクロ省略形 関数等
#define arsz(a) (sizeof(a) / sizeof(a[0]))
#define sz(a) ((ll)(a).size())
#define mp make_pair
#define pb pop_back
#define pf push_front
#define eb emplace_back
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
constexpr bool ev(ll a) { return !(a & 1); }
constexpr bool od(ll a) { return (a & 1); }
//@拡張系 こう出来るべきというもの
// 埋め込み 存在を意識せずに機能を増やされているもの
namespace std {
template <> class hash<std::pair<signed, signed>> {
public:
size_t operator()(const std::pair<signed, signed> &x) const {
return hash<ll>()(((ll)x.first << 32) | x.second);
}
};
template <> class hash<std::pair<ll, ll>> {
public
: /*大きいllが渡されると、<<32でオーバーフローするがとりあえず問題ないと判断*/
size_t operator()(const std::pair<ll, ll> &x) const {
return hash<ll>()(((ll)x.first << 32) | x.second);
}
};
} // namespace std
// stream まとめ
/*@formatter:on*/
istream &operator>>(istream &iss, P &a) {
iss >> a.first >> a.second;
return iss;
}
template <typename T> istream &operator>>(istream &iss, vector<T> &vec) {
for (T &x : vec)
iss >> x;
return iss;
}
template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) {
os << p.fi << " " << p.se;
return os;
}
ostream &operator<<(ostream &os, T p) {
os << p.f << " " << p.s << " " << p.t;
return os;
}
ostream &operator<<(ostream &os, F p) {
os << p.a << " " << p.b << " " << p.c << " " << p.d;
return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
for (ll i = 0; i < vec.size(); ++i)
os << vec[i] << (i + 1 == vec.size() ? "" : " ");
return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
for (ll i = 0; i < vec.size(); ++i) {
for (ll j = 0; j < vec[i].size(); ++j) {
os << vec[i][j] << " ";
}
os << endl;
}
return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, map<T, U> &m) {
for (auto &&v : m)
os << v;
return os;
}
template <class T> ostream &operator<<(ostream &os, set<T> s) {
fora(v, s) { os << v << " "; }
return os;
}
template <class T> ostream &operator<<(ostream &os, deque<T> a) {
fora(v, a) os << v << " ";
return os;
}
ostream &operator<<(ostream &os, vector<vector<char>> &vec) {
rep(h, sz(vec)) {
rep(w, sz(vec[0])) { os << vec[h][w]; }
os << endl;
}
return os;
}
// template<class T,class U>ostream &operator<<(ostream &os, vector<pair<T,U>>&
// a) {fora(v,a)os<<v<<endl;return os;}
/*@formatter:off*/
template <typename W, typename H> void resize(W &vec, const H head) {
vec.resize(head);
}
template <typename W, typename H, typename... T>
void resize(W &vec, const H &head, const T... tail) {
vec.resize(head);
for (auto &v : vec)
resize(v, tail...);
}
// template<typename W, typename H> void resize(vector<W> &vec, const H head) {
// vec.resize(head); } template<typename W, typename H, typename ... T> void
// resize(vector<W> &vec, const H &head, const T ... tail) {vec.resize(head);for
// (auto &v: vec)resize(v, tail...);}
template <typename T, typename F> bool all_of2(T &v, F f) { return f(v); }
template <typename T, typename F> bool all_of2(vector<T> &v, F f) {
rep(i, sz(v)) {
if (!all_of2(v[i], f))
return false;
}
return true;
}
template <typename T, typename F> bool any_of2(T &v, F f) { return f(v); }
template <typename T, typename F> bool any_of2(vector<T> &v, F f) {
rep(i, sz(v)) {
if (any_of2(v[i], f))
return true;
}
return false;
}
template <typename T, typename F> bool none_of2(T &v, F f) { return f(v); }
template <typename T, typename F> bool none_of2(vector<T> &v, F f) {
rep(i, sz(v)) {
if (none_of2(v[i], f))
return false;
}
return true;
}
template <typename T, typename F> bool find_if2(T &v, F f) { return f(v); }
template <typename T, typename F> ll find_if2(vector<T> &v, F f) {
rep(i, sz(v)) {
if (find_if2(v[i], f))
return i;
}
return sz(v);
}
template <typename T, typename F> bool rfind_if2(T &v, F f) { return f(v); }
template <typename T, typename F> ll rfind_if2(vector<T> &v, F f) {
rer(i, sz(v) - 1) {
if (rfind_if2(v[i], f))
return i;
}
return -1;
}
template <class T> bool contains(string &s, const T &v) {
return s.find(v) != string::npos;
}
template <typename T> bool contains(vector<T> &v, const T &val) {
return std::find(v.begin(), v.end(), val) != v.end();
}
template <typename T, typename F> bool contains_if2(vector<T> &v, F f) {
return find_if(v.begin(), v.end(), f) != v.end();
}
template <typename T, typename F> ll count_if2(T &v, F f) { return f(v); }
template <typename T, typename F> ll count_if2(vector<T> &vec, F f) {
ll ret = 0;
fora(v, vec) ret += count_if2(v, f);
return ret;
}
template <typename T, typename F> void for_each2(T &v, F f) { f(v); }
template <typename T, typename F> void for_each2(vector<T> &vec, F f) {
fora(v, vec) for_each2(v, f);
}
template <typename W> ll count_od(vector<W> &a) {
return count_if2(a, [](ll v) { return v & 1; });
}
template <typename W> ll count_ev(vector<W> &a) {
return count_if2(a, [](ll v) { return !(v & 1); });
}
// 削除された要素の数を返す
template <typename T, typename F> int erase_if2(vector<T> &v, F f) {
vector<T> nv;
int cou = 0;
rep(i, sz(v)) {
if (f(v[i])) {
cou++;
} else {
nv.push_back(v[i]);
}
}
v = nv;
return cou;
}
template <typename T, typename F> int erase_if2(vector<vector<T>> &v, F f) {
int cou = 0;
rep(i, sz(v)) { cou += erase_if2(v[i], f); }
return cou;
}
#define all_of(a, right) all_of2(a, lam(right))
#define all_of_f(a, f) all_of2(a, f)
#define any_of(a, right) any_of2(a, lam(right))
#define any_of_f(a, f) any_of2(a, f)
#define none_of(a, right) none_of2(a, lam(right))
#define none_of_f(a, f) none_of2(a, f)
#define find_if(a, right) find_if2(a, lam(right))
#define find_if_f(a, f) find_if2(a, f)
#define rfind_if(a, right) rfind_if2(a, lam(right))
#define rfind_if_f(a, f) rfind_if2(a, f)
#define contains_if(a, right) contains_if2(a, lam(right))
#define contains_if_f(a, f) contains_if2(a, f)
#define count_if(a, right) count_if2(a, lam(right))
#define count_if_f(a, f) count_if2(a, f)
#define for_each(a, right) \
do { \
fora(v, a) { v right; } \
} while (0)
#define for_each_f(a, f) \
do { \
fora(v, a) { f(v); } \
} while (0)
#define erase_if(a, right) erase_if2(a, lam(right))
#define erase_if_f(a, f) erase_if2(a, f)
template <class T, class U> void replace(vector<T> &a, T key, U v) {
replace(a.begin(), a.end(), key, v);
}
void replace(str &a, char key, str v) {
if (v == "")
a.erase(remove(all(a), key), a.end());
}
void replace(str &a, char key, char v) { replace(all(a), key, v); }
// keyと同じかどうか01で置き換える
template <class T, class U> void replace(vector<T> &a, U k) {
rep(i, sz(a)) a[i] = a[i] == k;
}
template <class T, class U> void replace(vector<vector<T>> &a, U k) {
rep(i, sz(a)) rep(j, sz(a[0])) a[i][j] = a[i][j] == k;
}
template <class T> void replace(T &a) { replace(a, '#'); }
void replace(str &a, str key, str v) {
stringstream t;
ll kn = sz(key);
std::string::size_type Pos(a.find(key));
ll l = 0;
while (Pos != std::string::npos) {
t << a.substr(l, Pos - l);
t << v;
l = Pos + kn;
Pos = a.find(key, Pos + kn);
}
t << a.substr(l, sz(a) - l);
a = t.str();
}
template <class T> bool includes(vector<T> &a, vector<T> &b) {
vi c = a;
vi d = b;
sort(all(c));
sort(all(d));
return includes(all(c), all(d));
}
template <class T> bool is_permutation(vector<T> &a, vector<T> &b) {
return is_permutation(all(a), all(b));
}
template <class T> bool next_permutation(vector<T> &a) {
return next_permutation(all(a));
}
void iota(vector<ll> &ve, ll s, ll n) {
ve.resize(n);
iota(all(ve), s);
}
vi iota(ll s, ll len) {
vi ve(len);
iota(all(ve), s);
return ve;
}
template <class A, class B> auto vtop(vector<A> &a, vector<B> &b) {
assert(sz(a) == sz(b)); /*stringを0で初期化できない */
vector<pair<A, B>> res;
rep(i, sz(a)) res.eb(a[i], b[i]);
return res;
}
template <class A, class B>
void ptov(vector<pair<A, B>> &p, vector<A> &a, vector<B> &b) {
a.resize(sz(p)), b.resize(sz(p));
rep(i, sz(p)) a[i] = p[i].fi, b[i] = p[i].se;
}
template <class A, class B, class C>
auto vtot(vector<A> &a, vector<B> &b, vector<C> &c) {
assert(sz(a) == sz(b) && sz(b) == sz(c));
vector<T2<A, B, C>> res;
rep(i, sz(a)) res.eb(a[i], b[i], c[i]);
return res;
}
template <class A, class B, class C, class D>
auto vtof(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) {
assert(sz(a) == sz(b) && sz(b) == sz(c) && sz(c) == sz(d));
vector<F2<A, B, C, D>> res;
rep(i, sz(a)) res.eb(a[i], b[i], c[i], d[i]);
return res;
}
enum pcomparator { fisi, fisd, fdsi, fdsd, sifi, sifd, sdfi, sdfd };
enum tcomparator {
fisiti,
fisitd,
fisdti,
fisdtd,
fdsiti,
fdsitd,
fdsdti,
fdsdtd,
fitisi,
fitisd,
fitdsi,
fitdsd,
fdtisi,
fdtisd,
fdtdsi,
fdtdsd,
sifiti,
sifitd,
sifdti,
sifdtd,
sdfiti,
sdfitd,
sdfdti,
sdfdtd,
sitifi,
sitifd,
sitdfi,
sitdfd,
sdtifi,
sdtifd,
sdtdfi,
sdfdfd,
tifisi,
tifisd,
tifdsi,
tifdsd,
tdfisi,
tdfisd,
tdfdsi,
tdfdsd,
tisifi,
tisifd,
tisdfi,
tisdfd,
tdsifi,
tdsifd,
tdsdfi,
tdsdfd
};
template <class A, class B> void sort(vector<pair<A, B>> &a, pcomparator type) {
typedef pair<A, B> U;
if (type == fisi)
sort(all(a),
[&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se < r.se; });
else if (type == fisd)
sort(all(a),
[&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se > r.se; });
else if (type == fdsi)
sort(all(a),
[&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se < r.se; });
else if (type == fdsd)
sort(all(a),
[&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se > r.se; });
else if (type == sifi)
sort(all(a),
[&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi < r.fi; });
else if (type == sifd)
sort(all(a),
[&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi > r.fi; });
else if (type == sdfi)
sort(all(a),
[&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi < r.fi; });
else if (type == sdfd)
sort(all(a),
[&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi > r.fi; });
};
template <class U> void sort(vector<U> &a, pcomparator type) {
if (type == fisi)
sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s < r.s; });
else if (type == fisd)
sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s > r.s; });
else if (type == fdsi)
sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s < r.s; });
else if (type == fdsd)
sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s > r.s; });
else if (type == sifi)
sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f < r.f; });
else if (type == sifd)
sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f > r.f; });
else if (type == sdfi)
sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f < r.f; });
else if (type == sdfd)
sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f > r.f; });
};
template <class A, class B, class C, class D>
void sort(vector<F2<A, B, C, D>> &a, pcomparator type) {
typedef F2<A, B, C, D> U;
if (type == fisi)
sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b < r.b; });
else if (type == fisd)
sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b > r.b; });
else if (type == fdsi)
sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b < r.b; });
else if (type == fdsd)
sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b > r.b; });
else if (type == sifi)
sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a < r.a; });
else if (type == sifd)
sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a > r.a; });
else if (type == sdfi)
sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a < r.a; });
else if (type == sdfd)
sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a > r.a; });
};
template <class U> void sort(vector<U> &a, tcomparator type) {
if (type == 0)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t < r.t;
});
else if (type == 1)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t > r.t;
});
else if (type == 2)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t < r.t;
});
else if (type == 3)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t > r.t;
});
else if (type == 4)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t < r.t;
});
else if (type == 5)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t > r.t;
});
else if (type == 6)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t < r.t;
});
else if (type == 7)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t > r.t;
});
else if (type == 8)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s < r.s;
});
else if (type == 9)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s > r.s;
});
else if (type == 10)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s < r.s;
});
else if (type == 11)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s > r.s;
});
else if (type == 12)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s < r.s;
});
else if (type == 13)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s > r.s;
});
else if (type == 14)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s < r.s;
});
else if (type == 15)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s > r.s;
});
else if (type == 16)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t < r.t;
});
else if (type == 17)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t > r.t;
});
else if (type == 18)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t < r.t;
});
else if (type == 19)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t > r.t;
});
else if (type == 20)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t < r.t;
});
else if (type == 21)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t > r.t;
});
else if (type == 22)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t < r.t;
});
else if (type == 23)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t > r.t;
});
else if (type == 24)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f < r.f;
});
else if (type == 25)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f > r.f;
});
else if (type == 26)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f < r.f;
});
else if (type == 27)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f > r.f;
});
else if (type == 28)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f < r.f;
});
else if (type == 29)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f > r.f;
});
else if (type == 30)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f < r.f;
});
else if (type == 31)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f > r.f;
});
else if (type == 32)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s < r.s;
});
else if (type == 33)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s > r.s;
});
else if (type == 34)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s < r.s;
});
else if (type == 35)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s > r.s;
});
else if (type == 36)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s < r.s;
});
else if (type == 37)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s > r.s;
});
else if (type == 38)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s < r.s;
});
else if (type == 39)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s > r.s;
});
else if (type == 40)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f < r.f;
});
else if (type == 41)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f > r.f;
});
else if (type == 42)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f < r.f;
});
else if (type == 43)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f > r.f;
});
else if (type == 44)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f < r.f;
});
else if (type == 45)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f > r.f;
});
else if (type == 46)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f < r.f;
});
else if (type == 47)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f > r.f;
});
}
template <class A, class B, class C, class D>
void sort(vector<F2<A, B, C, D>> &a, tcomparator type) {
typedef F2<A, B, C, D> U;
if (type == 0)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c < r.c;
});
else if (type == 1)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c > r.c;
});
else if (type == 2)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c < r.c;
});
else if (type == 3)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c > r.c;
});
else if (type == 4)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c < r.c;
});
else if (type == 5)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c > r.c;
});
else if (type == 6)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c < r.c;
});
else if (type == 7)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c > r.c;
});
else if (type == 8)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b < r.b;
});
else if (type == 9)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b > r.b;
});
else if (type == 10)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b < r.b;
});
else if (type == 11)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b > r.b;
});
else if (type == 12)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b < r.b;
});
else if (type == 13)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b > r.b;
});
else if (type == 14)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b < r.b;
});
else if (type == 15)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b > r.b;
});
else if (type == 16)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c < r.c;
});
else if (type == 17)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c > r.c;
});
else if (type == 18)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c < r.c;
});
else if (type == 19)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c > r.c;
});
else if (type == 20)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c < r.c;
});
else if (type == 21)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c > r.c;
});
else if (type == 22)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c < r.c;
});
else if (type == 23)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c > r.c;
});
else if (type == 24)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a < r.a;
});
else if (type == 25)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a > r.a;
});
else if (type == 26)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a < r.a;
});
else if (type == 27)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a > r.a;
});
else if (type == 28)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a < r.a;
});
else if (type == 29)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a > r.a;
});
else if (type == 30)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a < r.a;
});
else if (type == 31)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a > r.a;
});
else if (type == 32)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b < r.b;
});
else if (type == 33)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b > r.b;
});
else if (type == 34)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b < r.b;
});
else if (type == 35)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b > r.b;
});
else if (type == 36)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b < r.b;
});
else if (type == 37)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b > r.b;
});
else if (type == 38)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b < r.b;
});
else if (type == 39)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b > r.b;
});
else if (type == 40)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a < r.a;
});
else if (type == 41)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a > r.a;
});
else if (type == 42)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a < r.a;
});
else if (type == 43)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a > r.a;
});
else if (type == 44)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a < r.a;
});
else if (type == 45)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a > r.a;
});
else if (type == 46)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a < r.a;
});
else if (type == 47)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a > r.a;
});
}
void sort(string &a) { sort(all(a)); }
template <class T> void sort(vector<T> &a) { sort(all(a)); }
// P l, P rで f(P) の形で渡す
template <class U, class F> void sort(vector<U> &a, F f) {
sort(all(a), [&](U l, U r) { return f(l) < f(r); });
};
template <class T> void rsort(vector<T> &a) { sort(all(a), greater<T>()); };
template <class U, class F> void rsort(vector<U> &a, F f) {
sort(all(a), [&](U l, U r) { return f(l) > f(r); });
};
// F = T<T>
// 例えばreturn p.fi + p.se;
template <class A, class B> void sortp(vector<A> &a, vector<B> &b) {
auto c = vtop(a, b);
sort(c);
rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se;
}
template <class A, class B, class F>
void sortp(vector<A> &a, vector<B> &b, F f) {
auto c = vtop(a, b);
sort(c, f);
rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se;
}
template <class A, class B> void rsortp(vector<A> &a, vector<B> &b) {
auto c = vtop(a, b);
rsort(c);
rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second;
}
template <class A, class B, class F>
void rsortp(vector<A> &a, vector<B> &b, F f) {
auto c = vtop(a, b);
rsort(c, f);
rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second;
}
template <class A, class B, class C>
void sortt(vector<A> &a, vector<B> &b, vector<C> &c) {
auto d = vtot(a, b, c);
sort(d);
rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t;
}
template <class A, class B, class C, class F>
void sortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) {
auto d = vtot(a, b, c);
sort(d, f);
rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t;
}
template <class A, class B, class C>
void rsortt(vector<A> &a, vector<B> &b, vector<C> &c) {
auto d = vtot(a, b, c);
rsort(d);
rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t;
}
template <class A, class B, class C, class F>
void rsortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) {
auto d = vtot(a, b, c);
rsort(d, f);
rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t;
}
template <class A, class B, class C, class D>
void sortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) {
auto e = vtof(a, b, c, d);
sort(e);
rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d;
}
template <class A, class B, class C, class D>
void rsortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) {
auto e = vtof(a, b, c, d);
rsort(e);
rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d;
}
// sortindex 元のvectorはソートしない
template <class T> vi sorti(vector<T> &a) {
auto b = a;
vi ind = iota(0, sz(a));
sortp(b, ind);
return ind;
} /*indexの分で型が変わるためpcomparatorが必要*/
template <class T> vi sorti(vector<T> &a, pcomparator f) {
auto b = a;
vi ind = iota(0, sz(a));
sortp(b, ind, f);
return ind;
}
template <class T, class F> vi sorti(vector<T> &a, F f) {
vi ind = iota(0, sz(a));
sort(all(ind), [&](ll x, ll y) { return f(a[x]) < f(a[y]); });
return ind;
}
template <class T> vi rsorti(vector<T> &a) {
auto b = a;
vi ind = iota(0, sz(a));
rsortp(b, ind);
return ind;
}
template <class T, class F> vi rsorti(vector<T> &a, F f) {
vi ind = iota(0, sz(a));
sort(all(ind), [&](ll x, ll y) { return f(a[x]) > f(a[y]); });
return ind;
}
template <class A, class B, class F>
vi sortpi(vector<A> &a, vector<B> &b, F f) {
auto c = vtop(a, b);
vi ind = iota(0, sz(a));
sort(all(ind), [&](ll x, ll y) { return f(c[x]) < f(c[y]); });
return ind;
}
template <class A, class B>
vi sortpi(vector<A> &a, vector<B> &b, pcomparator f) {
vi ind = iota(0, sz(a));
auto c = a;
auto d = b;
sortt(c, d, ind, f);
return ind;
}
template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b) {
return sortpi(a, b, fisi);
};
template <class A, class B, class F>
vi rsortpi(vector<A> &a, vector<B> &b, F f) {
auto c = vtop(a, b);
vi ind = iota(0, sz(a));
sort(all(ind), [&](ll x, ll y) { return f(c[x]) > f(c[y]); });
return ind;
}
template <class A, class B> vi rsortpi(vector<A> &a, vector<B> &b) {
return sortpi(a, b, fdsd);
};
template <class A, class B, class C, class F>
vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) {
auto d = vtot(a, b, c);
vi ind = iota(0, sz(a));
sort(all(ind), [&](ll x, ll y) { return f(d[x]) < f(d[y]); });
return ind;
}
template <class A, class B, class C>
vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, pcomparator f) {
vi ind = iota(0, sz(a));
auto d = vtof(a, b, c, ind);
sort(d, f);
rep(i, sz(a)) ind[i] = d[i].d;
return ind;
}
template <class A, class B, class C>
vi sortti(vector<A> &a, vector<B> &b, vector<C> &c) {
vi ind = iota(0, sz(a));
sort(all(ind), [&](ll x, ll y) {
if (a[x] == a[y]) {
if (b[x] == b[y])
return c[x] < c[y];
else
return b[x] < b[y];
} else {
return a[x] < a[y];
}
});
return ind;
}
template <class A, class B, class C, class F>
vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) {
auto d = vtot(a, b, c);
vi ind = iota(0, sz(a));
sort(all(ind), [&](ll x, ll y) { return f(d[x]) > f(d[y]); });
return ind;
}
template <class A, class B, class C>
vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c) {
vi ind = iota(0, sz(a));
sort(all(ind), [&](ll x, ll y) {
if (a[x] == a[y]) {
if (b[x] == b[y])
return c[x] > c[y];
else
return b[x] > b[y];
} else {
return a[x] > a[y];
}
});
return ind;
}
template <class T> void sort2(vector<vector<T>> &a) {
for (ll i = 0, n = a.size(); i < n; ++i)
sort(a[i]);
}
template <class T> void rsort2(vector<vector<T>> &a) {
for (ll i = 0, n = a.size(); i < n; ++i)
rsort(a[i]);
}
template <typename A, size_t N, typename T> void fill(A (&a)[N], const T &v) {
rep(i, N) a[i] = v;
}
template <typename A, size_t N, size_t O, typename T>
void fill(A (&a)[N][O], const T &v) {
rep(i, N) rep(j, O) a[i][j] = v;
}
template <typename A, size_t N, size_t O, size_t P, typename T>
void fill(A (&a)[N][O][P], const T &v) {
rep(i, N) rep(j, O) rep(k, P) a[i][j][k] = v;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, typename T>
void fill(A (&a)[N][O][P][Q], const T &v) {
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) a[i][j][k][l] = v;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R,
typename T>
void fill(A (&a)[N][O][P][Q][R], const T &v) {
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) a[i][j][k][l][m] = v;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R,
size_t S, typename T>
void fill(A (&a)[N][O][P][Q][R][S], const T &v) {
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S)
a[i][j][k][l][m][n] = v;
}
template <typename W, typename T> void fill(W &xx, const T vall) { xx = vall; }
template <typename W, typename T> void fill(vector<W> &vecc, const T vall) {
for (auto &&vx : vecc)
fill(vx, vall);
}
template <typename W, typename T> void fill(vector<W> &xx, ll len, const T v) {
rep(i, len) xx[i] = v;
}
template <typename W, typename T>
void fill(vector<vector<W>> &xx, int sh, int th, int sw, int tw, T v) {
rep(h, sh, th) rep(w, sw, tw) xx[h][w] = v;
}
template <class T, class U> void fill(vector<T> &a, vi &ind, U val) {
fora(v, ind) a[v] = val;
}
template <class W, class T> void fill(mvec<W> &xx, const T v) {
fora(x, xx) fill(x, v);
}
template <typename A, size_t N> A sum(A (&a)[N]) {
A res = 0;
rep(i, N) res += a[i];
return res;
}
template <typename A, size_t N, size_t O> A sum(A (&a)[N][O]) {
A res = 0;
rep(i, N) rep(j, O) res += a[i][j];
return res;
}
template <typename A, size_t N, size_t O, size_t P> A sum(A (&a)[N][O][P]) {
A res = 0;
rep(i, N) rep(j, O) rep(k, P) res += a[i][j][k];
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q>
A sum(A (&a)[N][O][P][Q]) {
A res = 0;
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) res += a[i][j][k][l];
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R>
A sum(A (&a)[N][O][P][Q][R]) {
A res = 0;
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) res += a[i][j][k][l][m];
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R,
size_t S>
A sum(A (&a)[N][O][P][Q][R][S]) {
A res = 0;
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S) res +=
a[i][j][k][l][m][n];
return res;
}
//@汎用便利関数 入力
ll in() {
ll ret;
cin >> ret;
return ret;
}
string sin() {
string ret;
cin >> ret;
return ret;
}
template <class T> void in(T &head) { cin >> head; }
template <class T, class... U> void in(T &head, U &...tail) {
cin >> head;
in(tail...);
}
#define o_din(o1, o2, o3, o4, o5, o6, name, ...) name
#define din1(a) \
ll a; \
cin >> a
#define din2(a, b) \
ll a, b; \
cin >> a >> b
#define din3(a, b, c) \
ll a, b, c; \
cin >> a >> b >> c
#define din4(a, b, c, d) \
ll a, b, c, d; \
cin >> a >> b >> c >> d
#define din5(a, b, c, d, e) \
ll a, b, c, d, e; \
cin >> a >> b >> c >> d >> e
#define din6(a, b, c, d, e, f) \
ll a, b, c, d, e, f; \
cin >> a >> b >> c >> d >> e >> f
#define din(...) \
o_din(__VA_ARGS__, din6, din5, din4, din3, din2, din1)(__VA_ARGS__)
#define o_dins(o1, o2, o3, o4, o5, o6, name, ...) name
#define dins1(a) \
str a; \
cin >> a
#define dins2(a, b) \
str a, b; \
cin >> a >> b
#define dins3(a, b, c) \
str a, b, c; \
cin >> a >> b >> c
#define dins4(a, b, c, d) \
str a, b, c, d; \
cin >> a >> b >> c >> d
#define dins5(a, b, c, d, e) \
str a, b, c, d, e; \
cin >> a >> b >> c >> d >> e
#define dins6(a, b, c, d, e, f) \
str a, b, c, d, e, f; \
cin >> a >> b >> c >> d >> e >> f
#define dins(...) \
o_dins(__VA_ARGS__, dins6, dins5, dins4, dins3, dins2, dins1)(__VA_ARGS__)
#define o_dind(o1, o2, o3, o4, name, ...) name
#define din1d(a) \
din1(a); \
a--
#define din2d(a, b) \
din2(a, b); \
a--, b--
#define din3d(a, b, c) \
din3(a, b, c); \
a--, b--, c--
#define din4d(a, b, c, d) \
din4(a, b, c, d); \
a--, b--, c--, d--
#define dind(...) o_dind(__VA_ARGS__, din4d, din3d, din2d, din1d)(__VA_ARGS__)
template <class T> void out2(T &&head) { cout << head; }
template <class T, class... U> void out2(T &&head, U &&...tail) {
cout << head << " ";
out2(tail...);
}
template <class T, class... U> void out(T &&head, U &&...tail) {
cout << head << " ";
out2(tail...);
cout << "" << endl;
}
template <class T> void out(T &&head) { cout << head << endl; }
void out() { cout << "" << endl; }
#ifdef _DEBUG
template <class T> void err2(T &&head) { cerr << head; }
template <class T, class... U> void err2(T &&head, U &&...tail) {
cerr << head << " ";
err2(tail...);
}
template <class T, class... U> void err(T &&head, U &&...tail) {
cerr << head << " ";
err2(tail...);
cerr << "" << endl;
}
template <class T> void err(T &&head) { cerr << head << endl; }
void err() { cerr << "" << endl; }
/*@formatter:on*/
template <class T> string out_m2(vector<T> &a, ll W = inf) {
stringstream ss;
if (W == inf)
W = min(sz(a), 12ll);
if (sz(a) == 0)
return ss.str();
rep(i, W) {
ss << a[i];
if (typeid(a[i]) == typeid(P)) {
ss << endl;
} else {
ss << " ";
}
}
return ss.str();
}
/*@formatter:off*/
template <class T>
string out_m2(vector<vector<T>> &a, ll H = inf, ll W = inf, int key = -1) {
H = min({H, sz(a), 12ll});
W = min({W, sz(a[0]), 12ll});
stringstream ss;
ss << endl;
if (key == -1)
ss << " *|";
else
ss << " " << key << "|";
rep(w, W) ss << std::right << std::setw(4) << w;
ss << "" << endl;
rep(w, W * 4 + 3) ss << "_";
ss << "" << endl;
rep(h, H) {
ss << std::right << std::setw(2) << h << "|";
rep(w, min(sz(a[h]), 12ll)) {
if (abs(a[h][w]) == linf)
ss << " e"
<< "";
else
ss << std::right << std::setw(4) << a[h][w];
}
ss << "" << endl;
}
return ss.str();
}
template <class T>
string out_m2(vector<vector<vector<T>>> &a, ll H = inf, ll W = inf,
ll U = inf) {
stringstream ss;
if (H == inf)
H = 12;
H = min(H, sz(a));
rep(i, H) {
ss << endl;
ss << out_m2(a[i], W, U, i);
}
return ss.str();
}
template <class T, size_t N> string out_m2(T (&a)[N]) {
vector<T> b;
resize(b, N);
rep(i, N) { b[i] = a[i]; }
return out_m2(b);
}
template <class T, size_t N, size_t M> string out_m2(T (&a)[N][M]) {
vector<vector<T>> b;
resize(b, N, M);
rep(i, N) {
rep(j, M) { b[i][j] = a[i][j]; }
}
return out_m2(b);
}
template <class T, size_t N, size_t M, size_t O>
string out_m2(T (&a)[N][M][O]) {
vector<vector<vector<T>>> b;
resize(b, N, M, O);
rep(i, N) {
rep(j, M) {
rep(k, O) { b[i][j][k] = a[i][j][k]; }
}
}
return out_m2(b);
}
string out_m2(int a) {
stringstream ss;
ss << a;
return ss.str();
}
/*@formatter:on*/
template <class T>
string out_m2(mvec<mvec<T>> &a, ll H = inf, ll W = inf, int key = inf) {
H = min({H, sz(a), 6ll});
W = min({W, sz(a[0]), 6ll});
stringstream ss;
ss << endl;
// if (key == inf)ss << " *|"; else ss << " " << key << "|";
if (key == inf)
ss << " *|";
else {
ss << std::right << std::setw(2) << key;
ss << "|";
}
rep(w, -W, W) ss << std::right << std::setw(4) << w;
ss << "" << endl;
rep(w, W * 8 + 3) ss << "_";
ss << "" << endl;
rep(h, -H, H) {
ss << std::right << std::setw(2) << h << "|";
int NW = min(sz(a[h]), 6ll);
rep(w, -NW, NW) {
if (abs(a[h][w]) == linf)
ss << " e"
<< "";
else
ss << std::right << std::setw(4) << a[h][w];
}
ss << "" << endl;
}
return ss.str();
} /*@formatter:on*/
template <class T>
string out_m2(mvec<mvec<mvec<T>>> &a, ll H = inf, ll W = inf, ll U = inf) {
stringstream ss;
if (H == inf)
H = 6;
H = min(H, sz(a));
rep(i, -H, H) {
ss << endl;
ss << out_m2(a[i], W, U, i);
}
return ss.str();
}
/*@formatter:off*/
template <class T> string out_m2(T &a) {
stringstream ss;
ss << a;
return ss.str();
}
/*@formatter:on*/
template <class T> string out_m(vector<T> &a, ll W = inf) {
stringstream ss;
if (W == inf)
W = min(sz(a), 12ll);
if (sz(a) == 0)
return ss.str();
rep(i, W) { ss << a[i] << " "; }
ss << "" << endl;
return ss.str();
}
/*@formatter:off*/
template <class T>
string out_m(vector<vector<T>> &a, ll H = inf, ll W = inf, int key = -1) {
H = min({H, sz(a), 12ll});
W = min({W, sz(a[0]), 12ll});
stringstream ss;
ss << endl;
if (key == -1)
ss << " *|";
else
ss << " " << key << "|";
rep(w, W) ss << std::right << std::setw(4) << w;
ss << "" << endl;
rep(w, W * 4 + 3) ss << "_";
ss << "" << endl;
rep(h, H) {
ss << std::right << std::setw(2) << h << "|";
rep(w, min(sz(a[h]), 12ll)) {
if (abs(a[h][w]) == linf)
ss << " e"
<< "";
else
ss << std::right << std::setw(4) << a[h][w];
}
ss << "" << endl;
}
ss << endl;
return ss.str();
}
template <class T>
string out_m(vector<vector<vector<T>>> &a, ll H = inf, ll W = inf, ll U = inf) {
stringstream ss;
if (H == inf)
H = 5;
H = min(H, sz(a));
rep(i, H) {
ss << endl;
ss << out_m(a[i], W, U, i);
}
ss << endl;
return ss.str();
}
string out_m(int a) {
stringstream ss;
ss << a << endl;
return ss.str();
}
template <class T> string out_m(T &a) {
stringstream ss;
ss << a << endl;
return ss.str();
}
template <class T> void outv(vector<T> &a, ll W = inf) {
cout << out_m(a, W) << endl;
}
template <class T>
void outv(vector<vector<T>> &a, ll H = linf, ll W = linf, int key = -1) {
cout << out_m(a, H, W, key) << endl;
}
template <class T>
void outv(vector<vector<vector<T>>> &a, ll H = linf, ll W = linf, ll U = linf) {
cout << out_m(a, H, W, U) << endl;
}
#else
template <class T> void outv(vector<T> &a, ll W = inf) {
rep(i, min(W, sz(a))) { cout << a[i] << " "; }
cout << "" << endl;
}
template <class T>
void outv(vector<vector<T>> &a, ll H = linf, ll W = linf, int key = -1) {
rep(i, min(H, sz(a))) { outv(a[i], W); }
}
template <class T>
void outv(vector<vector<vector<T>>> &a, ll H = linf, ll W = linf, ll U = linf) {
;
}
#define err(...) ;
#endif
template <class T> void outl(vector<T> &a, int n = inf) {
rep(i, min(n, sz(a))) cout << a[i] << endl;
}
// テーブルをスペースなしで出力
template <class T> void outt(vector<vector<T>> &a) {
rep(i, sz(a)) {
rep(j, sz(a[i])) { cout << a[i][j]; }
cout << endl;
}
}
// int型をbit表記で出力
void outb(int a) { cout << bitset<20>(a) << endl; }
template <class T> void na(vector<T> &a, ll n) {
a.resize(n);
rep(i, n) cin >> a[i];
}
#define dna(a, n) \
vi a(n); \
rep(dnai, n) cin >> a[dnai];
#define dnad(a, n) \
vi a(n); \
rep(dnai, n) cin >> a[dnai], a[dnai]--;
template <class T> void nao(vector<T> &a, ll n) {
a.resize(n + 1);
a[0] = 0;
rep(i, n) cin >> a[i + 1];
}
template <class T> void naod(vector<T> &a, ll n) {
a.resize(n + 1);
a[0] = 0;
rep(i, n) cin >> a[i + 1], a[i + 1]--;
}
template <class T> void nad(vector<T> &a, ll n) {
a.resize(n);
rep(i, n) cin >> a[i], a[i]--;
}
template <class T, class U> void na2(vector<T> &a, vector<U> &b, ll n) {
a.resize(n);
b.resize(n);
rep(i, n) cin >> a[i] >> b[i];
}
#define dna2(a, b, n) \
vi a(n), b(n); \
rep(dna2i, n) cin >> a[dna2i] >> b[dna2i];
template <class T, class U> void nao2(vector<T> &a, vector<U> &b, ll n) {
a.resize(n + 1);
b.resize(n + 1);
a[0] = b[0] = 0;
rep(i, n) cin >> a[i + 1] >> b[i + 1];
}
#define dna2d(a, b, n) \
vi a(n), b(n); \
rep(dna2di, n) { \
cin >> a[dna2di] >> b[dna2di]; \
a[dna2di]--, b[dna2di]--; \
}
template <class T, class U> void na2d(vector<T> &a, vector<U> &b, ll n) {
a.resize(n);
b.resize(n);
rep(i, n) cin >> a[i] >> b[i], a[i]--, b[i]--;
}
template <class T, class U, class W>
void na3(vector<T> &a, vector<U> &b, vector<W> &c, ll n) {
a.resize(n);
b.resize(n);
c.resize(n);
rep(i, n) cin >> a[i] >> b[i] >> c[i];
}
#define dna3(a, b, c, n) \
vi a(n), b(n), c(n); \
rep(dna3i, n) cin >> a[dna3i] >> b[dna3i] >> c[dna3i];
template <class T, class U, class W>
void na3d(vector<T> &a, vector<U> &b, vector<W> &c, ll n) {
a.resize(n);
b.resize(n);
c.resize(n);
rep(i, n) cin >> a[i] >> b[i] >> c[i], a[i]--, b[i]--, c[i]--;
}
#define dna3d(a, b, c, n) \
vi a(n), b(n), c(n); \
rep(dna3di, n) { \
cin >> a[dna3di] >> b[dna3di] >> c[dna3di]; \
a[dna3di]--, b[dna3di]--, c[dna3di]--; \
}
template <class T, class U, class W, class X>
void na4(vector<T> &a, vector<U> &b, vector<W> &c, vector<X> &d, ll n) {
a.resize(n);
b.resize(n);
c.resize(n);
d.resize(n);
rep(i, n) cin >> a[i] >> b[i] >> c[i] >> d[i];
}
#define dna4(a, b, c, d, n) \
vi a(n), b(n), c(n), d(n); \
rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i];
#define dna4d(a, b, c, d, n) \
vi a(n), b(n), c(n), d(n); \
rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i], \
--a[dna4i], --b[dna4i], --c[dna4i], --d[dna4i];
#define nt(a, h, w) \
resize(a, h, w); \
rep(nthi, h) rep(ntwi, w) cin >> a[nthi][ntwi];
#define ntd(a, h, w) \
resize(a, h, w); \
rep(ntdhi, h) rep(ntdwi, w) cin >> a[ntdhi][ntdwi], a[ntdhi][ntdwi]--;
#define ntp(a, h, w) \
resize(a, h + 2, w + 2); \
fill(a, '#'); \
rep(ntphi, 1, h + 1) rep(ntpwi, 1, w + 1) cin >> a[ntphi][ntpwi];
// デバッグ
#define sp << " " <<
#define deb1(x) debugName(x) << " = " << out_m2(x)
#define deb2(x, ...) deb1(x) << ", " << deb1(__VA_ARGS__)
#define deb3(x, ...) deb1(x) << ", " << deb2(__VA_ARGS__)
#define deb4(x, ...) deb1(x) << ", " << deb3(__VA_ARGS__)
#define deb5(x, ...) deb1(x) << ", " << deb4(__VA_ARGS__)
#define deb6(x, ...) deb1(x) << ", " << deb5(__VA_ARGS__)
#define deb7(x, ...) deb1(x) << ", " << deb6(__VA_ARGS__)
#define deb8(x, ...) deb1(x) << ", " << deb7(__VA_ARGS__)
#define deb9(x, ...) deb1(x) << ", " << deb8(__VA_ARGS__)
#define deb10(x, ...) deb1(x) << ", " << deb9(__VA_ARGS__)
#define o_ebug(o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, name, ...) name
#ifdef _DEBUG
#define deb(...) \
cerr << o_ebug(__VA_ARGS__, deb10, deb9, deb8, deb7, deb6, deb5, deb4, deb3, \
deb2, deb1)(__VA_ARGS__) \
<< endl
#else
#define deb(...) ;
#endif
#define debugline(x) \
cerr << x << " " \
<< "(L:" << __LINE__ << ")" << '\n'
//@formatter:off
// よく使うクラス、構造体
// graphでredefineしているため、書き換えたら向こうも書き換える
struct unionfind {
vector<ll> par;
vector<ll> siz;
vector<ll> es;
ll n, trees; // 連結グループの数(親の種類)
unionfind(ll n) : n(n), trees(n) {
par.resize(n);
siz.resize(n);
es.resize(n);
for (ll i = 0; i < n; i++) {
par[i] = i;
siz[i] = 1;
}
}
ll root(ll x) {
if (par[x] == x) {
return x;
} else {
return par[x] = root(par[x]);
}
}
ll operator()(ll x) { return root(x); }
bool unite(ll x, ll y) {
x = root(x);
y = root(y);
es[x]++;
if (x == y)
return false;
if (siz[x] > siz[y])
swap(x, y);
trees--;
par[x] = y;
siz[y] += siz[x];
es[y] += es[x];
return true;
}
bool same(ll x, ll y) { return root(x) == root(y); }
ll size(ll x) { return siz[root(x)]; }
ll esize(ll x) { return es[root(x)]; }
vi sizes() {
vi cou(n);
vi ret;
ret.reserve(n);
rep(i, n) { cou[root(i)]++; }
rep(i, n) {
if (cou[i])
ret.push_back(cou[i]);
}
return ret;
}
// つながりを無向グラフと見なし、xが閉路に含まれるか判定
bool close(ll x) { return esize(x) >= size(x); }
vector<vi> sets() {
vi ind(n, -1);
ll i = 0;
vvi(res, trees);
rep(j, n) {
ll r = root(j);
if (ind[r] == -1)
ind[r] = i++;
res[ind[r]].push_back(j);
}
rep(i, trees) {
ll r = root(res[i][0]);
if (res[i][0] == r)
continue;
rep(j, 1, sz(res[i])) {
if (res[i][j] == r) {
swap(res[i][0], res[i][j]);
break;
}
}
}
return res;
}
}; //@formatter:off
using bll = __int128;
using u32 = unsigned;
using u64 = unsigned long long;
using u128 = __uint128_t;
std::ostream &operator<<(std::ostream &dest, __int128_t value) {
std::ostream::sentry s(dest);
if (s) {
__uint128_t tmp = value < 0 ? -value : value;
char buffer[128];
char *d = std::end(buffer);
do {
--d;
*d = "0123456789"[tmp % 10];
tmp /= 10;
} while (tmp != 0);
if (value < 0) {
--d;
*d = '-';
}
ll len = std::end(buffer) - d;
if (dest.rdbuf()->sputn(d, len) != len) {
dest.setstate(std::ios_base::badbit);
}
}
return dest;
}
//__int128 toi128(string &s) { __int128 ret = 0; for (ll i = 0; i <
//s.length(); ++i) if ('0' <= s[i] && s[i] <= '9') ret = 10 *
//ret + s[i] - '0'; return ret;}
// エラー
void ole() {
#ifdef _DEBUG
debugline("ole");
exit(0);
#endif
string a = "a";
rep(i, 30) a += a;
rep(i, 1 << 17) cout << a << endl;
cout << "OLE 出力長制限超過" << endl;
exit(0);
}
void re() {
assert(0 == 1);
exit(0);
}
void tle() {
while (inf)
cout << inf << endl;
}
// 便利関数
// テスト用
#define rand xor128_
unsigned long xor128_(void) {
static unsigned long x = 123456789, y = 362436069, z = 521288629,
w = 88675123;
unsigned long t;
t = (x ^ (x << 11));
x = y;
y = z;
z = w;
return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}
char ranc() { return (char)('a' + rand() % 26); }
ll rand(ll min, ll max) {
assert(min <= max);
if (min >= 0 && max >= 0) {
return rand() % (max + 1 - min) + min;
} else if (max < 0) {
return -rand(-max, -min);
} else {
if (rand() % 2) {
return rand(0, max);
} else {
return -rand(0, -min);
}
}
}
vi ranv(ll n, ll min, ll max) {
vi v(n);
rep(i, n) v[i] = rand(min, max);
return v;
}
str ransu(ll n) {
str s;
rep(i, n) s += (char)rand('A', 'Z');
return s;
}
str ransl(ll n) {
str s;
rep(i, n) s += (char)rand('a', 'z');
return s;
}
// 単調増加
vi ranvinc(ll n, ll min, ll max) {
vi v(n);
bool bad = 1;
while (bad) {
bad = 0;
v.resize(n);
rep(i, n) {
if (i && min > max - v[i - 1]) {
bad = 1;
break;
}
if (i)
v[i] = v[i - 1] + rand(min, max - v[i - 1]);
else
v[i] = rand(min, max);
}
}
return v;
}
// 便利 汎用
void ranvlr(ll n, ll min, ll max, vi &l, vi &r) {
l.resize(n);
r.resize(n);
rep(i, n) {
l[i] = rand(min, max);
r[i] = l[i] + rand(0, max - l[i]);
}
}
template <class T> vector<pair<T, int>> run_length(vector<T> &a) {
vector<pair<T, int>> ret;
ret.eb(a[0], 1);
rep(i, 1, sz(a)) {
if (ret.back().fi == a[i]) {
ret.back().se++;
} else {
ret.eb(a[i], 1);
}
}
return ret;
}
vector<pair<char, ll>> run_length(string &a) {
vector<pair<char, ll>> ret;
ret.eb(a[0], 1);
rep(i, 1, sz(a)) {
if (ret.back().fi == a[i]) {
ret.back().se++;
} else {
ret.eb(a[i], 1);
}
}
return ret;
}
template <class F> ll mgr(ll ok, ll ng, F f) {
bool han = true;
if (ok < ng)
while (ng - ok > 1) {
ll mid = (ok + ng) >> 1;
if (f(mid))
ok = mid, han = true;
else
ng = mid, han = false;
deb(mid, han);
}
else
while (ok - ng > 1) {
ll mid = (ok + ng) >> 1;
if (f(mid))
ok = mid, han = true;
else
ng = mid, han = false;
deb(mid, han);
}
return ok;
}
template <class F> dou mgrd(dou ok, dou ng, F f) {
bool han = true;
if (ok < ng)
rep(i, 100) {
dou mid = (ok + ng) / 2;
if (f(mid))
ok = mid, han = true;
else
ng = mid, han = false;
if (i < 30)
deb(mid, han);
}
else
rep(i, 100) {
dou mid = (ok + ng) / 2;
if (f(mid))
ok = mid, han = true;
else
ng = mid, han = false;
if (i < 30)
deb(mid, han);
}
return ok;
}
// strを整数として比較
string smax(str &a, str b) {
if (sz(a) < sz(b)) {
return b;
} else if (sz(a) > sz(b)) {
return a;
} else if (a < b)
return b;
else
return a;
}
// strを整数として比較
string smin(str &a, str b) {
if (sz(a) > sz(b)) {
return b;
} else if (sz(a) < sz(b)) {
return a;
} else if (a > b)
return b;
else
return a;
}
// エラー-1
template <typename W, typename T> ll find(vector<W> &a, int l, const T key) {
rep(i, l, sz(a)) if (a[i] == key) return i;
return -1;
}
template <typename W, typename T> ll find(vector<W> &a, const T key) {
rep(i, sz(a)) if (a[i] == key) return i;
return -1;
}
template <typename W, typename T> P find(vector<vector<W>> &a, const T key) {
rep(i, sz(a)) rep(j, sz(a[0])) if (a[i][j] == key) return mp(i, j);
return mp(-1, -1);
}
template <typename W, typename U>
T find(vector<vector<vector<W>>> &a, const U key) {
rep(i, sz(a)) rep(j, sz(a[0]))
rep(k, sz(a[0][0])) if (a[i][j][k] == key) return mt(i, j, k);
return mt(-1, -1, -1);
}
// stringも書く
int find(string &s, const string key) {
int klen = sz(key);
rep(i, sz(s) - klen + 1) {
if (s[i] != key[0])
continue;
if (s.substr(i, klen) == key) {
return i;
}
}
return -1;
}
int find(string &s, int l, const string key) {
int klen = sz(key);
rep(i, l, sz(s) - klen + 1) {
if (s[i] != key[0])
continue;
if (s.substr(i, klen) == key) {
return i;
}
}
return -1;
}
int find(string &s, const char key) {
rep(i, sz(s)) {
if (s[i] == key)
return i;
}
return -1;
}
int find(string &s, int l, const char key) {
rep(i, l, sz(s)) {
if (s[i] == key)
return i;
}
return -1;
}
template <typename W, typename T> ll count2(W &a, const T k) { return a == k; }
template <typename W, typename T> ll count2(vector<W> &a, const T k) {
ll ret = 0;
fora(v, a) ret += count2(v, k);
return ret;
}
template <typename W, typename T> ll count(vector<W> &a, const T k) {
ll ret = 0;
fora(v, a) ret += count2(v, k);
return ret;
}
vi count(vi &a) {
int ma = 0;
fora(v, a) {
if (ma < v)
ma = v;
}
vi res(ma + 1);
fora(v, a) { res[v]++; }
return res;
}
ll count(str &a, str k) {
ll ret = 0, len = k.length();
auto pos = a.find(k);
while (pos != string::npos)
pos = a.find(k, pos + len), ++ret;
return ret;
}
vi count(str &a) {
vi cou(26);
char c = 'a';
if ('A' <= a[0] && a[0] <= 'Z')
c = 'A';
rep(i, sz(a))++ cou[a[i] - c];
return cou;
}
#define couif count_if
// algorythm
ll rev(ll a) {
ll res = 0;
while (a) {
res *= 10;
res += a % 10;
a /= 10;
}
return res;
}
template <class T> void rev(vector<T> &a) { reverse(all(a)); }
template <class U> void rev(vector<vector<U>> &a) {
vector<vector<U>> b(sz(a[0]), vector<U>(sz(a)));
rep(h, sz(a)) rep(w, sz(a[0])) b[w][h] = a[h][w];
a = b;
}
void rev(string &a) { reverse(all(a)); }
constexpr ll p10[] = {1,
10,
100,
1000,
10000,
100000,
1000000,
10000000,
100000000,
1000000000,
10000000000ll,
100000000000ll,
1000000000000ll,
10000000000000ll,
100000000000000ll,
1000000000000000ll,
10000000000000000ll,
100000000000000000ll,
1000000000000000000ll};
ll get(ll a, ll keta) { return (a / (ll)pow(10, keta)) % 10; }
// 0は0桁
ll keta(ll v) {
if (v < p10[9]) {
if (v < p10[4]) {
if (v < p10[2]) {
if (v < p10[1]) {
if (v < p10[0])
return 0;
else
return 1;
} else
return 2;
} else {
if (v < p10[3])
return 3;
else
return 4;
}
} else {
if (v < p10[7]) {
if (v < p10[5])
return 5;
else if (v < p10[6])
return 6;
else
return 7;
} else {
if (v < p10[8])
return 8;
else
return 9;
}
}
} else {
if (v < p10[13]) {
if (v < p10[11]) {
if (v < p10[10])
return 10;
else
return 11;
} else {
if (v < p10[12])
return 12;
else
return 13;
}
} else {
if (v < p10[15]) {
if (v < p10[14])
return 14;
else
return 15;
} else {
if (v < p10[17]) {
if (v < p10[16])
return 16;
else
return 17;
} else {
if (v < p10[18])
return 18;
else
return 19;
}
}
}
}
}
ll dsum(ll v, ll sin = 10) {
ll ret = 0;
for (; v; v /= sin)
ret += v % sin;
return ret;
}
ll mask10(ll v) { return p10[v] - 1; }
// 変換系
//[v] := iとなるようなvectorを返す
// 存在しない物は-1
template <class T> auto keys(T &a) {
vector<decltype((a.begin())->fi)> res;
for (auto &&k : a)
res.push_back(k.fi);
return res;
}
template <class T> auto values(T &a) {
vector<decltype((a.begin())->se)> res;
for (auto &&k : a)
res.push_back(k.se);
return res;
}
template <class T, class U> bool chma(T &a, const U &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T, class U> bool chmi(T &a, const U &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template <class T> T min(T a, signed b) { return a < b ? a : b; }
template <class T> T max(T a, signed b) { return a < b ? b : a; }
template <class T> T min(T a, T b, T c) {
return a >= b ? b >= c ? c : b : a >= c ? c : a;
}
template <class T> T max(T a, T b, T c) {
return a <= b ? b <= c ? c : b : a <= c ? c : a;
}
template <class T> T min(vector<T> &a) { return *min_element(all(a)); }
template <class T> T mini(vector<T> &a) {
return min_element(all(a)) - a.begin();
}
template <class T> T min(vector<T> &a, ll n) {
return *min_element(a.begin(), a.begin() + min(n, sz(a)));
}
template <class T> T min(vector<T> &a, ll s, ll n) {
return *min_element(a.begin() + s, a.begin() + min(n, sz(a)));
}
template <class T> T max(vector<T> &a) { return *max_element(all(a)); }
template <class T, class U> T max(vector<T> &a, vector<U> &b) {
return max(*max_element(all(a)), *max_element(all(b)));
}
template <class T> T maxi(vector<T> &a) {
return max_element(all(a)) - a.begin();
}
template <class T> T max(vector<T> &a, ll n) {
return *max_element(a.begin(), a.begin() + min(n, sz(a)));
}
template <class T> T max(vector<T> &a, ll s, ll n) {
return *max_element(a.begin() + s, a.begin() + min(n, sz(a)));
}
template <typename A, size_t N> A max(A (&a)[N]) {
A res = a[0];
rep(i, N) res = max(res, a[i]);
return res;
}
template <typename A, size_t N, size_t O> A max(A (&a)[N][O]) {
A res = max(a[0]);
rep(i, N) res = max(res, max(a[i]));
return res;
}
template <typename A, size_t N, size_t O, size_t P> A max(A (&a)[N][O][P]) {
A res = max(a[0]);
rep(i, N) res = max(res, max(a[i]));
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q>
A max(A (&a)[N][O][P][Q], const T &v) {
A res = max(a[0]);
rep(i, N) res = max(res, max(a[i]));
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R>
A max(A (&a)[N][O][P][Q][R]) {
A res = max(a[0]);
rep(i, N) res = max(res, max(a[i]));
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R,
size_t S>
A max(A (&a)[N][O][P][Q][R][S]) {
A res = max(a[0]);
rep(i, N) res = max(res, max(a[i]));
return res;
}
template <typename A, size_t N> A min(A (&a)[N]) {
A res = a[0];
rep(i, N) res = min(res, a[i]);
return res;
}
template <typename A, size_t N, size_t O> A min(A (&a)[N][O]) {
A res = min(a[0]);
rep(i, N) res = min(res, min(a[i]));
return res;
}
template <typename A, size_t N, size_t O, size_t P> A min(A (&a)[N][O][P]) {
A res = min(a[0]);
rep(i, N) res = min(res, min(a[i]));
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q>
A min(A (&a)[N][O][P][Q], const T &v) {
A res = min(a[0]);
rep(i, N) res = min(res, min(a[i]));
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R>
A min(A (&a)[N][O][P][Q][R]) {
A res = min(a[0]);
rep(i, N) res = min(res, min(a[i]));
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R,
size_t S>
A min(A (&a)[N][O][P][Q][R][S]) {
A res = min(a[0]);
rep(i, N) res = min(res, min(a[i]));
return res;
}
template <class T> T sum(vector<T> &v, ll s, ll t) {
T ret = 0;
rep(i, s, min(sz(v), t)) ret += v[i];
return ret;
}
template <class T> T sum(vector<T> &v, ll t = inf) { return sum(v, 0, t); }
template <class T> T sum(vector<vector<T>> &v, int s, int t) {
T ret = 0;
rep(i, s, min(sz(v), t)) ret += sum(v[i]);
return ret;
}
template <class T> T sum(vector<vector<T>> &v, int t = inf) {
return sum(v, 0, t);
}
template <class T> T sum(vector<vector<vector<T>>> &v, int s, int t) {
T ret = 0;
rep(i, s, min(sz(v), t)) ret += sum(v[i]);
return ret;
}
template <class T> T sum(vector<vector<vector<T>>> &v, int t = inf) {
return sum(v, 0, t);
}
template <class T> T sum(vector<vector<vector<vector<T>>>> &v, int s, int t) {
T ret = 0;
rep(i, s, min(sz(v), t)) ret += sum(v[i]);
return ret;
}
template <class T> T sum(vector<vector<vector<vector<T>>>> &v, int t = inf) {
return sum(v, 0, t);
}
template <class T>
T sum(vector<vector<vector<vector<vector<T>>>>> &v, int s, int t) {
T ret = 0;
rep(i, s, min(sz(v), t)) ret += sum(v[i]);
return ret;
}
template <class T>
T sum(vector<vector<vector<vector<vector<T>>>>> &v, int t = inf) {
return sum(v, 0, t);
}
template <class T> auto sum(priority_queue<T, vector<T>, greater<T>> &r) {
auto q = r;
T ret = 0;
while (sz(q)) {
ret += q.top();
q.pop();
}
return ret;
}
template <class T> auto sum(priority_queue<T> &r) {
auto q = r;
T ret = 0;
while (sz(q)) {
ret += q.top();
q.pop();
}
return ret;
}
// template<class T, class U, class... W> auto sumn(vector<T> &v, U head, W...
// tail) { auto ret = sum(v[0], tail...); rep(i, 1, min(sz(v), head))ret
// += sum(v[i], tail...); return ret;}
vi v_i(vi &a) {
int n = max(a) + 1;
vi ret(n, -1);
rep(i, sz(a)) { ret[a[i]] = i; }
return ret;
}
void clear(PQ &q) { q = PQ(); }
void clear(priority_queue<int> &q) { q = priority_queue<int>(); }
template <class T> void clear(queue<T> &q) {
while (q.size())
q.pop();
}
template <class T> T *negarr(ll size) {
T *body = (T *)malloc((size * 2 + 1) * sizeof(T));
return body + size;
}
template <class T> T *negarr2(ll h, ll w) {
double **dummy1 = new double *[2 * h + 1];
double *dummy2 = new double[(2 * h + 1) * (2 * w + 1)];
dummy1[0] = dummy2 + w;
for (ll i = 1; i <= 2 * h + 1; ++i) {
dummy1[i] = dummy1[i - 1] + 2 * w + 1;
}
double **a = dummy1 + h;
return a;
}
// imoは0-indexed
// ruiは1-indexed
template <class T> vector<T> imo(vector<T> &v) {
vector<T> ret = v;
rep(i, sz(ret) - 1) ret[i + 1] += ret[i];
return ret;
}
// kと同じものの数
template <class T, class U> vi imo(vector<T> &a, U k) {
vector<T> ret = a;
rep(i, sz(ret)) ret[i] = a[i] == k;
rep(i, sz(ret) - 1) ret[i + 1] += ret[i];
return ret;
}
template <class T> vector<T> imox(vector<T> &v) {
vector<T> ret = v;
rep(i, sz(ret) - 1) ret[i + 1] ^= ret[i];
return ret;
}
// 漸化的に最小を持つ
template <class T> vector<T> imi(vector<T> &v) {
vector<T> ret = v;
rep(i, sz(ret) - 1) chmi(ret[i + 1], ret[i]);
return ret;
}
template <class T> vector<T> ima(vector<T> &v) {
vector<T> ret = v;
rep(i, sz(ret) - 1) chma(ret[i + 1], ret[i]);
return ret;
}
// template<class T> vector<T> rimi(vector<T> &v) { vector<T> ret = v; rer(i,
// sz(ret) - 1, 1)chmi(ret[i - 1], ret[i]); return ret;} template<class T>
// vector<T> rima(vector<T> &v) { vector<T> ret = v; rer(i, sz(ret) - 1,
// 1)chma(ret[i - 1], ret[i]); return ret;}
template <class T> struct ruiC {
vector<T> rui;
ruiC(vector<T> &ru) : rui(ru) {}
/*先頭0*/
ruiC() : rui(1, 0) {}
T operator()(ll l, ll r) {
if (l > r) {
cerr << "ruic ";
deb(l, r);
assert(0);
}
return rui[r] - rui[l];
}
T operator()(int r) { return operator()(0, r); }
/*ruiv[]をruic[]に変えた際意味が変わるのがまずいため()と統一*/
/*単体iを返す 累積でないことに注意(seg木との統一でこうしている)*/
// T operator[](ll i) { return rui[i + 1] - rui[i]; }
T operator[](ll i) { return rui[i]; }
/*0から順に追加される必要がある*/
void operator+=(T v) { rui.push_back(rui.back() + v); }
void add(int i, T v) {
if (sz(rui) - 1 != i)
ole();
operator+=(v);
}
T back() { return rui.back(); }
ll size() { return rui.size(); }
auto begin() { return rui.begin(); }
auto end() { return rui.end(); }
};
template <class T> struct ruimax {
template <typename Monoid> struct SegmentTree { /*pairで処理*/
int sz;
vector<Monoid> seg;
const Monoid M1 = mp(MIN(T), -1);
Monoid f(Monoid a, Monoid b) { return max(a, b); }
void build(vector<T> &a) {
int n = sz(a);
sz = 1;
while (sz < n)
sz <<= 1;
seg.assign(2 * sz, M1);
rep(i, n) { seg[i + sz] = mp(a[i], i); }
for (int k = sz - 1; k > 0; k--) {
seg[k] = f(seg[k << 1], seg[(k << 1) | 1]);
}
}
Monoid query(int a, int b) {
Monoid L = M1, R = M1;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1)
L = f(L, seg[a++]);
if (b & 1)
R = f(seg[--b], R);
}
return f(L, R);
}
Monoid operator[](const int &k) const { return seg[k + sz]; }
};
private:
vector<T> ve;
SegmentTree<pair<T, int>> seg;
vector<T> rv;
vector<T> ri;
bool build = false;
public:
int n;
ruimax(vector<T> &a) : ve(a), n(sz(a)) {
int index = -1;
int ma = MIN(T);
rv.resize(n + 1);
ri.resize(n + 1);
rv[0] = -linf;
ri[0] = -1;
rep(i, n) {
if (chma(ma, a[i])) {
index = i;
}
rv[i + 1] = ma;
ri[i + 1] = index;
}
}
T operator()(int l, int r) {
if (!(l <= r && 0 <= l && r <= n)) {
deb(l, r, n);
assert(0);
}
if (l == 0) {
return rv[r];
} else {
if (!build)
seg.build(ve), build = true;
return seg.query(l, r).first;
}
}
T operator()(int r = inf) { return operator()(0, min(r, n)); }
T operator[](int r) { return operator()(0, r); }
T getv(int l, int r) { return operator()(l, r); }
T geti(int l, int r) {
assert(l <= r && 0 <= l && r <= n);
if (l == 0) {
return ri[r];
} else {
if (!build)
seg.build(ve), build = true;
return seg.query(l, r).second;
}
}
T geti(int r = inf) { return geti(0, min(r, n)); };
T getv(int r = inf) { return getv(0, min(r, n)); };
auto begin() { return rv.begin(); }
auto end() { return rv.end(); }
};
template <class T> struct ruimin {
template <typename Monoid> struct SegmentTree { /*pairで処理*/
int sz;
vector<Monoid> seg;
const Monoid M1 = mp(MAX(T), -1);
Monoid f(Monoid a, Monoid b) { return min(a, b); }
void build(vector<T> &a) {
int n = sz(a);
sz = 1;
while (sz < n)
sz <<= 1;
seg.assign(2 * sz, M1);
rep(i, n) { seg[i + sz] = mp(a[i], i); }
for (int k = sz - 1; k > 0; k--) {
seg[k] = f(seg[k << 1], seg[(k << 1) | 1]);
}
}
Monoid query(int a, int b) {
Monoid L = M1, R = M1;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1)
L = f(L, seg[a++]);
if (b & 1)
R = f(seg[--b], R);
}
return f(L, R);
}
Monoid operator[](const int &k) const { return seg[k + sz]; }
};
private:
vector<T> ve;
SegmentTree<pair<T, int>> seg;
vector<T> rv;
vector<T> ri;
bool build = false;
int n;
public:
ruimin(vector<T> &a) : ve(a), n(sz(a)) {
int index = -1;
int mi = MAX(T);
rv.resize(n + 1);
ri.resize(n + 1);
rv[0] = linf;
ri[0] = -1;
rep(i, n) {
if (chmi(mi, a[i])) {
index = i;
}
rv[i + 1] = mi;
ri[i + 1] = index;
}
}
T operator()(int l, int r) {
assert(l <= r && 0 <= l && r <= n);
if (l == 0) {
return rv[r];
} else {
if (!build)
seg.build(ve), build = true;
return seg.query(l, r).first;
}
}
T operator()(int r = inf) { return operator()(0, min(r, n)); }
T operator[](int r) { return operator()(0, r); }
T getv(int l, int r) { return operator()(l, r); }
T geti(int l, int r) {
assert(l <= r && 0 <= l && r <= n);
if (l == 0) {
return ri[r];
} else {
if (!build)
seg.build(ve), build = true;
return seg.query(l, r).second;
}
}
T geti(int r = inf) { return geti(0, min(r, n)); };
T getv(int r = inf) { return getv(0, min(r, n)); };
auto begin() { return rv.begin(); }
auto end() { return rv.end(); }
};
template <class T> ostream &operator<<(ostream &os, ruiC<T> a) {
fora(v, a.rui) os << v << " ";
return os;
}
template <class T> vector<T> ruiv(vector<T> &a) {
vector<T> ret(a.size() + 1);
rep(i, a.size()) ret[i + 1] = ret[i] + a[i];
return ret;
}
template <class T> ruiC<T> ruic() { return ruiC<T>(); }
template <class T> ruiC<T> ruic(vector<T> &a) {
vector<T> ret = ruiv(a);
return ruiC<T>(ret);
}
vvi() ruib(vi &a) {
vvi(res, 61, sz(a) + 1);
rep(k, 61) {
rep(i, sz(a)) { res[k][i + 1] = res[k][i] + ((a[i] >> k) & 1); }
}
return res;
}
vector<ruiC<int>> ruibc(vi &a) {
vector<ruiC<int>> ret(61);
vvi(res, 61, sz(a));
rep(k, 61) {
rep(i, sz(a)) { res[k][i] = (a[i] >> k) & 1; }
ret[k] = ruic(res[k]);
}
return ret;
}
vector<ll> ruiv(string &a) {
if (sz(a) == 0)
return vi(1);
ll dec = ('0' <= a[0] && a[0] <= '9') ? '0' : 0;
vector<ll> ret(a.size() + 1);
rep(i, a.size()) ret[i + 1] = ret[i] + a[i] - dec;
return ret;
}
ruiC<ll> ruic(string &a) {
vector<ll> ret = ruiv(a);
return ruiC<ll>(ret);
}
// kと同じものの数
template <class T, class U> vi ruiv(T &a, U k) {
vi ret(a.size() + 1);
rep(i, a.size()) ret[i + 1] = ret[i] + (a[i] == k);
return ret;
}
template <class T, class U> ruiC<ll> ruic(T &a, U k) {
vi ret = ruiv(a, k);
return ruiC<ll>(ret);
}
// h query
template <class T> vector<T> imoh(vector<vector<T>> &v, int w) {
vector<T> ret(sz(v));
rep(h, sz(ret)) { ret[h] = v[h][w]; }
rep(i, sz(ret) - 1) { ret[i + 1] += ret[i]; }
return ret;
}
template <class T> vector<T> ruih(vector<vector<T>> &v, int w) {
vector<T> ret(sz(v) + 1);
rep(h, sz(v)) { ret[h + 1] = v[h][w]; }
rep(i, sz(v)) { ret[i + 1] += ret[i]; }
return ret;
}
template <class T> ruiC<T> ruihc(vector<vector<T>> &a, int w) {
vector<T> ret = ruih(a, w);
return ruiC<T>(ret);
}
// xor
template <class T> struct ruixC {
const vector<T> rui;
ruixC(vector<T> &ru) : rui(ru) {}
T operator()(ll l, ll r) {
if (l > r) {
cerr << "ruiXc ";
deb(l, r);
assert(0);
}
return rui[r] ^ rui[l];
}
T operator[](ll i) { return rui[i]; }
T back() { return rui.back(); }
ll size() { return rui.size(); }
};
template <class T> vector<T> ruix(vector<T> &a) {
vector<T> ret(a.size() + 1);
rep(i, a.size()) ret[i + 1] = ret[i] ^ a[i];
return ret;
}
template <class T> ruixC<ll> ruixc(vector<T> &a) {
vi ret = ruix(a);
return ruixC<ll>(ret);
}
template <class T> vector<T> ruim(vector<T> &a) {
vector<T> res(a.size() + 1, 1);
rep(i, a.size()) res[i + 1] = res[i] * a[i];
return res;
}
// 漸化的に最小を1indexで持つ
template <class T> vector<T> ruimi(vector<T> &a) {
ll n = sz(a);
vector<T> ret(n + 1);
rep(i, 1, n) {
ret[i] = a[i - 1];
chmi(ret[i + 1], ret[i]);
}
return ret;
}
// template<class T> T *rrui(vector<T> &a) {
// 右から左にかけての半開区間 (-1 n-1]
template <class T> struct rruiC {
vector<T> rui;
int n;
rruiC(vector<T> &a) : n(sz(a)) {
rui.resize(n + 1);
rer(i, n - 1) { rui[i] = rui[i + 1] + a[i]; }
}
/*[r l)*/
T operator()(int r, int l) {
r++;
l++;
assert(l <= r && l >= 0 && r <= n);
return rui[l] - rui[r];
}
T operator()(int l) { return operator()(n - 1, l); }
T operator[](int i) { return operator()(i); }
};
template <class T> ostream &operator<<(ostream &os, rruiC<T> a) {
fora(v, a.rui) os << v << " ";
return os;
}
#define rrui rruic
template <class T> rruiC<T> rruic(vector<T> &a) { return rruiC<T>(a); }
// 掛け算
template <class T> struct ruimulC {
vector<T> rv;
int n;
ruimulC(vector<T> &a) : rv(a), n(sz(a)) {
rv.resize(n + 1);
rv[0] = 1;
rep(i, n) { rv[i + 1] = a[i] * rv[i]; }
}
ruimulC() : n(0) {
rv.resize(n + 1);
rv[0] = 1;
}
void operator+=(T v) {
rv.push_back(rv.back() * v);
n++;
}
T operator()(int l, int r) {
assert(l <= r && 0 <= l && r <= n);
return rv[r] / rv[l];
}
T operator()(int r = inf) { return operator()(0, min(r, n)); }
T operator[](int r) { return operator()(0, r); }
auto begin() { return rv.begin(); }
auto end() { return rv.end(); }
};
template <class T> ruimulC<T> ruimul(vector<T> &a) { return ruimulC<T>(a); }
template <class T> ruimulC<T> ruimul() {
vector<T> a;
return ruimulC<T>(a);
}
/*@formatter:off*/
template <class T> T *rruim(vector<T> &a) {
ll len = a.size();
T *body = (T *)malloc((len + 1) * sizeof(T));
T *res = body + 1;
res[len - 1] = 1;
rer(i, len - 1) res[i - 1] = res[i] * a[i];
return res;
}
template <class T, class U> void inc(pair<T, U> &a, U v = 1) {
a.first += v, a.second += v;
}
template <class T, class U> void inc(T &a, U v = 1) { a += v; }
template <class T, class U> void inc(vector<T> &a, U v = 1) {
for (auto &u : a)
inc(u, v);
}
template <class T, class U> void dec(T &a, U v = 1) { a -= v; }
template <class T, class U> void dec(vector<T> &a, U v = 1) {
for (auto &u : a)
dec(u, v);
}
template <class U> void dec(string &a, U v = 1) {
for (auto &u : a)
dec(u, v);
}
template <class T, class U, class W>
void dec(vector<T> &a, vector<U> &b, W v = 1) {
for (auto &u : a)
dec(u, v);
for (auto &u : b)
dec(u, v);
}
template <class T, class U, class W>
void dec(vector<T> &a, vector<U> &b, vector<W> &c) {
for (auto &u : a)
dec(u, 1);
for (auto &u : b)
dec(u, 1);
for (auto &u : c)
dec(u, 1);
}
bool ins(ll h, ll w, ll H, ll W) { return h >= 0 && w >= 0 && h < H && w < W; }
bool ins(ll l, ll v, ll r) { return l <= v && v < r; }
template <class T> bool ins(vector<T> &a, ll i, ll j = 0) {
return ins(0, i, sz(a)) && ins(0, j, sz(a));
}
#define inside ins
ll u(ll a) { return a < 0 ? 0 : a; }
template <class T> vector<T> u(const vector<T> &a) {
vector<T> ret = a;
fora(v, ret) v = u(v);
return ret;
}
// 添え字を返す
template <class F> ll goldd_l(ll left, ll right, F calc) {
double GRATIO = 1.6180339887498948482045868343656;
ll lm = left + (ll)((right - left) / (GRATIO + 1.0));
ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0));
ll fl = calc(lm);
ll fr = calc(rm);
while (right - left > 10) {
if (fl < fr) {
right = rm;
rm = lm;
fr = fl;
lm = left + (ll)((right - left) / (GRATIO + 1.0));
fl = calc(lm);
} else {
left = lm;
lm = rm;
fl = fr;
rm = lm + (ll)((right - lm) / (GRATIO + 1.0));
fr = calc(rm);
}
}
ll minScore = MAX(ll);
ll resIndex = left;
for (ll i = left; i < right + 1; ++i) {
ll score = calc(i);
if (minScore > score) {
minScore = score;
resIndex = i;
}
}
return resIndex;
}
template <class F> ll goldt_l(ll left, ll right, F calc) {
double GRATIO = 1.6180339887498948482045868343656;
ll lm = left + (ll)((right - left) / (GRATIO + 1.0));
ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0));
ll fl = calc(lm);
ll fr = calc(rm);
while (right - left > 10) {
if (fl > fr) {
right = rm;
rm = lm;
fr = fl;
lm = left + (ll)((right - left) / (GRATIO + 1.0));
fl = calc(lm);
} else {
left = lm;
lm = rm;
fl = fr;
rm = lm + (ll)((right - lm) / (GRATIO + 1.0));
fr = calc(rm);
}
}
if (left > right) {
ll l = left;
left = right;
right = l;
}
ll maxScore = MIN(ll);
ll resIndex = left;
for (ll i = left; i < right + 1; ++i) {
ll score = calc(i);
if (maxScore < score) {
maxScore = score;
resIndex = i;
}
}
return resIndex;
}
/*loopは200にすればおそらく大丈夫 余裕なら300に*/
template <class F> dou goldd_d(dou left, dou right, F calc, ll loop = 200) {
dou GRATIO = 1.6180339887498948482045868343656;
dou lm = left + ((right - left) / (GRATIO + 1.0));
dou rm = lm + ((right - lm) / (GRATIO + 1.0));
dou fl = calc(lm);
dou fr = calc(rm); /*200にすればおそらく大丈夫*/ /*余裕なら300に*/
ll k = 141;
loop++;
while (--loop) {
if (fl < fr) {
right = rm;
rm = lm;
fr = fl;
lm = left + ((right - left) / (GRATIO + 1.0));
fl = calc(lm);
} else {
left = lm;
lm = rm;
fl = fr;
rm = lm + ((right - lm) / (GRATIO + 1.0));
fr = calc(rm);
}
}
return left;
}
template <class F> dou goldt_d(dou left, dou right, F calc, ll loop = 200) {
double GRATIO = 1.6180339887498948482045868343656;
dou lm = left + ((right - left) / (GRATIO + 1.0));
dou rm = lm + ((right - lm) / (GRATIO + 1.0));
dou fl = calc(lm);
dou fr = calc(rm);
loop++;
while (--loop) {
if (fl > fr) {
right = rm;
rm = lm;
fr = fl;
lm = left + ((right - left) / (GRATIO + 1.0));
fl = calc(lm);
} else {
left = lm;
lm = rm;
fl = fr;
rm = lm + ((right - lm) / (GRATIO + 1.0));
fr = calc(rm);
}
}
return left;
}
// l ~ rを複数の区間に分割し、極致を与えるiを返す time-20 msまで探索
/*@formatter:on*/
template <class F> ll goldd_ls(ll l, ll r, F calc, ll time = 2000) {
auto lim = milliseconds(time - 20);
ll mini = 0, minv = MAX(ll); /*区間をk分割する*/
rep(k, 1, inf) {
auto s = system_clock::now();
ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/
ll nl = l;
ll nr = l + haba;
rep(i, k) {
ll ni = goldd_l(nl, nr, calc);
if (chmi(minv, calc(ni)))
mini = ni;
nl = nr;
nr = nl + haba;
}
auto end = system_clock::now();
auto part = duration_cast<milliseconds>(end - s);
auto elapsed = duration_cast<milliseconds>(end - start_time);
if (elapsed + part * 2 >= lim) {
break;
}
}
return mini;
}
/*@formatter:off*/
template <class F> ll goldt_ls(ll l, ll r, F calc, ll time = 2000) {
auto lim = milliseconds(time - 20);
ll maxi = 0, maxv = MIN(ll); /*区間をk分割する*/
rep(k, 1, inf) {
auto s = system_clock::now();
ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/
ll nl = l;
ll nr = l + haba;
rep(i, k) {
ll ni = goldt_l(nl, nr, calc);
if (chma(maxv, calc(ni)))
maxi = ni;
nl = nr;
nr = nl + haba;
}
auto end = system_clock::now();
auto part = duration_cast<milliseconds>(end - s);
auto elapsed = duration_cast<milliseconds>(end - start_time);
if (elapsed + part * 2 >= lim) {
break;
}
}
return maxi;
}
template <class F>
dou goldd_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を持つ*/
auto lim = milliseconds(time - 20);
dou mini = 0, minv = MAX(dou); /*区間をk分割する*/
rep(k, 1, inf) {
auto s = system_clock::now();
dou haba = (r - l) / k;
dou nl = l;
dou nr = l + haba;
rep(i, k) {
dou ni = goldd_d(nl, nr, calc);
if (chmi(minv, calc(ni)))
mini = ni;
nl = nr;
nr = nl + haba;
}
auto end = system_clock::now();
auto part = duration_cast<milliseconds>(end - s);
auto elapsed = duration_cast<milliseconds>(end - start_time);
if (elapsed + part * 2 >= lim) {
break;
}
}
return mini;
}
template <class F>
dou goldt_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を残している*/
auto lim = milliseconds(time - 20);
dou maxi = 0, maxv = MIN(dou); /*区間をk分割する*/
rep(k, 1, inf) {
auto s = system_clock::now();
dou haba = (r - l) / k;
dou nl = l;
dou nr = l + haba;
rep(i, k) {
dou ni = goldt_d(nl, nr, calc);
if (chma(maxv, calc(ni)))
maxi = ni;
nl = nr;
nr = nl + haba;
}
auto end = system_clock::now();
auto part = duration_cast<milliseconds>(end - s);
auto elapsed = duration_cast<milliseconds>(end - start_time);
if (elapsed + part * 2 >= lim) {
break;
}
}
return maxi;
}
template <class T> T min(vector<vector<T>> &a) {
T res = MAX(T);
rep(i, a.size()) chmi(res, *min_element(all(a[i])));
return res;
}
template <class T> T max(vector<vector<T>> &a) {
T res = MIN(T);
rep(i, a.size()) chma(res, *max_element(all(a[i])));
return res;
}
constexpr bool bget(ll m, ll keta) {
#ifdef _DEBUG
assert(keta <= 62); // オーバーフロー 1^62までしか扱えない
#endif
return (m >> keta) & 1;
}
ll bget(ll m, ll keta, ll sinsuu) {
m /= (ll)pow(sinsuu, keta);
return m % sinsuu;
}
constexpr ll bit(ll n) {
#ifdef _DEBUG
assert(n <= 62); // オーバーフロー 1^62までしか扱えない
#endif
return (1LL << (n));
}
ll bit(ll n, ll sinsuu) { return (ll)pow(sinsuu, n); }
ll mask(ll n) { return (1ll << n) - 1; }
// aをbitに置きなおす
//{0, 2} -> 101
ll biti(vi &a) {
int m = 0;
for (auto &&v : a)
m |= bit(v);
return m;
}
//{1, 1, 0} -> 011
ll bitb(vi &a) {
int m = 0;
rep(i, sz(a)) if (a[i]) m |= bit(i);
return m;
}
#define bcou __builtin_popcountll
// 最下位ビット
ll lbit(ll n) { return n & -n; }
ll lbiti(ll n) { return log2(n & -n); }
// 最上位ビット
ll hbit(ll n) {
n |= (n >> 1);
n |= (n >> 2);
n |= (n >> 4);
n |= (n >> 8);
n |= (n >> 16);
n |= (n >> 32);
return n - (n >> 1);
}
ll hbiti(ll n) { return log2(hbit(n)); }
ll hbitk(ll n) {
ll k = 0;
rer(i, 5) {
ll a = k + (1ll << i);
ll b = 1ll << a;
if (b <= n)
k += 1ll << i;
}
return k;
}
// 初期化は0を渡す
ll nextComb(ll &mask, ll n, ll r) {
if (!mask)
return mask = (1LL << r) - 1;
ll x = mask & -mask; /*最下位の1*/
ll y = mask + x; /*連続した下の1を繰り上がらせる*/
ll res = ((mask & ~y) / x >> 1) | y;
if (bget(res, n))
return mask = 0;
else
return mask = res;
}
// n桁以下でビットがr個立っているもののvectorを返す
vi bitCombList(ll n, ll r) {
vi res;
ll m = 0;
while (nextComb(m, n, r)) {
res.push_back(m);
}
return res;
}
// masの立ってるindexを見る
#define forbit(i, mas) \
for (int forbitj = lbit(mas), forbitm = mas, i = log2(forbitj); forbitm; \
forbitm = forbitj ? forbitm ^ forbitj : 0, forbitj = lbit(forbitm), \
i = log2(forbitj))
char itoal(ll i) { return 'a' + i; }
char itoaL(ll i) { return 'A' + i; }
ll altoi(char c) {
if ('A' <= c && c <= 'Z')
return c - 'A';
return c - 'a';
}
ll ctoi(char c) { return c - '0'; }
char itoc(ll i) { return i + '0'; }
ll vtoi(vi &v) {
ll res = 0;
if (sz(v) > 18) {
debugline("vtoi");
deb(sz(v));
ole();
}
rep(i, sz(v)) {
res *= 10;
res += v[i];
}
return res;
}
vi itov(ll i) {
vi res;
while (i) {
res.push_back(i % 10);
i /= 10;
}
rev(res);
return res;
}
vi stov(string &a) {
ll n = sz(a);
vi ret(n);
rep(i, n) { ret[i] = a[i] - '0'; }
return ret;
}
// 基準を満たさないものは0になる
vi stov(string &a, char one) {
ll n = sz(a);
vi ret(n);
rep(i, n) ret[i] = a[i] == one;
return ret;
}
vector<vector<ll>> ctoi(vector<vector<char>> s, char c) {
ll n = sz(s), m = sz(s[0]);
vector<vector<ll>> res(n, vector<ll>(m));
rep(i, n) rep(j, m) res[i][j] = s[i][j] == c;
return res;
}
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());
//[i] := vを返す
// aは0~n-1で置き換えられる
vi compress(vi &a) {
vi b;
ll len = a.size();
for (ll i = 0; i < len; ++i) {
b.push_back(a[i]);
}
sort(b);
unique(b);
for (ll i = 0; i < len; ++i) {
a[i] = lower_bound(all(b), a[i]) - b.begin();
}
ll blen = sz(b);
vi ret(blen);
rep(i, blen) { ret[i] = b[i]; }
return ret;
}
vi compress(vi &a, umap<ll, ll> &map) {
vi b;
ll len = a.size();
for (ll i = 0; i < len; ++i) {
b.push_back(a[i]);
}
sort(b);
unique(b);
for (ll i = 0; i < len; ++i) {
ll v = a[i];
a[i] = lower_bound(all(b), a[i]) - b.begin();
map[v] = a[i];
}
ll blen = sz(b);
vi ret(blen);
rep(i, blen) { ret[i] = b[i]; }
return ret;
}
vi compress(vi &a, vi &r) {
vi b;
ll len = a.size();
fora(v, a) b.push_back(v);
fora(v, r) b.push_back(v);
sort(b);
unique(b);
for (ll i = 0; i < len; ++i)
a[i] = lower_bound(all(b), a[i]) - b.begin();
for (ll i = 0; i < sz(r); ++i)
r[i] = lower_bound(all(b), r[i]) - b.begin();
ll blen = sz(b);
vi ret(blen);
rep(i, blen) { ret[i] = b[i]; }
return ret;
}
vi compress(vi &a, vi &r, vi &s) {
vi b;
ll len = a.size();
fora(v, a) b.push_back(v);
fora(v, r) b.push_back(v);
fora(v, s) b.push_back(v);
sort(b);
unique(b);
for (ll i = 0; i < len; ++i)
a[i] = lower_bound(all(b), a[i]) - b.begin();
for (ll i = 0; i < sz(r); ++i)
r[i] = lower_bound(all(b), r[i]) - b.begin();
for (ll i = 0; i < sz(s); ++i)
r[i] = lower_bound(all(b), s[i]) - b.begin();
ll blen = sz(b);
vi ret(blen);
rep(i, blen) { ret[i] = b[i]; }
return ret;
}
vi compress(vector<vi> &a) {
vi b;
fora(vv, a) fora(v, vv) b.push_back(v);
sort(b);
unique(b);
fora(vv, a) fora(v, vv) v = lower_bound(all(b), v) - b.begin();
ll blen = sz(b);
vi ret(blen);
rep(i, blen) { ret[i] = b[i]; }
return ret;
}
vi compress(vector<vector<vi>> &a) {
vi b;
fora(vvv, a) fora(vv, vvv) fora(v, vv) b.push_back(v);
sort(b);
unique(b);
fora(vvv, a) fora(vv, vvv) fora(v, vv) v = lower_bound(all(b), v) - b.begin();
ll blen = sz(b);
vi ret(blen);
rep(i, blen) { ret[i] = b[i]; }
return ret;
}
void compress(ll a[], ll len) {
vi b;
for (ll i = 0; i < len; ++i) {
b.push_back(a[i]);
}
sort(b);
unique(b);
for (ll i = 0; i < len; ++i) {
a[i] = lower_bound(all(b), a[i]) - b.begin();
}
}
// 要素が見つからなかったときに困る
#define binarySearch(a, v) (binary_search(all(a), v))
#define lowerIndex(a, v) (lower_bound(all(a), v) - a.begin())
#define upperIndex(a, v) (upper_bound(all(a), v) - a.begin())
#define rlowerIndex(a, v) (upper_bound(all(a), v) - a.begin() - 1)
#define rupperIndex(a, v) (lower_bound(all(a), v) - a.begin() - 1)
template <class T, class U, class W> T lowerBound(vector<T> &a, U v, W banpei) {
auto it = lower_bound(a.begin(), a.end(), v);
if (it == a.end())
return banpei;
else
return *it;
}
template <class T, class U, class W> T lowerBound(ruiC<T> &a, U v, W banpei) {
return lowerBound(a.rui, v, banpei);
}
template <class T, class U, class W> T upperBound(vector<T> &a, U v, W banpei) {
auto it = upper_bound(a.begin(), a.end(), v);
if (it == a.end())
return banpei;
else
return *it;
}
template <class T, class U, class W> T upperBound(ruiC<T> &a, U v, W banpei) {
return upperBound(a.rui, v, banpei);
}
template <class T, class U, class W>
T rlowerBound(vector<T> &a, U v, W banpei) {
auto it = upper_bound(a.begin(), a.end(), v);
if (it == a.begin())
return banpei;
else {
return *(--it);
}
}
template <class T, class U, class W> T rlowerBound(ruiC<T> &a, U v, W banpei) {
return rlowerBound(a.rui, v, banpei);
}
template <class T, class U, class W>
T rupperBound(vector<T> &a, U v, W banpei) {
auto it = lower_bound(a.begin(), a.end(), v);
if (it == a.begin())
return banpei;
else {
return *(--it);
}
}
template <class T, class U, class W> T rupperBound(ruiC<T> &a, U v, W banpei) {
return rupperBound(a.rui, v, banpei);
}
#define next2(a) next(next(a))
#define prev2(a) prev(prev(a))
// 狭義の単調増加列 長さを返す
template <class T> int lis(vector<T> &a) {
int n = sz(a);
vi tail(n + 1, MAX(T));
rep(i, n) {
int id = lowerIndex(tail, a[i]); /**/
tail[id] = a[i];
}
return lowerIndex(tail, MAX(T));
}
template <class T> int lis_eq(vector<T> &a) {
int n = sz(a);
vi tail(n + 1, MAX(T));
rep(i, n) {
int id = upperIndex(tail, a[i]); /**/
tail[id] = a[i];
}
return lowerIndex(tail, MAX(T));
}
// iteratorを返す
// valueが1以上の物を返す 0は見つけ次第削除
// vを減らす場合 (*it).se--でいい
template <class T, class U, class V> auto lower_map(map<T, U> &m, V k) {
auto ret = m.lower_bound(k);
while (ret != m.end() && (*ret).second == 0) {
ret = m.erase(ret);
}
return ret;
}
template <class T, class U, class V> auto upper_map(map<T, U> &m, V k) {
auto ret = m.upper_bound(k);
while (ret != m.end() && (*ret).second == 0) {
ret = m.erase(ret);
}
return ret;
}
// 存在しなければエラー
template <class T, class U, class V> auto rlower_map(map<T, U> &m, V k) {
auto ret = upper_map(m, k);
assert(ret != m.begin());
ret--;
while (1) {
if ((*ret).second != 0)
break;
assert(ret != m.begin());
auto next = ret;
--next;
m.erase(ret);
ret = next;
}
return ret;
}
template <class T, class U, class V> auto rupper_map(map<T, U> &m, V k) {
auto ret = lower_map(m, k);
assert(ret != m.begin());
ret--;
while (1) {
if ((*ret).second != 0)
break;
assert(ret != m.begin());
auto next = ret;
--next;
m.erase(ret);
ret = next;
}
return ret;
}
template <class T> void fin(T s) { cout << s << endl, exit(0); }
// 便利 数学 math
// sub ⊂ top
bool subset(int sub, int top) { return (sub & top) == sub; }
//-180 ~ 180 degree
double atand(double h, double w) { return atan2(h, w) / PI * 180; }
//% -mの場合、最小の正の数を返す
ll mod(ll a, ll m) {
if (m < 0)
m *= -1;
return (a % m + m) % m;
}
ll pow(ll a) { return a * a; };
ll fact(ll v) { return v <= 1 ? 1 : v * fact(v - 1); }
dou factd(int v) {
static vd fact(2, 1);
if (sz(fact) <= v) {
rep(i, sz(fact), v + 1) { fact.push_back(fact.back() * i); }
}
return fact[v];
}
ll comi(ll n, ll r) {
assert(n < 100);
static vvi(pas, 100, 100);
if (pas[0][0])
return pas[n][r];
pas[0][0] = 1;
rep(i, 1, 100) {
pas[i][0] = 1;
rep(j, 1, i + 1) pas[i][j] = pas[i - 1][j - 1] + pas[i - 1][j];
}
return pas[n][r];
}
double comd2(ll n, ll r) {
static vvd(comb, 2020, 2020);
if (comb[0][0] == 0) {
comb[0][0] = 1;
rep(i, 2000) {
comb[i + 1][0] = 1;
rep(j, 1, i + 2) { comb[i + 1][j] = comb[i][j] + comb[i][j - 1]; }
}
}
return comb[n][r];
}
double comd(int n, int r) {
if (r < 0 || r > n)
return 0;
if (n < 2020)
return comd2(n, r);
static vd fact(2, 1);
if (sz(fact) <= n) {
rep(i, sz(fact), n + 1) { fact.push_back(fact.back() * i); }
}
return fact[n] / fact[n - r] / fact[r];
}
ll gcd(ll a, ll b) {
while (b)
a %= b, swap(a, b);
return abs(a);
}
ll gcd(vi b) {
ll res = b[0];
rep(i, 1, sz(b)) res = gcd(b[i], res);
return res;
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll lcm(vi a) {
ll res = a[0];
rep(i, 1, sz(a)) res = lcm(a[i], res);
return res;
}
ll ceil(ll a, ll b) {
if (b == 0) {
debugline("ceil");
deb(a, b);
ole();
return -1;
} else if (a < 0) {
return 0;
} else {
return (a + b - 1) / b;
}
}
ll sig0(ll t) { return t <= 0 ? 0 : ((1 + t) * t) >> 1; }
// ll sig(ll s, ll t) { return ((s + t) * (t - s + 1)) >> 1; }
ll sig(ll s, ll t) {
if (s > t)
swap(s, t);
return sig0(t - s) + s * (t - s + 1);
}
#define tousa_i tosa_i
#define lower_tousa_i lower_tosa_i
#define upper_tousa upper_tosa
#define upper_tousa_i upper_tosa_i
ll tosa_i(ll st, ll ad, ll v) {
assert(((v - st) % ad) == 0);
return (v - st) / ad;
}
ll tosa_s(ll st, ll ad, ll len) { return st * len + sig0(len - 1) * ad; }
// ax + r (x は非負整数) で表せる整数のうち、v 以上となる最小の整数
ll lower_tosa(ll st, ll ad, ll v) {
if (st >= v)
return st;
return (v - st + ad - 1) / ad * ad + st;
}
// 第何項か
ll lower_tosa_i(ll st, ll ad, ll v) {
if (st >= v)
return 0;
return (v - st + ad - 1) / ad;
}
ll upper_tosa(ll st, ll ad, ll v) { return lower_tosa(st, ad, v + 1); }
ll upper_tosa_i(ll st, ll ad, ll v) { return lower_tosa_i(st, ad, v + 1); }
// v * v >= aとなる最小のvを返す
ll sqrt(ll a) {
if (a < 0) {
debugline("sqrt");
deb(a);
ole();
}
ll res = (ll)std::sqrt(a);
while (res * res < a)
++res;
return res;
}
double log(double e, double x) { return log(x) / log(e); }
/*@formatter:off*/
// 機能拡張
template <class T, class U> string to_string(T a, U b) {
string res = "";
res += a;
res += b;
return res;
}
template <class T, class U, class V> string to_string(T a, U b, V c) {
string res = "";
res += a;
res += b;
res += c;
return res;
}
template <class T, class U, class V, class W>
string to_string(T a, U b, V c, W d) {
string res = "";
res += a;
res += b;
res += c;
res += d;
return res;
}
template <class T, class U, class V, class W, class X>
string to_string(T a, U b, V c, W d, X e) {
string res = "";
res += a;
res += b;
res += c;
res += d;
res += e;
return res;
}
constexpr int bsetlen = k5 * 2;
// constexpr int bsetlen = 5050;
#define bset bitset<bsetlen>
bool operator<(bitset<bsetlen> &a, bitset<bsetlen> &b) {
rer(i, bsetlen - 1) {
if (a[i] < b[i])
return true;
if (a[i] > b[i])
return false;
}
return false;
}
bool operator>(bitset<bsetlen> &a, bitset<bsetlen> &b) {
rer(i, bsetlen - 1) {
if (a[i] > b[i])
return true;
if (a[i] < b[i])
return false;
}
return false;
}
bool operator<=(bitset<bsetlen> &a, bitset<bsetlen> &b) {
rer(i, bsetlen - 1) {
if (a[i] < b[i])
return true;
if (a[i] > b[i])
return false;
}
return true;
}
bool operator>=(bitset<bsetlen> &a, bitset<bsetlen> &b) {
rer(i, bsetlen - 1) {
if (a[i] > b[i])
return true;
if (a[i] < b[i])
return false;
}
return true;
}
string operator~(string &a) {
string res = a;
for (auto &&c : res) {
if (c == '0')
c = '1';
else if (c == '1')
c = '0';
else {
cerr << "cant ~" << a << "must bit" << endl;
exit(0);
}
}
return res;
}
ostream &operator<<(ostream &os, bset &a) {
bitset<10> b;
vi list;
rep(i, bsetlen) {
if (a[i])
list.push_back(i), b[i] = 1;
}
os << b << ", " << list;
return os;
}
int hbiti(bset &a) {
rer(i, bsetlen) {
if (a[i])
return i;
}
return -1;
}
#define hk(a, b, c) (a <= b && b < c)
// O(N/64)
bset nap(bset &a, int v) {
bset r = a | a << v;
return r;
}
bset nap(bset &a, bset &v) {
bset r = a;
rep(i, bsetlen) {
if (v[i])
r |= a << i;
}
return r;
}
template <class T> void seth(vector<vector<T>> &S, int w, vector<T> &v) {
assert(sz(S) == sz(v));
assert(w < sz(S[0]));
rep(h, sz(S)) { S[h][w] = v[h]; }
}
template <class T, class U> void operator+=(pair<T, U> &a, pair<T, U> &b) {
a.fi += b.fi;
a.se += b.se;
}
template <class T, class U> pair<T, U> operator+(pair<T, U> &a, pair<T, U> &b) {
return pair<T, U>(a.fi + b.fi, a.se + b.se);
}
template <typename CharT, typename Traits, typename Alloc>
basic_string<CharT, Traits, Alloc>
operator+(const basic_string<CharT, Traits, Alloc> &lhs, const int rv) {
basic_string<CharT, Traits, Alloc> str(lhs);
str.append(to_string(rv));
return str;
}
template <typename CharT, typename Traits, typename Alloc>
void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const int rv) {
lhs += to_string(rv);
}
template <typename CharT, typename Traits, typename Alloc>
basic_string<CharT, Traits, Alloc>
operator+(const basic_string<CharT, Traits, Alloc> &lhs, const signed rv) {
basic_string<CharT, Traits, Alloc> str(lhs);
str.append(to_string(rv));
return str;
}
template <typename CharT, typename Traits, typename Alloc>
void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const signed rv) {
lhs += to_string(rv);
}
template <typename CharT, typename Traits, typename Alloc>
void operator*=(basic_string<CharT, Traits, Alloc> &s, int num) {
auto bek = s;
s = "";
for (; num; num >>= 1) {
if (num & 1) {
s += bek;
}
bek += bek;
}
}
template <class T, class U> void operator+=(queue<T> &a, U v) { a.push(v); }
template <class T, class U> void operator+=(deque<T> &a, U v) {
a.push_back(v);
}
template <class T>
priority_queue<T, vector<T>, greater<T>> &
operator+=(priority_queue<T, vector<T>, greater<T>> &a, vector<T> &v) {
fora(d, v) a.push(d);
return a;
}
template <class T, class U>
priority_queue<T, vector<T>, greater<T>> &
operator+=(priority_queue<T, vector<T>, greater<T>> &a, U v) {
a.push(v);
return a;
}
template <class T, class U>
priority_queue<T> &operator+=(priority_queue<T> &a, U v) {
a.push(v);
return a;
}
template <class T> set<T> &operator+=(set<T> &a, vector<T> v) {
fora(d, v) a.insert(d);
return a;
}
template <class T, class U> auto operator+=(set<T> &a, U v) {
return a.insert(v);
}
template <class T, class U> auto operator-=(set<T> &a, U v) {
return a.erase(v);
}
template <class T, class U> auto operator+=(mset<T> &a, U v) {
return a.insert(v);
}
template <class T, class U>
set<T, greater<T>> &operator+=(set<T, greater<T>> &a, U v) {
a.insert(v);
return a;
}
template <class T, class U> vector<T> &operator+=(vector<T> &a, U v) {
a.push_back(v);
return a;
}
template <class T, class U> vector<T> operator+(const vector<T> &a, U v) {
vector<T> ret = a;
ret += v;
return ret;
}
template <class T, class U> vector<T> operator+(U v, const vector<T> &a) {
vector<T> ret = a;
ret.insert(ret.begin(), v);
return ret;
}
template <class T> vector<T> operator+(vector<T> a, vector<T> b) {
vector<T> ret;
ret = a;
fora(v, b) ret += v;
return ret;
}
template <class T> vector<T> &operator+=(vector<T> &a, vector<T> &b) {
rep(i, sz(b)) { /*こうしないとa+=aで両辺が増え続けてバグる*/
a.push_back(b[i]);
}
return a;
}
template <class T, class U> map<T, U> &operator+=(map<T, U> &a, map<T, U> &b) {
fora(bv, b) { a[bv.first] += bv.second; }
return a;
}
template <class T> vector<T> &operator-=(vector<T> &a, vector<T> &b) {
if (sz(a) != sz(b)) {
debugline("vector<T> operator-=");
deb(a);
deb(b);
exit(0);
}
rep(i, sz(a)) a[i] -= b[i];
return a;
}
template <class T> vector<T> operator-(vector<T> &a, vector<T> &b) {
if (sz(a) != sz(b)) {
debugline("vector<T> operator-");
deb(a);
deb(b);
ole();
}
vector<T> res(sz(a));
rep(i, sz(a)) res[i] = a[i] - b[i];
return res;
}
template <class T, class U> void operator*=(vector<T> &a, U b) {
vector<T> ta = a;
rep(b - 1) { a += ta; }
}
template <typename T> void erase(vector<T> &v, unsigned ll i) {
v.erase(v.begin() + i);
}
template <typename T> void erase(vector<T> &v, unsigned ll s, unsigned ll e) {
v.erase(v.begin() + s, v.begin() + e);
}
template <class T, class U> void erase(map<T, U> &m, ll okl, ll ngr) {
m.erase(m.lower_bound(okl), m.lower_bound(ngr));
}
template <class T> void erase(set<T> &m, ll okl, ll ngr) {
m.erase(m.lower_bound(okl), m.lower_bound(ngr));
}
template <typename T> void erasen(vector<T> &v, unsigned ll s, unsigned ll n) {
v.erase(v.begin() + s, v.begin() + s + n);
}
template <typename T, typename U>
void insert(vector<T> &v, unsigned ll i, U t) {
v.insert(v.begin() + i, t);
}
template <typename T, typename U> void push_front(vector<T> &v, U t) {
v.insert(v.begin(), t);
}
template <typename T, typename U>
void insert(vector<T> &v, unsigned ll i, vector<T> list) {
for (auto &&va : list)
v.insert(v.begin() + i++, va);
}
template <typename T> void insert(set<T> &v, vector<T> list) {
for (auto &&va : list)
v.insert(va);
}
vector<string> split(const string a, const char deli) {
string b = a + deli;
ll l = 0, r = 0, n = b.size();
vector<string> res;
rep(i, n) {
if (b[i] == deli) {
r = i;
if (l < r)
res.push_back(b.substr(l, r - l));
l = i + 1;
}
}
return res;
}
vector<string> split(const string a, const string deli) {
vector<string> res;
ll kn = sz(deli);
std::string::size_type Pos(a.find(deli));
ll l = 0;
while (Pos != std::string::npos) {
if (Pos - l)
res.push_back(a.substr(l, Pos - l));
l = Pos + kn;
Pos = a.find(deli, Pos + kn);
}
if (sz(a) - l)
res.push_back(a.substr(l, sz(a) - l));
return res;
}
void yn(bool a) {
if (a)
cout << "yes" << endl;
else
cout << "no" << endl;
}
void Yn(bool a) {
if (a)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
void YN(bool a) {
if (a)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
void fyn(bool a) {
if (a)
cout << "yes" << endl;
else
cout << "no" << endl;
exit(0);
}
void fYn(bool a) {
if (a)
cout << "Yes" << endl;
else
cout << "No" << endl;
exit(0);
}
void fYN(bool a) {
if (a)
cout << "YES" << endl;
else
cout << "NO" << endl;
exit(0);
}
void Possible(bool a) {
if (a)
cout << "Possible" << endl;
else
cout << "Impossible" << endl;
exit(0);
}
void POSSIBLE(bool a) {
if (a)
cout << "POSSIBLE" << endl;
else
cout << "IMPOSSIBLE" << endl;
exit(0);
}
template <typename T> class fixed_point : T {
public:
explicit constexpr fixed_point(T &&t) noexcept : T(std::forward<T>(t)) {}
template <typename... Args>
constexpr decltype(auto) operator()(Args &&...args) const {
return T::operator()(*this, std::forward<Args>(args)...);
}
};
template <typename T>
static inline constexpr decltype(auto) fix(T &&t) noexcept {
return fixed_point<T>{std::forward<T>(t)};
}
// 未分類
// tou分割する
template <typename T> vector<vector<T>> cut(vector<T> &a, int tou = 2) {
int N = sz(a);
vector<vector<T>> res(tou);
int hab = N / tou;
vi lens(tou, hab);
rep(i, N % tou) { lens[tou - 1 - i]++; }
int l = 0;
rep(i, tou) {
int len = lens[i];
int r = l + len;
res[i].resize(len);
std::copy(a.begin() + l, a.begin() + r, res[i].begin());
l = r;
}
return res;
}
// 長さn毎に分割する
template <typename T> vector<vector<T>> cutn(vector<T> &a, int len) {
int N = sz(a);
vector<vector<T>> res(ceil(N, len));
vi lens(N / len, len);
if (N % len)
lens.push_back(N % len);
int l = 0;
rep(i, sz(lens)) {
int len = lens[i];
int r = l + len;
res[i].resize(len);
std::copy(a.begin() + l, a.begin() + r, res[i].begin());
l = r;
}
return res;
}
vi inds_(vi &a) {
int n = sz(a);
vb was(n);
vi res(n);
rep(i, n) {
assert(!was[a[i]]);
res[a[i]] = i;
was[a[i]] = true;
}
return res;
}
//@起動時
struct initon {
initon() {
cin.tie(0);
ios::sync_with_stdio(false);
cout.setf(ios::fixed);
cout.precision(16);
srand((unsigned)clock() + (unsigned)time(NULL));
};
} initonv; //@formatter:on
#define pre prev
#define nex next
// gra mll pr
// 上下左右
const string udlr = "udlr";
string UDLR = "UDLR"; // x4と連動 UDLR.find('U') := x4[0]
// 右、上が正
constexpr ll h4[] = {1, -1, 0, 0};
constexpr ll w4[] = {0, 0, -1, 1};
constexpr ll h8[] = {0, 1, 0, -1, -1, 1, 1, -1};
constexpr ll w8[] = {1, 0, -1, 0, 1, -1, 1, -1};
int mei_inc(int h, int w, int H, int W, int i) {
while (++i < 4) {
if (inside(h + h4[i], w + w4[i], H, W))
return i;
}
return i;
}
#define mei(nh, nw, h, w) \
for (int i = mei_inc(h, w, H, W, -1), nh = i < 4 ? h + h4[i] : 0, \
nw = i < 4 ? w + w4[i] : 0; \
i < 4; i = mei_inc(h, w, H, W, i), nh = h + h4[i], nw = w + w4[i])
// グラフ内で #undef getid
// #define getidとしているため、ここを書き直したらgraphも書き直す
#define getid_2(h, w) (h * W + w)
#define getid_1(p) (p.first * W + p.second)
#define o_getid(a, b, name, ...) name
#define getid(...) o_getid(__VA_ARGS__, getid_2, getid_1)(__VA_ARGS__)
#define getp(id) mp(id / W, id % W)
#define shuffle(a) \
std::random_device seed_gen; \
std::mt19937 engine(seed_gen()); \
std::shuffle((a).begin(), (a).end(), engine);
// 1980 開始からtime ms経っていたらtrue
bool timeup(int time) {
auto end_time = system_clock::now();
auto part = duration_cast<milliseconds>(end_time - start_time);
auto lim = milliseconds(time);
return part >= lim;
}
// vectorで取れる要素数
// bool=> 1e9 * 8.32
// int => 1e8 * 2.6
// ll => 1e8 * 1.3
vvc(ba);
ll N, M, H, W;
vi A, B, C;
// 16:10
void solve() {
vector<vector<vi>> dp;
vector<vector<vb>> was;
resize(dp, 3011, 3011, 2);
resize(was, 3011, 3011, 2);
in(N);
na(A, N);
out(fix([&](auto rec, int l, int r, int te) -> int {
if (l == r)
return 0;
int &d = dp[l][r][te];
if (was[l][r][te])
return d;
was[l][r][te] = 1;
// 最大化
if (te == 0) {
d = -linf;
chma(d, rec(l + 1, r, te ^ 1) + A[l]);
chma(d, rec(l, r - 1, te ^ 1) + A[r - 1]);
} else {
d = linf;
chmi(d, rec(l + 1, r, te ^ 1) - A[l]);
chmi(d, rec(l, r - 1, te ^ 1) - A[r - 1]);
}
return d;
})(0, N, 0));
}
auto my(ll n, vi &a) { return 0; }
auto sister(ll n, vi &a) {
ll ret = 0;
return ret;
}
signed main() {
solve();
#define arg n, a
#ifdef _DEBUG
bool bad = 0;
for (ll i = 0, ok = 1; i < k5 && ok; ++i) {
ll n = rand(1, 8);
vi a = ranv(n, 1, 10);
auto myres = my(arg);
auto res = sister(arg);
ok = myres == res;
if (!ok) {
out(arg);
cerr << "AC : " << res << endl;
cerr << "MY : " << myres << endl;
bad = 1;
break;
}
}
if (!bad) {
// cout << "完璧 : solveを書き直そう" << endl;
// cout << " : そして、solve()を呼び出すのだ" << endl;
// cout << " : cin>>n; na(a,n);も忘れるな" << endl;
}
#endif
return 0;
};
| // #undef _DEBUG
// #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace std::chrono;
#define int long long // todo 消したら動かない intの代わりにsignedを使う
#define ll long long
auto start_time = system_clock::now();
#define debugName(VariableName) #VariableName
/*@formatter:off*/
//[-n, n)にアクセスできる
// また、外部関数resizeに渡せる
// sizeは[-n, n)でnを返す
template <class T> class mvec {
vector<T> v;
int n;
public:
mvec() : n(0), v(0) {}
mvec(int n) : n(n), v(n * 2) {}
mvec(int n, T val) : n(n), v(n * 2, val) {}
auto &operator[](int i) { return v[i + n]; }
auto size() { return n; }
void resize(int sn) {
assert(n == 0);
n = sn;
v.resize(sn * 2);
}
auto begin() { return v.begin(); }
auto rbegin() { return v.rbegin(); }
auto end() { return v.end(); }
auto rend() { return v.rend(); }
};
//[]でboolは参照を返さないため特殊化が必要
template <> struct mvec<bool> {
vector<bool> v;
int n;
mvec() : n(0), v(0) {}
mvec(int n) : n(n), v(n * 2) {}
mvec(int n, bool val) : n(n), v(n * 2, val) {}
auto operator[](int i) { return v[i + n]; }
auto size() { return v.size(); }
void resize(int sn) {
assert(n == 0);
n = sn;
v.resize(sn * 2);
}
auto begin() { return v.begin(); }
auto rbegin() { return v.rbegin(); }
auto end() { return v.end(); }
auto rend() { return v.rend(); }
};
template <class T> ostream &operator<<(ostream &os, mvec<T> &a) {
int spa = 3;
for (auto &&v : a) {
spa = max(spa, (int)(to_string(v).size()) + 1);
}
int n = (int)a.size();
os << endl;
for (int i = -n; i < n; i++) {
int need = spa - ((int)to_string(i).size());
if (i == -n) {
need -= min(need, spa - ((int)to_string(a[i]).size()));
}
while (need--) {
os << " ";
}
os << i;
}
os << endl;
int i = -n;
for (auto &&v : a) {
int need = spa - ((int)to_string(v).size());
if (i == -n) {
need -= min(need, spa - ((int)to_string(i).size()));
}
while (need--) {
os << " ";
}
os << v;
i++;
}
return os;
}
#define mv mvec
#define MV mvec
using mvi = mvec<ll>;
using mvb = mvec<bool>;
using mvs = mvec<string>;
using mvd = mvec<double>;
using mvc = mvec<char>;
#define o_mvvt(o1, o2, o3, o4, name, ...) name
#define mvvt0(t) mvec<mvec<t>>
#define mvvt1(t, a) mvec<mvec<t>> a
#define mvvt2(t, a, b) mvec<mvec<t>> a(b)
#define mvvt3(t, a, b, c) mvec<mvec<t>> a(b, mvec<t>(c))
#define mvvt4(t, a, b, c, d) mvec<mvec<t>> a(b, mvec<t>(c, d))
#define mvvi(...) \
o_mvvt(__VA_ARGS__, mvvt4, mvvt3, mvvt2, mvvt1, mvvt0)(ll, __VA_ARGS__)
template <typename T> mvec<T> make_mv(size_t a) { return mvec<T>(a); }
template <typename T, typename... Ts> auto make_mv(size_t a, Ts... ts) {
return mvec<decltype(make_mv<T>(ts...))>(a, make_mv<T>(ts...));
}
#define mvni(name, ...) auto name = make_mv<ll>(__VA_ARGS__)
#ifdef _DEBUG
// https://marycore.jp/prog/cpp/class-extension-methods/ 違うかも
template <class T, class A = std::allocator<T>>
struct debtor : std::vector<T, A> {
using std::vector<T, A>::vector;
template <class U> int deb_v(U a, int v) { return v; }
template <class U> int deb_v(debtor<U> &a, int v = 0) {
cerr << a.size() << " ";
return deb_v(a.at(0), v + 1);
}
template <class U> void deb_o(U a) { cerr << a << " "; }
template <class U> void deb_o(debtor<U> &a) {
for (int i = 0; i < min((int)a.size(), 15ll); i++) {
deb_o(a[i]);
}
if ((int)a.size() > 15) {
cerr << "...";
}
cerr << endl;
}
typename std::vector<T>::reference
operator[](typename std::vector<T>::size_type n) {
if (n < 0 || n >= (int)this->size()) {
int siz = (int)this->size();
cerr << "vector size = ";
int dim = deb_v((*this));
cerr << endl;
cerr << "out index at " << n << endl;
cerr << endl;
if (dim <= 2) {
deb_o((*this));
}
exit(0);
}
return this->at(n);
}
};
#define vector debtor
#endif
#ifdef _DEBUG
// 区間削除は出来ない
template <class T> struct my_pbds_tree {
set<T> s;
auto begin() { return s.begin(); }
auto end() { return s.end(); }
auto rbegin() { return s.rbegin(); }
auto rend() { return s.rend(); }
auto empty() { return s.empty(); }
auto size() { return s.size(); }
void clear() { s.clear(); }
template <class U> void insert(U v) { s.insert(v); }
template <class U> void operator+=(U v) { insert(v); }
template <class F> auto erase(F v) { return s.erase(v); }
template <class U> auto find(U v) { return s.find(v); }
template <class U> auto lower_bound(U v) { return s.lower_bound(v); }
template <class U> auto upper_bound(U v) { return s.upper_bound(v); }
auto find_by_order(ll k) {
auto it = s.begin();
for (ll i = 0; i < k; i++)
it++;
return it;
}
auto order_of_key(ll v) {
auto it = s.begin();
ll i = 0;
for (; it != s.end() && *it < v; i++)
it++;
return i;
}
};
#define pbds(T) my_pbds_tree<T>
// gp_hash_tableでcountを使えないようにするため
template <class T, class U> struct my_unordered_map {
unordered_map<T, U> m;
my_unordered_map(){};
auto begin() { return m.begin(); }
auto end() { return m.end(); }
auto cbegin() { return m.cbegin(); }
auto cend() { return m.cend(); }
template <class V> auto erase(V v) { return m.erase(v); }
void clear() {
m.clear();
} /*countは gp_hash_tableに存在しない*/ /*!= m.end()*/
template <class V> auto find(V v) { return m.find(v); }
template <class V> auto &operator[](V n) { return m[n]; }
};
#define unordered_map my_unordered_map
#define umapi unordered_map<ll, ll>
#define umapp unordered_map<P, ll>
#define umapip unordered_map<ll, P>
#else
// umapはunorderd_mapになる
// umapiはgp_hash_table
// find_by_order(k) k番目のイテレーター
// order_of_key(k) k以上が前から何番目か
#define pbds(U) \
__gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, \
__gnu_pbds::tree_order_statistics_node_update>
#define umapi __gnu_pbds::gp_hash_table<ll, ll, xorshift>
#define umapp __gnu_pbds::gp_hash_table<P, ll, xorshift>
#define umapip __gnu_pbds::gp_hash_table<ll, P, xorshift>
#endif
struct xorshift {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
size_t operator()(std::pair<ll, ll> x) const {
ll v = ((x.first) << 32) | x.second;
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(v + FIXED_RANDOM);
}
};
template <class U, class L>
void operator+=(
__gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update> &s,
L v) {
s.insert(v);
}
// 衝突対策
#define ws ws_
template <class A, class B, class C> struct T2 {
A f;
B s;
C t;
T2() { f = 0, s = 0, t = 0; }
T2(A f, B s, C t) : f(f), s(s), t(t) {}
bool operator<(const T2 &r) const {
return f != r.f ? f < r.f
: s != r.s ? s < r.s
: t < r.t; /*return f != r.f ? f > r.f : s != r.s ?n s >
r.s : t > r.t; 大きい順 */
}
bool operator>(const T2 &r) const {
return f != r.f ? f > r.f
: s != r.s ? s > r.s
: t > r.t; /*return f != r.f ? f > r.f : s != r.s ? s >
r.s : t > r.t; 小さい順 */
}
bool operator==(const T2 &r) const {
return f == r.f && s == r.s && t == r.t;
}
bool operator!=(const T2 &r) const {
return f != r.f || s != r.s || t != r.t;
}
};
template <class A, class B, class C, class D> struct F2 {
A a;
B b;
C c;
D d;
F2() { a = 0, b = 0, c = 0, d = 0; }
F2(A a, B b, C c, D d) : a(a), b(b), c(c), d(d) {}
bool operator<(const F2 &r) const {
return a != r.a ? a < r.a
: b != r.b ? b < r.b
: c != r.c ? c < r.c
: d < r.d; /* return a != r.a ? a > r.a : b != r.b ? b
> r.b : c != r.c ? c > r.c : d > r.d;*/
}
bool operator>(const F2 &r) const {
return a != r.a ? a > r.a
: b != r.b ? b > r.b
: c != r.c ? c > r.c
: d > r.d; /* return a != r.a ? a < r.a : b != r.b
? b < r.b : c != r.c ? c < r.c : d < r.d;*/
}
bool operator==(const F2 &r) const {
return a == r.a && b == r.b && c == r.c && d == r.d;
}
bool operator!=(const F2 &r) const {
return a != r.a || b != r.b || c != r.c || d != r.d;
}
ll operator[](ll i) {
assert(i < 4);
return i == 0 ? a : i == 1 ? b : i == 2 ? c : d;
}
};
typedef T2<ll, ll, ll> T;
typedef F2<ll, ll, ll, ll> F;
T mt(ll a, ll b, ll c) { return T(a, b, c); }
F mf(ll a, ll b, ll c, ll d) { return F(a, b, c, d); }
//@マクロ省略系 型,構造
#define double long double
#define pow powl
#define ull unsigned long long
using dou = double;
using itn = int;
using str = string;
using bo = bool;
#define au auto
using P = pair<ll, ll>;
using mvp = mvec<P>;
using mvt = mvec<T>;
#define MIN(a) numeric_limits<a>::min()
#define MAX(a) numeric_limits<a>::max()
#define fi first
#define se second
#define beg begin
#define rbeg rbegin
#define con continue
#define bre break
#define brk break
#define is ==
#define el else
#define elf else if
#define upd update
#define sstream stringstream
#define maxq 1
#define minq -1
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
#define MALLOC(type, len) (type *)malloc((len) * sizeof(type))
#define lam(right) [&](auto &p) { return p right; }
// マクロ省略系 コンテナ
using vi = vector<ll>;
using vb = vector<bool>;
using vs = vector<string>;
using vd = vector<double>;
using vc = vector<char>;
using vp = vector<P>;
using vt = vector<T>;
#define V vector
#define o_vvt(o1, o2, o3, o4, name, ...) name
#define vvt0(t) vector<vector<t>>
#define vvt1(t, a) vector<vector<t>> a
#define vvt2(t, a, b) vector<vector<t>> a(b)
#define vvt3(t, a, b, c) vector<vector<t>> a(b, vector<t>(c))
#define vvt4(t, a, b, c, d) vector<vector<t>> a(b, vector<t>(c, d))
#define vvi(...) \
o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(ll, __VA_ARGS__)
#define vvb(...) \
o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(bool, __VA_ARGS__)
#define vvs(...) \
o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(string, __VA_ARGS__)
#define vvd(...) \
o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(double, __VA_ARGS__)
#define vvc(...) \
o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(char, __VA_ARGS__)
#define vvp(...) \
o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(P, __VA_ARGS__)
#define vvt(...) \
o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(T, __VA_ARGS__)
#define vv(type, ...) \
o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(type, __VA_ARGS__)
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
#define vni(name, ...) auto name = make_v<ll>(__VA_ARGS__)
#define vnb(name, ...) auto name = make_v<bool>(__VA_ARGS__)
#define vns(name, ...) auto name = make_v<string>(__VA_ARGS__)
#define vnd(name, ...) auto name = make_v<double>(__VA_ARGS__)
#define vnc(name, ...) auto name = make_v<char>(__VA_ARGS__)
#define vnp(name, ...) auto name = make_v<P>(__VA_ARGS__)
#define vn(type, name, ...) auto name = make_v<type>(__VA_ARGS__)
#define PQ priority_queue<ll, vector<ll>, greater<ll>>
#define tos to_string
using mapi = map<ll, ll>;
using mapp = map<P, ll>;
using mapd = map<dou, ll>;
using mapc = map<char, ll>;
using maps = map<str, ll>;
using seti = set<ll>;
using setd = set<dou>;
using setc = set<char>;
using sets = set<str>;
using qui = queue<ll>;
#define uset unordered_set
#define useti unordered_set<ll, xorshift>
#define mset multiset
#define mseti multiset<ll>
#define umap unordered_map
#define mmap multimap
// 任意のマクロサポート用 使う度に初期化する
int index_, v1_, v2_, v3_;
template <class T> struct pq {
priority_queue<T, vector<T>, greater<T>> q; /*小さい順*/
T su = 0;
void clear() {
q = priority_queue<T, vector<T>, greater<T>>();
su = 0;
}
void operator+=(T v) {
su += v;
q.push(v);
}
T sum() { return su; }
T top() { return q.top(); }
void pop() {
su -= q.top();
q.pop();
}
T poll() {
T ret = q.top();
su -= ret;
q.pop();
return ret;
}
ll size() { return q.size(); }
};
template <class T> struct pqg {
priority_queue<T> q; /*大きい順*/
T su = 0;
void clear() {
q = priority_queue<T>();
su = 0;
}
void operator+=(T v) {
su += v;
q.push(v);
}
T sum() { return su; }
T top() { return q.top(); }
void pop() {
su -= q.top();
q.pop();
}
T poll() {
T ret = q.top();
su -= ret;
q.pop();
return ret;
}
ll size() { return q.size(); }
};
#define pqi pq<ll>
#define pqgi pqg<ll>
// マクロ 繰り返し
// ↓@オーバーロード隔離
#define o_rep(o1, o2, o3, o4, name, ...) name
#define rep1(n) for (ll rep1i = 0, rep1lim = n; rep1i < rep1lim; ++rep1i)
#define rep2(i, n) for (ll i = 0, rep2lim = n; i < rep2lim; ++i)
#define rep3(i, m, n) for (ll i = m, rep3lim = n; i < rep3lim; ++i)
#define rep4(i, m, n, ad) for (ll i = m, rep4lim = n; i < rep4lim; i += ad)
// 逆順 閉区間
#define rer2(i, n) for (ll i = n; i >= 0; i--)
#define rer3(i, m, n) for (ll i = m, rer3lim = n; i >= rer3lim; i--)
#define rer4(i, m, n, dec) for (ll i = m, rer4lim = n; i >= rer4lim; i -= dec)
// ループを一つにまとめないとフォーマットで汚くなるため
#define nex_ind1(i) i++
#define nex_ind2(i, j, J) \
i = (j + 1 == J) ? i + 1 : i, j = (j + 1 == J ? 0 : j + 1)
#define nex_ind3(i, j, k, J, K) \
i = (j + 1 == J && k + 1 == K) ? i + 1 : i, \
j = (k + 1 == K) ? (j + 1 == J ? 0 : j + 1) : j, \
k = (k + 1 == K ? 0 : k + 1)
#define nex_ind4(i, j, k, l, J, K, L) \
i = (j + 1 == J && k + 1 == K && l + 1 == L) ? i + 1 : i, \
j = (k + 1 == K && l + 1 == L) ? (j + 1 == J ? 0 : j + 1) : j, \
k = (l + 1 == L ? (k + 1 == K ? 0 : k + 1) : k), l = l + 1 == L ? 0 : l + 1
#define nex_ind5(i, j, k, l, m, J, K, L, M) \
i = (j + 1 == J && k + 1 == K && l + 1 == L && m + 1 == M) ? i + 1 : i, \
j = (k + 1 == K && l + 1 == L && m + 1 == M) ? (j + 1 == J ? 0 : j + 1) : j, \
k = (l + 1 == L && m + 1 == M ? (k + 1 == K ? 0 : k + 1) : k), \
l = m + 1 == M ? l + 1 == L ? 0 : l + 1 : l, m = m + 1 == M ? 0 : m + 1
#define repss1(i, I) for (int i = 0; i < I; i++)
#define repss2(i, j, I, J) \
for (int i = (J ? 0 : I), j = 0; i < I; nex_ind2(i, j, J))
#define repss3(i, j, k, I, J, K) \
for (int i = (J && K ? 0 : I), j = 0, k = 0; i < I; nex_ind3(i, j, k, J, K))
#define repss4(i, j, k, l, I, J, K, L) \
for (int i = (J && K && L ? 0 : I), j = 0, k = 0, l = 0; i < I; \
nex_ind4(i, j, k, l, J, K, L))
#define repss5(i, j, k, l, m, I, J, K, L, M) \
for (int i = (J && K && L && M ? 0 : I), j = 0, k = 0, l = 0, m = 0; i < I; \
nex_ind5(i, j, k, l, m, J, K, L, M))
#define o_repss(o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, name, ...) name
// i,j,k...をnまで見る
#define reps1(i, n) repss1(i, n)
#define reps2(i, j, n) repss2(i, j, n, n)
#define reps3(i, j, k, n) repss3(i, j, k, n, n, n)
#define reps4(i, j, k, l, n) repss4(i, j, k, l, n, n, n, n)
#define o_reps(o1, o2, o3, o4, o5, name, ...) name
template <class T>
void nex_repv2(int &i, int &j, int &I, int &J, vector<vector<T>> &s) {
while (1) {
j++;
if (j >= J) {
j = 0;
i++;
if (i < I) {
J = (int)s[i].size();
}
}
if (i >= I || J)
return;
}
}
template <class T>
void nex_repv3(int &i, int &j, int &k, int &I, int &J, int &K,
vector<vector<vector<T>>> &s) {
while (1) {
k++;
if (k >= K) {
k = 0;
j++;
if (j >= J) {
j = 0;
i++;
if (i >= I)
return;
}
}
J = (int)s[i].size();
K = (int)s[i][j].size();
if (J && K)
return;
}
}
#define repv_1(i, a) repss1(i, sz(a))
// 正方形である必要はない
// 直前を持つのとどっちが早いか
#define repv_2(i, j, a) \
for (int I = (int)a.size(), J = (int)a[0].size(), i = 0, j = 0; i < I; \
nex_repv2(i, j, I, J, a))
// 箱状になっている事が要求される つまり[i] 次元目の要素数は一定
#define repv_3(i, j, k, a) \
for (int I = (int)a.size(), J = (int)a[0].size(), K = (int)a[0][0].size(), \
i = 0, j = 0, k = 0; \
i < I; nex_repv3(i, j, k, I, J, K, a))
#define repv_4(i, j, k, l, a) \
repss4(i, j, k, l, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0]))
#define repv_5(i, j, k, l, m, a) \
repss5(i, j, k, l, m, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0]), \
sz(a[0][0][0][0]))
#define o_repv(o1, o2, o3, o4, o5, o6, name, ...) name
template <typename T> struct has_rbegin_rend {
private:
template <typename U>
static auto check(U &&obj)
-> decltype(std::rbegin(obj), std::rend(obj), std::true_type{});
static std::false_type check(...);
public:
static constexpr bool value = decltype(check(std::declval<T>()))::value;
};
template <typename T>
constexpr bool has_rbegin_rend_v = has_rbegin_rend<T>::value;
template <typename Iterator> class Range {
public:
Range(Iterator &&begin, Iterator &&end) noexcept
: m_begin(std::forward<Iterator>(begin)),
m_end(std::forward<Iterator>(end)) {}
Iterator begin() const noexcept { return m_begin; }
Iterator end() const noexcept { return m_end; }
private:
const Iterator m_begin;
const Iterator m_end;
};
template <typename Iterator>
static inline Range<Iterator> makeRange(Iterator &&begin,
Iterator &&end) noexcept {
return Range<Iterator>{std::forward<Iterator>(begin),
std::forward<Iterator>(end)};
}
template <typename T>
static inline decltype(auto)
makeReversedRange(const std::initializer_list<T> &iniList) noexcept {
return makeRange(std::rbegin(iniList), std::rend(iniList));
}
template <typename T, typename std::enable_if_t<has_rbegin_rend_v<T>,
std::nullptr_t> = nullptr>
static inline decltype(auto) makeReversedRange(T &&c) noexcept {
return makeRange(std::rbegin(c), std::rend(c));
} /* rbegin(),
rend()を持たないものはこっちに分岐させて,エラーメッセージを少なくする*/
template <typename T, typename std::enable_if<!has_rbegin_rend<T>::value,
std::nullptr_t>::type = nullptr>
static inline void makeReversedRange(T &&) noexcept {
static_assert(has_rbegin_rend<T>::value,
"Specified argument doesn't have reverse iterator.");
}
#define form1(st) \
for (auto &&form_it = st.begin(); form_it != st.end(); ++form_it)
#define form3(k, v, st) \
for (auto &&form_it = st.begin(); form_it != st.end(); ++form_it)
#define form4(k, v, st, r) \
for (auto &&form_it = st.begin(); form_it != st.end() && (*form_it).fi < r; \
++form_it)
#define form5(k, v, st, l, r) \
for (auto &&form_it = st.lower_bound(l); \
form_it != st.end() && (*form_it).fi < r; ++form_it)
#define o_form(a, b, c, d, e, name, ...) name
template <class U> vector<U> to1d(vector<U> &a) { return a; }
template <class U> vector<U> to1d(V<V<U>> &a) {
vector<U> res;
for (auto &&a1 : a)
for (auto &&a2 : a1)
res.push_back(a2);
return res;
}
template <class U> vector<U> to1d(V<V<V<U>>> &a) {
vector<U> res;
for (auto &&a1 : a)
for (auto &&a2 : a1)
for (auto &&a3 : a2)
res.push_back(a3);
return res;
}
template <class U> vector<U> to1d(V<V<V<V<U>>>> &a) {
vector<U> res;
for (auto &&a1 : a)
for (auto &&a2 : a1)
for (auto &&a3 : a2)
for (auto &&a4 : a3)
res.push_back(a4);
return res;
}
template <class U> vector<U> to1d(V<V<V<V<V<U>>>>> &a) {
vector<U> res;
for (auto &&a1 : a)
for (auto &&a2 : a1)
for (auto &&a3 : a2)
for (auto &&a4 : a3)
for (auto &&a5 : a4)
res.push_back(a5);
return res;
}
template <class U> vector<U> to1d(V<V<V<V<V<V<U>>>>>> &a) {
vector<U> res;
for (auto &&a1 : a)
for (auto &&a2 : a1)
for (auto &&a3 : a2)
for (auto &&a4 : a3)
for (auto &&a5 : a4)
for (auto &&a6 : a5)
res.push_back(a6);
return res;
}
#define fora_init_2(a, A) ;
#define fora_init_4(a, b, A, B) \
auto &&a = A[fora_i]; \
auto &&b = B[fora_i];
#define fora_init_6(a, b, c, A, B, C) \
auto &&a = A[fora_i]; \
auto &&b = B[fora_i]; \
auto &&c = C[fora_i];
#define fora_init_8(a, b, c, d, A, B, C, D) \
auto &&a = A[fora_i]; \
auto &&b = B[fora_i]; \
auto &&c = C[fora_i]; \
auto &&d = D[fora_i];
#define o_fora_init(a, b, c, d, e, f, g, h, name, ...) name
#define fora_init(...) \
o_fora_init(__VA_ARGS__, fora_init_8, a, fora_init_6, a, fora_init_4, a, \
fora_init_2)(__VA_ARGS__)
#define forr_init_2(a, A) auto &&a = A[forr_i];
#define forr_init_4(a, b, A, B) \
auto &&a = A[forr_i]; \
auto &&b = B[forr_i];
#define forr_init_6(a, b, c, A, B, C) \
auto &&a = A[forr_i]; \
auto &&b = B[forr_i]; \
auto &&c = C[forr_i];
#define forr_init_8(a, b, c, d, A, B, C, D) \
auto &&a = A[forr_i]; \
auto &&b = B[forr_i]; \
auto &&c = C[forr_i]; \
auto &&d = D[forr_i];
#define o_forr_init(a, b, c, d, e, f, g, h, name, ...) name
#define forr_init(...) \
o_forr_init(__VA_ARGS__, forr_init_8, a, forr_init_6, a, forr_init_4, a, \
forr_init_2)(__VA_ARGS__)
#define forp_init(k, v, ...) \
auto &&k = (*forp_it).fi; \
auto &&v = (*forp_it).se;
#define form_init(k, v, ...) \
auto &&k = (*form_it).fi; \
auto &&v = (*form_it).se;
// ↑@オーバーロード隔離
// rep系はインデックス、for系は中身
#define rep(...) o_rep(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rer(...) o_rep(__VA_ARGS__, rer4, rer3, rer2, )(__VA_ARGS__)
// char用のrep
#define repc(i, m, n) for (char i = m, repc3lim = n; i < repc3lim; ++i)
// i,j,k...をnまで見る
#define reps(...) o_reps(__VA_ARGS__, reps4, reps3, reps2, reps1, )(__VA_ARGS__)
#define repss(...) \
o_repss(__VA_ARGS__, repss5, a, repss4, a, repss3, a, repss2, a, \
repss1)(__VA_ARGS__)
// vectorのindexを走査する
// repv(i,j,vvi)
#define repv(...) \
o_repv(__VA_ARGS__, repv_5, repv_4, repv_3, repv_2, repv_1, )(__VA_ARGS__)
#define rerv(i, A) for (int i = sz(A) - 1; i >= 0; i--)
// repvn(dp) nは次元
#define repv1(a) repv(i, a)
#define repv2(a) repv(i, j, a)
#define repv3(a) repv(i, j, k, a)
#define repv4(a) repv(i, j, k, l, a)
#define fora_2(a, A) for (auto &&a : A)
#define fora_4(a, b, A, B) rep(fora_i, sz(A))
#define fora_6(a, b, c, A, B, C) rep(fora_i, sz(A))
#define fora_8(a, b, c, d, A, B, C, D) rep(fora_i, sz(A))
#define o_fora(a, b, c, d, e, f, g, h, name, ...) name
#define fora(...) \
o_fora(__VA_ARGS__, fora_8, a, fora_6, a, fora_4, a, fora_2)(__VA_ARGS__)
#define forr_2(a, A) rer(forr_i, sz(A) - 1)
#define forr_4(a, b, A, B) rer(forr_i, sz(A) - 1)
#define forr_6(a, b, c, A, B, C) rer(forr_i, sz(A) - 1)
#define forr_8(a, b, c, d, A, B, C, D) rer(forr_i, sz(A) - 1)
#define o_forr(a, b, c, d, e, f, g, h, name, ...) name
#define forr(...) \
o_forr(__VA_ARGS__, forr_8, a, forr_6, a, forr_4, a, forr_2)(__VA_ARGS__)
// #define forr(v, a) for(auto&& v : makeReversedRange(a))
// 参照を取らない
#define forv(a, b) for (auto a : to1d(b))
// インデックスを前後含めて走査
#define ring(i, s, len) \
for (int i = s, prev = (s == 0) ? len - 1 : s - 1, \
next = (s == len - 1) ? 0 : s + 1, cou = 0; \
cou < len; \
cou++, prev = i, i = next, next = (next == len - 1) ? 0 : next + 1)
// 値と前後を見る
#define ringv(v, d) \
index_ = 0; \
for (auto prev = d[sz(d) - 1], next = (int)d.size() > 1 ? d[1] : d[0], \
v = d[0]; \
index_ < sz(d); index_++, prev = v, v = next, \
next = (index_ >= sz(d) - 1 ? d[0] : d[index_ + 1]))
// 0の左と nの右
#define forlr(v, d) \
for (auto prev = -1_, next = (int)d.size() > 1 ? d[1] : d[0], v = d[0]; \
index_ < sz(d); index_++, prev = v, v = next, \
next = (index_ >= sz(d) - 1 ? -1_ : d[index_ + 1]))
#define form(...) \
o_form(__VA_ARGS__, form5, form4, form3, form2, form1)(__VA_ARGS__)
#define forp(k, v, st) \
for (auto &&forp_it = st.begin(); forp_it != st.end(); ++forp_it)
// マクロ 定数
#define k3 1010
#define k4 10101
#define k5 101010
#define k6 1010101
#define k7 10101010
const ll inf = (ll)1e9 + 100;
const ll linf = (ll)1e18 + 100;
const char infc = '{';
const string infs = "{";
const double eps = 1e-9;
const double PI = 3.1415926535897932384626433832795029L;
// マクロ省略形 関数等
#define arsz(a) (sizeof(a) / sizeof(a[0]))
#define sz(a) ((ll)(a).size())
#define mp make_pair
#define pb pop_back
#define pf push_front
#define eb emplace_back
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
constexpr bool ev(ll a) { return !(a & 1); }
constexpr bool od(ll a) { return (a & 1); }
//@拡張系 こう出来るべきというもの
// 埋め込み 存在を意識せずに機能を増やされているもの
namespace std {
template <> class hash<std::pair<signed, signed>> {
public:
size_t operator()(const std::pair<signed, signed> &x) const {
return hash<ll>()(((ll)x.first << 32) | x.second);
}
};
template <> class hash<std::pair<ll, ll>> {
public
: /*大きいllが渡されると、<<32でオーバーフローするがとりあえず問題ないと判断*/
size_t operator()(const std::pair<ll, ll> &x) const {
return hash<ll>()(((ll)x.first << 32) | x.second);
}
};
} // namespace std
// stream まとめ
/*@formatter:on*/
istream &operator>>(istream &iss, P &a) {
iss >> a.first >> a.second;
return iss;
}
template <typename T> istream &operator>>(istream &iss, vector<T> &vec) {
for (T &x : vec)
iss >> x;
return iss;
}
template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) {
os << p.fi << " " << p.se;
return os;
}
ostream &operator<<(ostream &os, T p) {
os << p.f << " " << p.s << " " << p.t;
return os;
}
ostream &operator<<(ostream &os, F p) {
os << p.a << " " << p.b << " " << p.c << " " << p.d;
return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
for (ll i = 0; i < vec.size(); ++i)
os << vec[i] << (i + 1 == vec.size() ? "" : " ");
return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
for (ll i = 0; i < vec.size(); ++i) {
for (ll j = 0; j < vec[i].size(); ++j) {
os << vec[i][j] << " ";
}
os << endl;
}
return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, map<T, U> &m) {
for (auto &&v : m)
os << v;
return os;
}
template <class T> ostream &operator<<(ostream &os, set<T> s) {
fora(v, s) { os << v << " "; }
return os;
}
template <class T> ostream &operator<<(ostream &os, deque<T> a) {
fora(v, a) os << v << " ";
return os;
}
ostream &operator<<(ostream &os, vector<vector<char>> &vec) {
rep(h, sz(vec)) {
rep(w, sz(vec[0])) { os << vec[h][w]; }
os << endl;
}
return os;
}
// template<class T,class U>ostream &operator<<(ostream &os, vector<pair<T,U>>&
// a) {fora(v,a)os<<v<<endl;return os;}
/*@formatter:off*/
template <typename W, typename H> void resize(W &vec, const H head) {
vec.resize(head);
}
template <typename W, typename H, typename... T>
void resize(W &vec, const H &head, const T... tail) {
vec.resize(head);
for (auto &v : vec)
resize(v, tail...);
}
// template<typename W, typename H> void resize(vector<W> &vec, const H head) {
// vec.resize(head); } template<typename W, typename H, typename ... T> void
// resize(vector<W> &vec, const H &head, const T ... tail) {vec.resize(head);for
// (auto &v: vec)resize(v, tail...);}
template <typename T, typename F> bool all_of2(T &v, F f) { return f(v); }
template <typename T, typename F> bool all_of2(vector<T> &v, F f) {
rep(i, sz(v)) {
if (!all_of2(v[i], f))
return false;
}
return true;
}
template <typename T, typename F> bool any_of2(T &v, F f) { return f(v); }
template <typename T, typename F> bool any_of2(vector<T> &v, F f) {
rep(i, sz(v)) {
if (any_of2(v[i], f))
return true;
}
return false;
}
template <typename T, typename F> bool none_of2(T &v, F f) { return f(v); }
template <typename T, typename F> bool none_of2(vector<T> &v, F f) {
rep(i, sz(v)) {
if (none_of2(v[i], f))
return false;
}
return true;
}
template <typename T, typename F> bool find_if2(T &v, F f) { return f(v); }
template <typename T, typename F> ll find_if2(vector<T> &v, F f) {
rep(i, sz(v)) {
if (find_if2(v[i], f))
return i;
}
return sz(v);
}
template <typename T, typename F> bool rfind_if2(T &v, F f) { return f(v); }
template <typename T, typename F> ll rfind_if2(vector<T> &v, F f) {
rer(i, sz(v) - 1) {
if (rfind_if2(v[i], f))
return i;
}
return -1;
}
template <class T> bool contains(string &s, const T &v) {
return s.find(v) != string::npos;
}
template <typename T> bool contains(vector<T> &v, const T &val) {
return std::find(v.begin(), v.end(), val) != v.end();
}
template <typename T, typename F> bool contains_if2(vector<T> &v, F f) {
return find_if(v.begin(), v.end(), f) != v.end();
}
template <typename T, typename F> ll count_if2(T &v, F f) { return f(v); }
template <typename T, typename F> ll count_if2(vector<T> &vec, F f) {
ll ret = 0;
fora(v, vec) ret += count_if2(v, f);
return ret;
}
template <typename T, typename F> void for_each2(T &v, F f) { f(v); }
template <typename T, typename F> void for_each2(vector<T> &vec, F f) {
fora(v, vec) for_each2(v, f);
}
template <typename W> ll count_od(vector<W> &a) {
return count_if2(a, [](ll v) { return v & 1; });
}
template <typename W> ll count_ev(vector<W> &a) {
return count_if2(a, [](ll v) { return !(v & 1); });
}
// 削除された要素の数を返す
template <typename T, typename F> int erase_if2(vector<T> &v, F f) {
vector<T> nv;
int cou = 0;
rep(i, sz(v)) {
if (f(v[i])) {
cou++;
} else {
nv.push_back(v[i]);
}
}
v = nv;
return cou;
}
template <typename T, typename F> int erase_if2(vector<vector<T>> &v, F f) {
int cou = 0;
rep(i, sz(v)) { cou += erase_if2(v[i], f); }
return cou;
}
#define all_of(a, right) all_of2(a, lam(right))
#define all_of_f(a, f) all_of2(a, f)
#define any_of(a, right) any_of2(a, lam(right))
#define any_of_f(a, f) any_of2(a, f)
#define none_of(a, right) none_of2(a, lam(right))
#define none_of_f(a, f) none_of2(a, f)
#define find_if(a, right) find_if2(a, lam(right))
#define find_if_f(a, f) find_if2(a, f)
#define rfind_if(a, right) rfind_if2(a, lam(right))
#define rfind_if_f(a, f) rfind_if2(a, f)
#define contains_if(a, right) contains_if2(a, lam(right))
#define contains_if_f(a, f) contains_if2(a, f)
#define count_if(a, right) count_if2(a, lam(right))
#define count_if_f(a, f) count_if2(a, f)
#define for_each(a, right) \
do { \
fora(v, a) { v right; } \
} while (0)
#define for_each_f(a, f) \
do { \
fora(v, a) { f(v); } \
} while (0)
#define erase_if(a, right) erase_if2(a, lam(right))
#define erase_if_f(a, f) erase_if2(a, f)
template <class T, class U> void replace(vector<T> &a, T key, U v) {
replace(a.begin(), a.end(), key, v);
}
void replace(str &a, char key, str v) {
if (v == "")
a.erase(remove(all(a), key), a.end());
}
void replace(str &a, char key, char v) { replace(all(a), key, v); }
// keyと同じかどうか01で置き換える
template <class T, class U> void replace(vector<T> &a, U k) {
rep(i, sz(a)) a[i] = a[i] == k;
}
template <class T, class U> void replace(vector<vector<T>> &a, U k) {
rep(i, sz(a)) rep(j, sz(a[0])) a[i][j] = a[i][j] == k;
}
template <class T> void replace(T &a) { replace(a, '#'); }
void replace(str &a, str key, str v) {
stringstream t;
ll kn = sz(key);
std::string::size_type Pos(a.find(key));
ll l = 0;
while (Pos != std::string::npos) {
t << a.substr(l, Pos - l);
t << v;
l = Pos + kn;
Pos = a.find(key, Pos + kn);
}
t << a.substr(l, sz(a) - l);
a = t.str();
}
template <class T> bool includes(vector<T> &a, vector<T> &b) {
vi c = a;
vi d = b;
sort(all(c));
sort(all(d));
return includes(all(c), all(d));
}
template <class T> bool is_permutation(vector<T> &a, vector<T> &b) {
return is_permutation(all(a), all(b));
}
template <class T> bool next_permutation(vector<T> &a) {
return next_permutation(all(a));
}
void iota(vector<ll> &ve, ll s, ll n) {
ve.resize(n);
iota(all(ve), s);
}
vi iota(ll s, ll len) {
vi ve(len);
iota(all(ve), s);
return ve;
}
template <class A, class B> auto vtop(vector<A> &a, vector<B> &b) {
assert(sz(a) == sz(b)); /*stringを0で初期化できない */
vector<pair<A, B>> res;
rep(i, sz(a)) res.eb(a[i], b[i]);
return res;
}
template <class A, class B>
void ptov(vector<pair<A, B>> &p, vector<A> &a, vector<B> &b) {
a.resize(sz(p)), b.resize(sz(p));
rep(i, sz(p)) a[i] = p[i].fi, b[i] = p[i].se;
}
template <class A, class B, class C>
auto vtot(vector<A> &a, vector<B> &b, vector<C> &c) {
assert(sz(a) == sz(b) && sz(b) == sz(c));
vector<T2<A, B, C>> res;
rep(i, sz(a)) res.eb(a[i], b[i], c[i]);
return res;
}
template <class A, class B, class C, class D>
auto vtof(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) {
assert(sz(a) == sz(b) && sz(b) == sz(c) && sz(c) == sz(d));
vector<F2<A, B, C, D>> res;
rep(i, sz(a)) res.eb(a[i], b[i], c[i], d[i]);
return res;
}
enum pcomparator { fisi, fisd, fdsi, fdsd, sifi, sifd, sdfi, sdfd };
enum tcomparator {
fisiti,
fisitd,
fisdti,
fisdtd,
fdsiti,
fdsitd,
fdsdti,
fdsdtd,
fitisi,
fitisd,
fitdsi,
fitdsd,
fdtisi,
fdtisd,
fdtdsi,
fdtdsd,
sifiti,
sifitd,
sifdti,
sifdtd,
sdfiti,
sdfitd,
sdfdti,
sdfdtd,
sitifi,
sitifd,
sitdfi,
sitdfd,
sdtifi,
sdtifd,
sdtdfi,
sdfdfd,
tifisi,
tifisd,
tifdsi,
tifdsd,
tdfisi,
tdfisd,
tdfdsi,
tdfdsd,
tisifi,
tisifd,
tisdfi,
tisdfd,
tdsifi,
tdsifd,
tdsdfi,
tdsdfd
};
template <class A, class B> void sort(vector<pair<A, B>> &a, pcomparator type) {
typedef pair<A, B> U;
if (type == fisi)
sort(all(a),
[&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se < r.se; });
else if (type == fisd)
sort(all(a),
[&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se > r.se; });
else if (type == fdsi)
sort(all(a),
[&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se < r.se; });
else if (type == fdsd)
sort(all(a),
[&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se > r.se; });
else if (type == sifi)
sort(all(a),
[&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi < r.fi; });
else if (type == sifd)
sort(all(a),
[&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi > r.fi; });
else if (type == sdfi)
sort(all(a),
[&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi < r.fi; });
else if (type == sdfd)
sort(all(a),
[&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi > r.fi; });
};
template <class U> void sort(vector<U> &a, pcomparator type) {
if (type == fisi)
sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s < r.s; });
else if (type == fisd)
sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s > r.s; });
else if (type == fdsi)
sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s < r.s; });
else if (type == fdsd)
sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s > r.s; });
else if (type == sifi)
sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f < r.f; });
else if (type == sifd)
sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f > r.f; });
else if (type == sdfi)
sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f < r.f; });
else if (type == sdfd)
sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f > r.f; });
};
template <class A, class B, class C, class D>
void sort(vector<F2<A, B, C, D>> &a, pcomparator type) {
typedef F2<A, B, C, D> U;
if (type == fisi)
sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b < r.b; });
else if (type == fisd)
sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b > r.b; });
else if (type == fdsi)
sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b < r.b; });
else if (type == fdsd)
sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b > r.b; });
else if (type == sifi)
sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a < r.a; });
else if (type == sifd)
sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a > r.a; });
else if (type == sdfi)
sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a < r.a; });
else if (type == sdfd)
sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a > r.a; });
};
template <class U> void sort(vector<U> &a, tcomparator type) {
if (type == 0)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t < r.t;
});
else if (type == 1)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t > r.t;
});
else if (type == 2)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t < r.t;
});
else if (type == 3)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t > r.t;
});
else if (type == 4)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t < r.t;
});
else if (type == 5)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t > r.t;
});
else if (type == 6)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t < r.t;
});
else if (type == 7)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t > r.t;
});
else if (type == 8)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s < r.s;
});
else if (type == 9)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s > r.s;
});
else if (type == 10)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s < r.s;
});
else if (type == 11)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s > r.s;
});
else if (type == 12)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s < r.s;
});
else if (type == 13)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s > r.s;
});
else if (type == 14)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s < r.s;
});
else if (type == 15)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s > r.s;
});
else if (type == 16)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t < r.t;
});
else if (type == 17)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t > r.t;
});
else if (type == 18)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t < r.t;
});
else if (type == 19)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t > r.t;
});
else if (type == 20)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t < r.t;
});
else if (type == 21)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t > r.t;
});
else if (type == 22)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t < r.t;
});
else if (type == 23)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t > r.t;
});
else if (type == 24)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f < r.f;
});
else if (type == 25)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f > r.f;
});
else if (type == 26)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f < r.f;
});
else if (type == 27)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f > r.f;
});
else if (type == 28)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f < r.f;
});
else if (type == 29)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f > r.f;
});
else if (type == 30)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f < r.f;
});
else if (type == 31)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f > r.f;
});
else if (type == 32)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s < r.s;
});
else if (type == 33)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s > r.s;
});
else if (type == 34)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s < r.s;
});
else if (type == 35)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s > r.s;
});
else if (type == 36)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s < r.s;
});
else if (type == 37)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s > r.s;
});
else if (type == 38)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s < r.s;
});
else if (type == 39)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s > r.s;
});
else if (type == 40)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f < r.f;
});
else if (type == 41)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f > r.f;
});
else if (type == 42)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f < r.f;
});
else if (type == 43)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f > r.f;
});
else if (type == 44)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f < r.f;
});
else if (type == 45)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f > r.f;
});
else if (type == 46)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f < r.f;
});
else if (type == 47)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f > r.f;
});
}
template <class A, class B, class C, class D>
void sort(vector<F2<A, B, C, D>> &a, tcomparator type) {
typedef F2<A, B, C, D> U;
if (type == 0)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c < r.c;
});
else if (type == 1)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c > r.c;
});
else if (type == 2)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c < r.c;
});
else if (type == 3)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c > r.c;
});
else if (type == 4)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c < r.c;
});
else if (type == 5)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c > r.c;
});
else if (type == 6)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c < r.c;
});
else if (type == 7)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c > r.c;
});
else if (type == 8)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b < r.b;
});
else if (type == 9)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b > r.b;
});
else if (type == 10)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b < r.b;
});
else if (type == 11)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b > r.b;
});
else if (type == 12)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b < r.b;
});
else if (type == 13)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b > r.b;
});
else if (type == 14)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b < r.b;
});
else if (type == 15)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b > r.b;
});
else if (type == 16)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c < r.c;
});
else if (type == 17)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c > r.c;
});
else if (type == 18)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c < r.c;
});
else if (type == 19)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c > r.c;
});
else if (type == 20)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c < r.c;
});
else if (type == 21)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c > r.c;
});
else if (type == 22)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c < r.c;
});
else if (type == 23)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c > r.c;
});
else if (type == 24)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a < r.a;
});
else if (type == 25)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a > r.a;
});
else if (type == 26)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a < r.a;
});
else if (type == 27)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a > r.a;
});
else if (type == 28)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a < r.a;
});
else if (type == 29)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a > r.a;
});
else if (type == 30)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a < r.a;
});
else if (type == 31)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a > r.a;
});
else if (type == 32)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b < r.b;
});
else if (type == 33)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b > r.b;
});
else if (type == 34)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b < r.b;
});
else if (type == 35)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b > r.b;
});
else if (type == 36)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b < r.b;
});
else if (type == 37)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b > r.b;
});
else if (type == 38)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b < r.b;
});
else if (type == 39)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b > r.b;
});
else if (type == 40)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a < r.a;
});
else if (type == 41)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a > r.a;
});
else if (type == 42)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a < r.a;
});
else if (type == 43)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a > r.a;
});
else if (type == 44)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a < r.a;
});
else if (type == 45)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a > r.a;
});
else if (type == 46)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a < r.a;
});
else if (type == 47)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a > r.a;
});
}
void sort(string &a) { sort(all(a)); }
template <class T> void sort(vector<T> &a) { sort(all(a)); }
// P l, P rで f(P) の形で渡す
template <class U, class F> void sort(vector<U> &a, F f) {
sort(all(a), [&](U l, U r) { return f(l) < f(r); });
};
template <class T> void rsort(vector<T> &a) { sort(all(a), greater<T>()); };
template <class U, class F> void rsort(vector<U> &a, F f) {
sort(all(a), [&](U l, U r) { return f(l) > f(r); });
};
// F = T<T>
// 例えばreturn p.fi + p.se;
template <class A, class B> void sortp(vector<A> &a, vector<B> &b) {
auto c = vtop(a, b);
sort(c);
rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se;
}
template <class A, class B, class F>
void sortp(vector<A> &a, vector<B> &b, F f) {
auto c = vtop(a, b);
sort(c, f);
rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se;
}
template <class A, class B> void rsortp(vector<A> &a, vector<B> &b) {
auto c = vtop(a, b);
rsort(c);
rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second;
}
template <class A, class B, class F>
void rsortp(vector<A> &a, vector<B> &b, F f) {
auto c = vtop(a, b);
rsort(c, f);
rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second;
}
template <class A, class B, class C>
void sortt(vector<A> &a, vector<B> &b, vector<C> &c) {
auto d = vtot(a, b, c);
sort(d);
rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t;
}
template <class A, class B, class C, class F>
void sortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) {
auto d = vtot(a, b, c);
sort(d, f);
rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t;
}
template <class A, class B, class C>
void rsortt(vector<A> &a, vector<B> &b, vector<C> &c) {
auto d = vtot(a, b, c);
rsort(d);
rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t;
}
template <class A, class B, class C, class F>
void rsortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) {
auto d = vtot(a, b, c);
rsort(d, f);
rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t;
}
template <class A, class B, class C, class D>
void sortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) {
auto e = vtof(a, b, c, d);
sort(e);
rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d;
}
template <class A, class B, class C, class D>
void rsortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) {
auto e = vtof(a, b, c, d);
rsort(e);
rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d;
}
// sortindex 元のvectorはソートしない
template <class T> vi sorti(vector<T> &a) {
auto b = a;
vi ind = iota(0, sz(a));
sortp(b, ind);
return ind;
} /*indexの分で型が変わるためpcomparatorが必要*/
template <class T> vi sorti(vector<T> &a, pcomparator f) {
auto b = a;
vi ind = iota(0, sz(a));
sortp(b, ind, f);
return ind;
}
template <class T, class F> vi sorti(vector<T> &a, F f) {
vi ind = iota(0, sz(a));
sort(all(ind), [&](ll x, ll y) { return f(a[x]) < f(a[y]); });
return ind;
}
template <class T> vi rsorti(vector<T> &a) {
auto b = a;
vi ind = iota(0, sz(a));
rsortp(b, ind);
return ind;
}
template <class T, class F> vi rsorti(vector<T> &a, F f) {
vi ind = iota(0, sz(a));
sort(all(ind), [&](ll x, ll y) { return f(a[x]) > f(a[y]); });
return ind;
}
template <class A, class B, class F>
vi sortpi(vector<A> &a, vector<B> &b, F f) {
auto c = vtop(a, b);
vi ind = iota(0, sz(a));
sort(all(ind), [&](ll x, ll y) { return f(c[x]) < f(c[y]); });
return ind;
}
template <class A, class B>
vi sortpi(vector<A> &a, vector<B> &b, pcomparator f) {
vi ind = iota(0, sz(a));
auto c = a;
auto d = b;
sortt(c, d, ind, f);
return ind;
}
template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b) {
return sortpi(a, b, fisi);
};
template <class A, class B, class F>
vi rsortpi(vector<A> &a, vector<B> &b, F f) {
auto c = vtop(a, b);
vi ind = iota(0, sz(a));
sort(all(ind), [&](ll x, ll y) { return f(c[x]) > f(c[y]); });
return ind;
}
template <class A, class B> vi rsortpi(vector<A> &a, vector<B> &b) {
return sortpi(a, b, fdsd);
};
template <class A, class B, class C, class F>
vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) {
auto d = vtot(a, b, c);
vi ind = iota(0, sz(a));
sort(all(ind), [&](ll x, ll y) { return f(d[x]) < f(d[y]); });
return ind;
}
template <class A, class B, class C>
vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, pcomparator f) {
vi ind = iota(0, sz(a));
auto d = vtof(a, b, c, ind);
sort(d, f);
rep(i, sz(a)) ind[i] = d[i].d;
return ind;
}
template <class A, class B, class C>
vi sortti(vector<A> &a, vector<B> &b, vector<C> &c) {
vi ind = iota(0, sz(a));
sort(all(ind), [&](ll x, ll y) {
if (a[x] == a[y]) {
if (b[x] == b[y])
return c[x] < c[y];
else
return b[x] < b[y];
} else {
return a[x] < a[y];
}
});
return ind;
}
template <class A, class B, class C, class F>
vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) {
auto d = vtot(a, b, c);
vi ind = iota(0, sz(a));
sort(all(ind), [&](ll x, ll y) { return f(d[x]) > f(d[y]); });
return ind;
}
template <class A, class B, class C>
vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c) {
vi ind = iota(0, sz(a));
sort(all(ind), [&](ll x, ll y) {
if (a[x] == a[y]) {
if (b[x] == b[y])
return c[x] > c[y];
else
return b[x] > b[y];
} else {
return a[x] > a[y];
}
});
return ind;
}
template <class T> void sort2(vector<vector<T>> &a) {
for (ll i = 0, n = a.size(); i < n; ++i)
sort(a[i]);
}
template <class T> void rsort2(vector<vector<T>> &a) {
for (ll i = 0, n = a.size(); i < n; ++i)
rsort(a[i]);
}
template <typename A, size_t N, typename T> void fill(A (&a)[N], const T &v) {
rep(i, N) a[i] = v;
}
template <typename A, size_t N, size_t O, typename T>
void fill(A (&a)[N][O], const T &v) {
rep(i, N) rep(j, O) a[i][j] = v;
}
template <typename A, size_t N, size_t O, size_t P, typename T>
void fill(A (&a)[N][O][P], const T &v) {
rep(i, N) rep(j, O) rep(k, P) a[i][j][k] = v;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, typename T>
void fill(A (&a)[N][O][P][Q], const T &v) {
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) a[i][j][k][l] = v;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R,
typename T>
void fill(A (&a)[N][O][P][Q][R], const T &v) {
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) a[i][j][k][l][m] = v;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R,
size_t S, typename T>
void fill(A (&a)[N][O][P][Q][R][S], const T &v) {
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S)
a[i][j][k][l][m][n] = v;
}
template <typename W, typename T> void fill(W &xx, const T vall) { xx = vall; }
template <typename W, typename T> void fill(vector<W> &vecc, const T vall) {
for (auto &&vx : vecc)
fill(vx, vall);
}
template <typename W, typename T> void fill(vector<W> &xx, ll len, const T v) {
rep(i, len) xx[i] = v;
}
template <typename W, typename T>
void fill(vector<vector<W>> &xx, int sh, int th, int sw, int tw, T v) {
rep(h, sh, th) rep(w, sw, tw) xx[h][w] = v;
}
template <class T, class U> void fill(vector<T> &a, vi &ind, U val) {
fora(v, ind) a[v] = val;
}
template <class W, class T> void fill(mvec<W> &xx, const T v) {
fora(x, xx) fill(x, v);
}
template <typename A, size_t N> A sum(A (&a)[N]) {
A res = 0;
rep(i, N) res += a[i];
return res;
}
template <typename A, size_t N, size_t O> A sum(A (&a)[N][O]) {
A res = 0;
rep(i, N) rep(j, O) res += a[i][j];
return res;
}
template <typename A, size_t N, size_t O, size_t P> A sum(A (&a)[N][O][P]) {
A res = 0;
rep(i, N) rep(j, O) rep(k, P) res += a[i][j][k];
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q>
A sum(A (&a)[N][O][P][Q]) {
A res = 0;
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) res += a[i][j][k][l];
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R>
A sum(A (&a)[N][O][P][Q][R]) {
A res = 0;
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) res += a[i][j][k][l][m];
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R,
size_t S>
A sum(A (&a)[N][O][P][Q][R][S]) {
A res = 0;
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S) res +=
a[i][j][k][l][m][n];
return res;
}
//@汎用便利関数 入力
ll in() {
ll ret;
cin >> ret;
return ret;
}
string sin() {
string ret;
cin >> ret;
return ret;
}
template <class T> void in(T &head) { cin >> head; }
template <class T, class... U> void in(T &head, U &...tail) {
cin >> head;
in(tail...);
}
#define o_din(o1, o2, o3, o4, o5, o6, name, ...) name
#define din1(a) \
ll a; \
cin >> a
#define din2(a, b) \
ll a, b; \
cin >> a >> b
#define din3(a, b, c) \
ll a, b, c; \
cin >> a >> b >> c
#define din4(a, b, c, d) \
ll a, b, c, d; \
cin >> a >> b >> c >> d
#define din5(a, b, c, d, e) \
ll a, b, c, d, e; \
cin >> a >> b >> c >> d >> e
#define din6(a, b, c, d, e, f) \
ll a, b, c, d, e, f; \
cin >> a >> b >> c >> d >> e >> f
#define din(...) \
o_din(__VA_ARGS__, din6, din5, din4, din3, din2, din1)(__VA_ARGS__)
#define o_dins(o1, o2, o3, o4, o5, o6, name, ...) name
#define dins1(a) \
str a; \
cin >> a
#define dins2(a, b) \
str a, b; \
cin >> a >> b
#define dins3(a, b, c) \
str a, b, c; \
cin >> a >> b >> c
#define dins4(a, b, c, d) \
str a, b, c, d; \
cin >> a >> b >> c >> d
#define dins5(a, b, c, d, e) \
str a, b, c, d, e; \
cin >> a >> b >> c >> d >> e
#define dins6(a, b, c, d, e, f) \
str a, b, c, d, e, f; \
cin >> a >> b >> c >> d >> e >> f
#define dins(...) \
o_dins(__VA_ARGS__, dins6, dins5, dins4, dins3, dins2, dins1)(__VA_ARGS__)
#define o_dind(o1, o2, o3, o4, name, ...) name
#define din1d(a) \
din1(a); \
a--
#define din2d(a, b) \
din2(a, b); \
a--, b--
#define din3d(a, b, c) \
din3(a, b, c); \
a--, b--, c--
#define din4d(a, b, c, d) \
din4(a, b, c, d); \
a--, b--, c--, d--
#define dind(...) o_dind(__VA_ARGS__, din4d, din3d, din2d, din1d)(__VA_ARGS__)
template <class T> void out2(T &&head) { cout << head; }
template <class T, class... U> void out2(T &&head, U &&...tail) {
cout << head << " ";
out2(tail...);
}
template <class T, class... U> void out(T &&head, U &&...tail) {
cout << head << " ";
out2(tail...);
cout << "" << endl;
}
template <class T> void out(T &&head) { cout << head << endl; }
void out() { cout << "" << endl; }
#ifdef _DEBUG
template <class T> void err2(T &&head) { cerr << head; }
template <class T, class... U> void err2(T &&head, U &&...tail) {
cerr << head << " ";
err2(tail...);
}
template <class T, class... U> void err(T &&head, U &&...tail) {
cerr << head << " ";
err2(tail...);
cerr << "" << endl;
}
template <class T> void err(T &&head) { cerr << head << endl; }
void err() { cerr << "" << endl; }
/*@formatter:on*/
template <class T> string out_m2(vector<T> &a, ll W = inf) {
stringstream ss;
if (W == inf)
W = min(sz(a), 12ll);
if (sz(a) == 0)
return ss.str();
rep(i, W) {
ss << a[i];
if (typeid(a[i]) == typeid(P)) {
ss << endl;
} else {
ss << " ";
}
}
return ss.str();
}
/*@formatter:off*/
template <class T>
string out_m2(vector<vector<T>> &a, ll H = inf, ll W = inf, int key = -1) {
H = min({H, sz(a), 12ll});
W = min({W, sz(a[0]), 12ll});
stringstream ss;
ss << endl;
if (key == -1)
ss << " *|";
else
ss << " " << key << "|";
rep(w, W) ss << std::right << std::setw(4) << w;
ss << "" << endl;
rep(w, W * 4 + 3) ss << "_";
ss << "" << endl;
rep(h, H) {
ss << std::right << std::setw(2) << h << "|";
rep(w, min(sz(a[h]), 12ll)) {
if (abs(a[h][w]) == linf)
ss << " e"
<< "";
else
ss << std::right << std::setw(4) << a[h][w];
}
ss << "" << endl;
}
return ss.str();
}
template <class T>
string out_m2(vector<vector<vector<T>>> &a, ll H = inf, ll W = inf,
ll U = inf) {
stringstream ss;
if (H == inf)
H = 12;
H = min(H, sz(a));
rep(i, H) {
ss << endl;
ss << out_m2(a[i], W, U, i);
}
return ss.str();
}
template <class T, size_t N> string out_m2(T (&a)[N]) {
vector<T> b;
resize(b, N);
rep(i, N) { b[i] = a[i]; }
return out_m2(b);
}
template <class T, size_t N, size_t M> string out_m2(T (&a)[N][M]) {
vector<vector<T>> b;
resize(b, N, M);
rep(i, N) {
rep(j, M) { b[i][j] = a[i][j]; }
}
return out_m2(b);
}
template <class T, size_t N, size_t M, size_t O>
string out_m2(T (&a)[N][M][O]) {
vector<vector<vector<T>>> b;
resize(b, N, M, O);
rep(i, N) {
rep(j, M) {
rep(k, O) { b[i][j][k] = a[i][j][k]; }
}
}
return out_m2(b);
}
string out_m2(int a) {
stringstream ss;
ss << a;
return ss.str();
}
/*@formatter:on*/
template <class T>
string out_m2(mvec<mvec<T>> &a, ll H = inf, ll W = inf, int key = inf) {
H = min({H, sz(a), 6ll});
W = min({W, sz(a[0]), 6ll});
stringstream ss;
ss << endl;
// if (key == inf)ss << " *|"; else ss << " " << key << "|";
if (key == inf)
ss << " *|";
else {
ss << std::right << std::setw(2) << key;
ss << "|";
}
rep(w, -W, W) ss << std::right << std::setw(4) << w;
ss << "" << endl;
rep(w, W * 8 + 3) ss << "_";
ss << "" << endl;
rep(h, -H, H) {
ss << std::right << std::setw(2) << h << "|";
int NW = min(sz(a[h]), 6ll);
rep(w, -NW, NW) {
if (abs(a[h][w]) == linf)
ss << " e"
<< "";
else
ss << std::right << std::setw(4) << a[h][w];
}
ss << "" << endl;
}
return ss.str();
} /*@formatter:on*/
template <class T>
string out_m2(mvec<mvec<mvec<T>>> &a, ll H = inf, ll W = inf, ll U = inf) {
stringstream ss;
if (H == inf)
H = 6;
H = min(H, sz(a));
rep(i, -H, H) {
ss << endl;
ss << out_m2(a[i], W, U, i);
}
return ss.str();
}
/*@formatter:off*/
template <class T> string out_m2(T &a) {
stringstream ss;
ss << a;
return ss.str();
}
/*@formatter:on*/
template <class T> string out_m(vector<T> &a, ll W = inf) {
stringstream ss;
if (W == inf)
W = min(sz(a), 12ll);
if (sz(a) == 0)
return ss.str();
rep(i, W) { ss << a[i] << " "; }
ss << "" << endl;
return ss.str();
}
/*@formatter:off*/
template <class T>
string out_m(vector<vector<T>> &a, ll H = inf, ll W = inf, int key = -1) {
H = min({H, sz(a), 12ll});
W = min({W, sz(a[0]), 12ll});
stringstream ss;
ss << endl;
if (key == -1)
ss << " *|";
else
ss << " " << key << "|";
rep(w, W) ss << std::right << std::setw(4) << w;
ss << "" << endl;
rep(w, W * 4 + 3) ss << "_";
ss << "" << endl;
rep(h, H) {
ss << std::right << std::setw(2) << h << "|";
rep(w, min(sz(a[h]), 12ll)) {
if (abs(a[h][w]) == linf)
ss << " e"
<< "";
else
ss << std::right << std::setw(4) << a[h][w];
}
ss << "" << endl;
}
ss << endl;
return ss.str();
}
template <class T>
string out_m(vector<vector<vector<T>>> &a, ll H = inf, ll W = inf, ll U = inf) {
stringstream ss;
if (H == inf)
H = 5;
H = min(H, sz(a));
rep(i, H) {
ss << endl;
ss << out_m(a[i], W, U, i);
}
ss << endl;
return ss.str();
}
string out_m(int a) {
stringstream ss;
ss << a << endl;
return ss.str();
}
template <class T> string out_m(T &a) {
stringstream ss;
ss << a << endl;
return ss.str();
}
template <class T> void outv(vector<T> &a, ll W = inf) {
cout << out_m(a, W) << endl;
}
template <class T>
void outv(vector<vector<T>> &a, ll H = linf, ll W = linf, int key = -1) {
cout << out_m(a, H, W, key) << endl;
}
template <class T>
void outv(vector<vector<vector<T>>> &a, ll H = linf, ll W = linf, ll U = linf) {
cout << out_m(a, H, W, U) << endl;
}
#else
template <class T> void outv(vector<T> &a, ll W = inf) {
rep(i, min(W, sz(a))) { cout << a[i] << " "; }
cout << "" << endl;
}
template <class T>
void outv(vector<vector<T>> &a, ll H = linf, ll W = linf, int key = -1) {
rep(i, min(H, sz(a))) { outv(a[i], W); }
}
template <class T>
void outv(vector<vector<vector<T>>> &a, ll H = linf, ll W = linf, ll U = linf) {
;
}
#define err(...) ;
#endif
template <class T> void outl(vector<T> &a, int n = inf) {
rep(i, min(n, sz(a))) cout << a[i] << endl;
}
// テーブルをスペースなしで出力
template <class T> void outt(vector<vector<T>> &a) {
rep(i, sz(a)) {
rep(j, sz(a[i])) { cout << a[i][j]; }
cout << endl;
}
}
// int型をbit表記で出力
void outb(int a) { cout << bitset<20>(a) << endl; }
template <class T> void na(vector<T> &a, ll n) {
a.resize(n);
rep(i, n) cin >> a[i];
}
#define dna(a, n) \
vi a(n); \
rep(dnai, n) cin >> a[dnai];
#define dnad(a, n) \
vi a(n); \
rep(dnai, n) cin >> a[dnai], a[dnai]--;
template <class T> void nao(vector<T> &a, ll n) {
a.resize(n + 1);
a[0] = 0;
rep(i, n) cin >> a[i + 1];
}
template <class T> void naod(vector<T> &a, ll n) {
a.resize(n + 1);
a[0] = 0;
rep(i, n) cin >> a[i + 1], a[i + 1]--;
}
template <class T> void nad(vector<T> &a, ll n) {
a.resize(n);
rep(i, n) cin >> a[i], a[i]--;
}
template <class T, class U> void na2(vector<T> &a, vector<U> &b, ll n) {
a.resize(n);
b.resize(n);
rep(i, n) cin >> a[i] >> b[i];
}
#define dna2(a, b, n) \
vi a(n), b(n); \
rep(dna2i, n) cin >> a[dna2i] >> b[dna2i];
template <class T, class U> void nao2(vector<T> &a, vector<U> &b, ll n) {
a.resize(n + 1);
b.resize(n + 1);
a[0] = b[0] = 0;
rep(i, n) cin >> a[i + 1] >> b[i + 1];
}
#define dna2d(a, b, n) \
vi a(n), b(n); \
rep(dna2di, n) { \
cin >> a[dna2di] >> b[dna2di]; \
a[dna2di]--, b[dna2di]--; \
}
template <class T, class U> void na2d(vector<T> &a, vector<U> &b, ll n) {
a.resize(n);
b.resize(n);
rep(i, n) cin >> a[i] >> b[i], a[i]--, b[i]--;
}
template <class T, class U, class W>
void na3(vector<T> &a, vector<U> &b, vector<W> &c, ll n) {
a.resize(n);
b.resize(n);
c.resize(n);
rep(i, n) cin >> a[i] >> b[i] >> c[i];
}
#define dna3(a, b, c, n) \
vi a(n), b(n), c(n); \
rep(dna3i, n) cin >> a[dna3i] >> b[dna3i] >> c[dna3i];
template <class T, class U, class W>
void na3d(vector<T> &a, vector<U> &b, vector<W> &c, ll n) {
a.resize(n);
b.resize(n);
c.resize(n);
rep(i, n) cin >> a[i] >> b[i] >> c[i], a[i]--, b[i]--, c[i]--;
}
#define dna3d(a, b, c, n) \
vi a(n), b(n), c(n); \
rep(dna3di, n) { \
cin >> a[dna3di] >> b[dna3di] >> c[dna3di]; \
a[dna3di]--, b[dna3di]--, c[dna3di]--; \
}
template <class T, class U, class W, class X>
void na4(vector<T> &a, vector<U> &b, vector<W> &c, vector<X> &d, ll n) {
a.resize(n);
b.resize(n);
c.resize(n);
d.resize(n);
rep(i, n) cin >> a[i] >> b[i] >> c[i] >> d[i];
}
#define dna4(a, b, c, d, n) \
vi a(n), b(n), c(n), d(n); \
rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i];
#define dna4d(a, b, c, d, n) \
vi a(n), b(n), c(n), d(n); \
rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i], \
--a[dna4i], --b[dna4i], --c[dna4i], --d[dna4i];
#define nt(a, h, w) \
resize(a, h, w); \
rep(nthi, h) rep(ntwi, w) cin >> a[nthi][ntwi];
#define ntd(a, h, w) \
resize(a, h, w); \
rep(ntdhi, h) rep(ntdwi, w) cin >> a[ntdhi][ntdwi], a[ntdhi][ntdwi]--;
#define ntp(a, h, w) \
resize(a, h + 2, w + 2); \
fill(a, '#'); \
rep(ntphi, 1, h + 1) rep(ntpwi, 1, w + 1) cin >> a[ntphi][ntpwi];
// デバッグ
#define sp << " " <<
#define deb1(x) debugName(x) << " = " << out_m2(x)
#define deb2(x, ...) deb1(x) << ", " << deb1(__VA_ARGS__)
#define deb3(x, ...) deb1(x) << ", " << deb2(__VA_ARGS__)
#define deb4(x, ...) deb1(x) << ", " << deb3(__VA_ARGS__)
#define deb5(x, ...) deb1(x) << ", " << deb4(__VA_ARGS__)
#define deb6(x, ...) deb1(x) << ", " << deb5(__VA_ARGS__)
#define deb7(x, ...) deb1(x) << ", " << deb6(__VA_ARGS__)
#define deb8(x, ...) deb1(x) << ", " << deb7(__VA_ARGS__)
#define deb9(x, ...) deb1(x) << ", " << deb8(__VA_ARGS__)
#define deb10(x, ...) deb1(x) << ", " << deb9(__VA_ARGS__)
#define o_ebug(o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, name, ...) name
#ifdef _DEBUG
#define deb(...) \
cerr << o_ebug(__VA_ARGS__, deb10, deb9, deb8, deb7, deb6, deb5, deb4, deb3, \
deb2, deb1)(__VA_ARGS__) \
<< endl
#else
#define deb(...) ;
#endif
#define debugline(x) \
cerr << x << " " \
<< "(L:" << __LINE__ << ")" << '\n'
//@formatter:off
// よく使うクラス、構造体
// graphでredefineしているため、書き換えたら向こうも書き換える
struct unionfind {
vector<ll> par;
vector<ll> siz;
vector<ll> es;
ll n, trees; // 連結グループの数(親の種類)
unionfind(ll n) : n(n), trees(n) {
par.resize(n);
siz.resize(n);
es.resize(n);
for (ll i = 0; i < n; i++) {
par[i] = i;
siz[i] = 1;
}
}
ll root(ll x) {
if (par[x] == x) {
return x;
} else {
return par[x] = root(par[x]);
}
}
ll operator()(ll x) { return root(x); }
bool unite(ll x, ll y) {
x = root(x);
y = root(y);
es[x]++;
if (x == y)
return false;
if (siz[x] > siz[y])
swap(x, y);
trees--;
par[x] = y;
siz[y] += siz[x];
es[y] += es[x];
return true;
}
bool same(ll x, ll y) { return root(x) == root(y); }
ll size(ll x) { return siz[root(x)]; }
ll esize(ll x) { return es[root(x)]; }
vi sizes() {
vi cou(n);
vi ret;
ret.reserve(n);
rep(i, n) { cou[root(i)]++; }
rep(i, n) {
if (cou[i])
ret.push_back(cou[i]);
}
return ret;
}
// つながりを無向グラフと見なし、xが閉路に含まれるか判定
bool close(ll x) { return esize(x) >= size(x); }
vector<vi> sets() {
vi ind(n, -1);
ll i = 0;
vvi(res, trees);
rep(j, n) {
ll r = root(j);
if (ind[r] == -1)
ind[r] = i++;
res[ind[r]].push_back(j);
}
rep(i, trees) {
ll r = root(res[i][0]);
if (res[i][0] == r)
continue;
rep(j, 1, sz(res[i])) {
if (res[i][j] == r) {
swap(res[i][0], res[i][j]);
break;
}
}
}
return res;
}
}; //@formatter:off
using bll = __int128;
using u32 = unsigned;
using u64 = unsigned long long;
using u128 = __uint128_t;
std::ostream &operator<<(std::ostream &dest, __int128_t value) {
std::ostream::sentry s(dest);
if (s) {
__uint128_t tmp = value < 0 ? -value : value;
char buffer[128];
char *d = std::end(buffer);
do {
--d;
*d = "0123456789"[tmp % 10];
tmp /= 10;
} while (tmp != 0);
if (value < 0) {
--d;
*d = '-';
}
ll len = std::end(buffer) - d;
if (dest.rdbuf()->sputn(d, len) != len) {
dest.setstate(std::ios_base::badbit);
}
}
return dest;
}
//__int128 toi128(string &s) { __int128 ret = 0; for (ll i = 0; i <
//s.length(); ++i) if ('0' <= s[i] && s[i] <= '9') ret = 10 *
//ret + s[i] - '0'; return ret;}
// エラー
void ole() {
#ifdef _DEBUG
debugline("ole");
exit(0);
#endif
string a = "a";
rep(i, 30) a += a;
rep(i, 1 << 17) cout << a << endl;
cout << "OLE 出力長制限超過" << endl;
exit(0);
}
void re() {
assert(0 == 1);
exit(0);
}
void tle() {
while (inf)
cout << inf << endl;
}
// 便利関数
// テスト用
#define rand xor128_
unsigned long xor128_(void) {
static unsigned long x = 123456789, y = 362436069, z = 521288629,
w = 88675123;
unsigned long t;
t = (x ^ (x << 11));
x = y;
y = z;
z = w;
return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}
char ranc() { return (char)('a' + rand() % 26); }
ll rand(ll min, ll max) {
assert(min <= max);
if (min >= 0 && max >= 0) {
return rand() % (max + 1 - min) + min;
} else if (max < 0) {
return -rand(-max, -min);
} else {
if (rand() % 2) {
return rand(0, max);
} else {
return -rand(0, -min);
}
}
}
vi ranv(ll n, ll min, ll max) {
vi v(n);
rep(i, n) v[i] = rand(min, max);
return v;
}
str ransu(ll n) {
str s;
rep(i, n) s += (char)rand('A', 'Z');
return s;
}
str ransl(ll n) {
str s;
rep(i, n) s += (char)rand('a', 'z');
return s;
}
// 単調増加
vi ranvinc(ll n, ll min, ll max) {
vi v(n);
bool bad = 1;
while (bad) {
bad = 0;
v.resize(n);
rep(i, n) {
if (i && min > max - v[i - 1]) {
bad = 1;
break;
}
if (i)
v[i] = v[i - 1] + rand(min, max - v[i - 1]);
else
v[i] = rand(min, max);
}
}
return v;
}
// 便利 汎用
void ranvlr(ll n, ll min, ll max, vi &l, vi &r) {
l.resize(n);
r.resize(n);
rep(i, n) {
l[i] = rand(min, max);
r[i] = l[i] + rand(0, max - l[i]);
}
}
template <class T> vector<pair<T, int>> run_length(vector<T> &a) {
vector<pair<T, int>> ret;
ret.eb(a[0], 1);
rep(i, 1, sz(a)) {
if (ret.back().fi == a[i]) {
ret.back().se++;
} else {
ret.eb(a[i], 1);
}
}
return ret;
}
vector<pair<char, ll>> run_length(string &a) {
vector<pair<char, ll>> ret;
ret.eb(a[0], 1);
rep(i, 1, sz(a)) {
if (ret.back().fi == a[i]) {
ret.back().se++;
} else {
ret.eb(a[i], 1);
}
}
return ret;
}
template <class F> ll mgr(ll ok, ll ng, F f) {
bool han = true;
if (ok < ng)
while (ng - ok > 1) {
ll mid = (ok + ng) >> 1;
if (f(mid))
ok = mid, han = true;
else
ng = mid, han = false;
deb(mid, han);
}
else
while (ok - ng > 1) {
ll mid = (ok + ng) >> 1;
if (f(mid))
ok = mid, han = true;
else
ng = mid, han = false;
deb(mid, han);
}
return ok;
}
template <class F> dou mgrd(dou ok, dou ng, F f) {
bool han = true;
if (ok < ng)
rep(i, 100) {
dou mid = (ok + ng) / 2;
if (f(mid))
ok = mid, han = true;
else
ng = mid, han = false;
if (i < 30)
deb(mid, han);
}
else
rep(i, 100) {
dou mid = (ok + ng) / 2;
if (f(mid))
ok = mid, han = true;
else
ng = mid, han = false;
if (i < 30)
deb(mid, han);
}
return ok;
}
// strを整数として比較
string smax(str &a, str b) {
if (sz(a) < sz(b)) {
return b;
} else if (sz(a) > sz(b)) {
return a;
} else if (a < b)
return b;
else
return a;
}
// strを整数として比較
string smin(str &a, str b) {
if (sz(a) > sz(b)) {
return b;
} else if (sz(a) < sz(b)) {
return a;
} else if (a > b)
return b;
else
return a;
}
// エラー-1
template <typename W, typename T> ll find(vector<W> &a, int l, const T key) {
rep(i, l, sz(a)) if (a[i] == key) return i;
return -1;
}
template <typename W, typename T> ll find(vector<W> &a, const T key) {
rep(i, sz(a)) if (a[i] == key) return i;
return -1;
}
template <typename W, typename T> P find(vector<vector<W>> &a, const T key) {
rep(i, sz(a)) rep(j, sz(a[0])) if (a[i][j] == key) return mp(i, j);
return mp(-1, -1);
}
template <typename W, typename U>
T find(vector<vector<vector<W>>> &a, const U key) {
rep(i, sz(a)) rep(j, sz(a[0]))
rep(k, sz(a[0][0])) if (a[i][j][k] == key) return mt(i, j, k);
return mt(-1, -1, -1);
}
// stringも書く
int find(string &s, const string key) {
int klen = sz(key);
rep(i, sz(s) - klen + 1) {
if (s[i] != key[0])
continue;
if (s.substr(i, klen) == key) {
return i;
}
}
return -1;
}
int find(string &s, int l, const string key) {
int klen = sz(key);
rep(i, l, sz(s) - klen + 1) {
if (s[i] != key[0])
continue;
if (s.substr(i, klen) == key) {
return i;
}
}
return -1;
}
int find(string &s, const char key) {
rep(i, sz(s)) {
if (s[i] == key)
return i;
}
return -1;
}
int find(string &s, int l, const char key) {
rep(i, l, sz(s)) {
if (s[i] == key)
return i;
}
return -1;
}
template <typename W, typename T> ll count2(W &a, const T k) { return a == k; }
template <typename W, typename T> ll count2(vector<W> &a, const T k) {
ll ret = 0;
fora(v, a) ret += count2(v, k);
return ret;
}
template <typename W, typename T> ll count(vector<W> &a, const T k) {
ll ret = 0;
fora(v, a) ret += count2(v, k);
return ret;
}
vi count(vi &a) {
int ma = 0;
fora(v, a) {
if (ma < v)
ma = v;
}
vi res(ma + 1);
fora(v, a) { res[v]++; }
return res;
}
ll count(str &a, str k) {
ll ret = 0, len = k.length();
auto pos = a.find(k);
while (pos != string::npos)
pos = a.find(k, pos + len), ++ret;
return ret;
}
vi count(str &a) {
vi cou(26);
char c = 'a';
if ('A' <= a[0] && a[0] <= 'Z')
c = 'A';
rep(i, sz(a))++ cou[a[i] - c];
return cou;
}
#define couif count_if
// algorythm
ll rev(ll a) {
ll res = 0;
while (a) {
res *= 10;
res += a % 10;
a /= 10;
}
return res;
}
template <class T> void rev(vector<T> &a) { reverse(all(a)); }
template <class U> void rev(vector<vector<U>> &a) {
vector<vector<U>> b(sz(a[0]), vector<U>(sz(a)));
rep(h, sz(a)) rep(w, sz(a[0])) b[w][h] = a[h][w];
a = b;
}
void rev(string &a) { reverse(all(a)); }
constexpr ll p10[] = {1,
10,
100,
1000,
10000,
100000,
1000000,
10000000,
100000000,
1000000000,
10000000000ll,
100000000000ll,
1000000000000ll,
10000000000000ll,
100000000000000ll,
1000000000000000ll,
10000000000000000ll,
100000000000000000ll,
1000000000000000000ll};
ll get(ll a, ll keta) { return (a / (ll)pow(10, keta)) % 10; }
// 0は0桁
ll keta(ll v) {
if (v < p10[9]) {
if (v < p10[4]) {
if (v < p10[2]) {
if (v < p10[1]) {
if (v < p10[0])
return 0;
else
return 1;
} else
return 2;
} else {
if (v < p10[3])
return 3;
else
return 4;
}
} else {
if (v < p10[7]) {
if (v < p10[5])
return 5;
else if (v < p10[6])
return 6;
else
return 7;
} else {
if (v < p10[8])
return 8;
else
return 9;
}
}
} else {
if (v < p10[13]) {
if (v < p10[11]) {
if (v < p10[10])
return 10;
else
return 11;
} else {
if (v < p10[12])
return 12;
else
return 13;
}
} else {
if (v < p10[15]) {
if (v < p10[14])
return 14;
else
return 15;
} else {
if (v < p10[17]) {
if (v < p10[16])
return 16;
else
return 17;
} else {
if (v < p10[18])
return 18;
else
return 19;
}
}
}
}
}
ll dsum(ll v, ll sin = 10) {
ll ret = 0;
for (; v; v /= sin)
ret += v % sin;
return ret;
}
ll mask10(ll v) { return p10[v] - 1; }
// 変換系
//[v] := iとなるようなvectorを返す
// 存在しない物は-1
template <class T> auto keys(T &a) {
vector<decltype((a.begin())->fi)> res;
for (auto &&k : a)
res.push_back(k.fi);
return res;
}
template <class T> auto values(T &a) {
vector<decltype((a.begin())->se)> res;
for (auto &&k : a)
res.push_back(k.se);
return res;
}
template <class T, class U> bool chma(T &a, const U &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T, class U> bool chmi(T &a, const U &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template <class T> T min(T a, signed b) { return a < b ? a : b; }
template <class T> T max(T a, signed b) { return a < b ? b : a; }
template <class T> T min(T a, T b, T c) {
return a >= b ? b >= c ? c : b : a >= c ? c : a;
}
template <class T> T max(T a, T b, T c) {
return a <= b ? b <= c ? c : b : a <= c ? c : a;
}
template <class T> T min(vector<T> &a) { return *min_element(all(a)); }
template <class T> T mini(vector<T> &a) {
return min_element(all(a)) - a.begin();
}
template <class T> T min(vector<T> &a, ll n) {
return *min_element(a.begin(), a.begin() + min(n, sz(a)));
}
template <class T> T min(vector<T> &a, ll s, ll n) {
return *min_element(a.begin() + s, a.begin() + min(n, sz(a)));
}
template <class T> T max(vector<T> &a) { return *max_element(all(a)); }
template <class T, class U> T max(vector<T> &a, vector<U> &b) {
return max(*max_element(all(a)), *max_element(all(b)));
}
template <class T> T maxi(vector<T> &a) {
return max_element(all(a)) - a.begin();
}
template <class T> T max(vector<T> &a, ll n) {
return *max_element(a.begin(), a.begin() + min(n, sz(a)));
}
template <class T> T max(vector<T> &a, ll s, ll n) {
return *max_element(a.begin() + s, a.begin() + min(n, sz(a)));
}
template <typename A, size_t N> A max(A (&a)[N]) {
A res = a[0];
rep(i, N) res = max(res, a[i]);
return res;
}
template <typename A, size_t N, size_t O> A max(A (&a)[N][O]) {
A res = max(a[0]);
rep(i, N) res = max(res, max(a[i]));
return res;
}
template <typename A, size_t N, size_t O, size_t P> A max(A (&a)[N][O][P]) {
A res = max(a[0]);
rep(i, N) res = max(res, max(a[i]));
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q>
A max(A (&a)[N][O][P][Q], const T &v) {
A res = max(a[0]);
rep(i, N) res = max(res, max(a[i]));
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R>
A max(A (&a)[N][O][P][Q][R]) {
A res = max(a[0]);
rep(i, N) res = max(res, max(a[i]));
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R,
size_t S>
A max(A (&a)[N][O][P][Q][R][S]) {
A res = max(a[0]);
rep(i, N) res = max(res, max(a[i]));
return res;
}
template <typename A, size_t N> A min(A (&a)[N]) {
A res = a[0];
rep(i, N) res = min(res, a[i]);
return res;
}
template <typename A, size_t N, size_t O> A min(A (&a)[N][O]) {
A res = min(a[0]);
rep(i, N) res = min(res, min(a[i]));
return res;
}
template <typename A, size_t N, size_t O, size_t P> A min(A (&a)[N][O][P]) {
A res = min(a[0]);
rep(i, N) res = min(res, min(a[i]));
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q>
A min(A (&a)[N][O][P][Q], const T &v) {
A res = min(a[0]);
rep(i, N) res = min(res, min(a[i]));
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R>
A min(A (&a)[N][O][P][Q][R]) {
A res = min(a[0]);
rep(i, N) res = min(res, min(a[i]));
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R,
size_t S>
A min(A (&a)[N][O][P][Q][R][S]) {
A res = min(a[0]);
rep(i, N) res = min(res, min(a[i]));
return res;
}
template <class T> T sum(vector<T> &v, ll s, ll t) {
T ret = 0;
rep(i, s, min(sz(v), t)) ret += v[i];
return ret;
}
template <class T> T sum(vector<T> &v, ll t = inf) { return sum(v, 0, t); }
template <class T> T sum(vector<vector<T>> &v, int s, int t) {
T ret = 0;
rep(i, s, min(sz(v), t)) ret += sum(v[i]);
return ret;
}
template <class T> T sum(vector<vector<T>> &v, int t = inf) {
return sum(v, 0, t);
}
template <class T> T sum(vector<vector<vector<T>>> &v, int s, int t) {
T ret = 0;
rep(i, s, min(sz(v), t)) ret += sum(v[i]);
return ret;
}
template <class T> T sum(vector<vector<vector<T>>> &v, int t = inf) {
return sum(v, 0, t);
}
template <class T> T sum(vector<vector<vector<vector<T>>>> &v, int s, int t) {
T ret = 0;
rep(i, s, min(sz(v), t)) ret += sum(v[i]);
return ret;
}
template <class T> T sum(vector<vector<vector<vector<T>>>> &v, int t = inf) {
return sum(v, 0, t);
}
template <class T>
T sum(vector<vector<vector<vector<vector<T>>>>> &v, int s, int t) {
T ret = 0;
rep(i, s, min(sz(v), t)) ret += sum(v[i]);
return ret;
}
template <class T>
T sum(vector<vector<vector<vector<vector<T>>>>> &v, int t = inf) {
return sum(v, 0, t);
}
template <class T> auto sum(priority_queue<T, vector<T>, greater<T>> &r) {
auto q = r;
T ret = 0;
while (sz(q)) {
ret += q.top();
q.pop();
}
return ret;
}
template <class T> auto sum(priority_queue<T> &r) {
auto q = r;
T ret = 0;
while (sz(q)) {
ret += q.top();
q.pop();
}
return ret;
}
// template<class T, class U, class... W> auto sumn(vector<T> &v, U head, W...
// tail) { auto ret = sum(v[0], tail...); rep(i, 1, min(sz(v), head))ret
// += sum(v[i], tail...); return ret;}
vi v_i(vi &a) {
int n = max(a) + 1;
vi ret(n, -1);
rep(i, sz(a)) { ret[a[i]] = i; }
return ret;
}
void clear(PQ &q) { q = PQ(); }
void clear(priority_queue<int> &q) { q = priority_queue<int>(); }
template <class T> void clear(queue<T> &q) {
while (q.size())
q.pop();
}
template <class T> T *negarr(ll size) {
T *body = (T *)malloc((size * 2 + 1) * sizeof(T));
return body + size;
}
template <class T> T *negarr2(ll h, ll w) {
double **dummy1 = new double *[2 * h + 1];
double *dummy2 = new double[(2 * h + 1) * (2 * w + 1)];
dummy1[0] = dummy2 + w;
for (ll i = 1; i <= 2 * h + 1; ++i) {
dummy1[i] = dummy1[i - 1] + 2 * w + 1;
}
double **a = dummy1 + h;
return a;
}
// imoは0-indexed
// ruiは1-indexed
template <class T> vector<T> imo(vector<T> &v) {
vector<T> ret = v;
rep(i, sz(ret) - 1) ret[i + 1] += ret[i];
return ret;
}
// kと同じものの数
template <class T, class U> vi imo(vector<T> &a, U k) {
vector<T> ret = a;
rep(i, sz(ret)) ret[i] = a[i] == k;
rep(i, sz(ret) - 1) ret[i + 1] += ret[i];
return ret;
}
template <class T> vector<T> imox(vector<T> &v) {
vector<T> ret = v;
rep(i, sz(ret) - 1) ret[i + 1] ^= ret[i];
return ret;
}
// 漸化的に最小を持つ
template <class T> vector<T> imi(vector<T> &v) {
vector<T> ret = v;
rep(i, sz(ret) - 1) chmi(ret[i + 1], ret[i]);
return ret;
}
template <class T> vector<T> ima(vector<T> &v) {
vector<T> ret = v;
rep(i, sz(ret) - 1) chma(ret[i + 1], ret[i]);
return ret;
}
// template<class T> vector<T> rimi(vector<T> &v) { vector<T> ret = v; rer(i,
// sz(ret) - 1, 1)chmi(ret[i - 1], ret[i]); return ret;} template<class T>
// vector<T> rima(vector<T> &v) { vector<T> ret = v; rer(i, sz(ret) - 1,
// 1)chma(ret[i - 1], ret[i]); return ret;}
template <class T> struct ruiC {
vector<T> rui;
ruiC(vector<T> &ru) : rui(ru) {}
/*先頭0*/
ruiC() : rui(1, 0) {}
T operator()(ll l, ll r) {
if (l > r) {
cerr << "ruic ";
deb(l, r);
assert(0);
}
return rui[r] - rui[l];
}
T operator()(int r) { return operator()(0, r); }
/*ruiv[]をruic[]に変えた際意味が変わるのがまずいため()と統一*/
/*単体iを返す 累積でないことに注意(seg木との統一でこうしている)*/
// T operator[](ll i) { return rui[i + 1] - rui[i]; }
T operator[](ll i) { return rui[i]; }
/*0から順に追加される必要がある*/
void operator+=(T v) { rui.push_back(rui.back() + v); }
void add(int i, T v) {
if (sz(rui) - 1 != i)
ole();
operator+=(v);
}
T back() { return rui.back(); }
ll size() { return rui.size(); }
auto begin() { return rui.begin(); }
auto end() { return rui.end(); }
};
template <class T> struct ruimax {
template <typename Monoid> struct SegmentTree { /*pairで処理*/
int sz;
vector<Monoid> seg;
const Monoid M1 = mp(MIN(T), -1);
Monoid f(Monoid a, Monoid b) { return max(a, b); }
void build(vector<T> &a) {
int n = sz(a);
sz = 1;
while (sz < n)
sz <<= 1;
seg.assign(2 * sz, M1);
rep(i, n) { seg[i + sz] = mp(a[i], i); }
for (int k = sz - 1; k > 0; k--) {
seg[k] = f(seg[k << 1], seg[(k << 1) | 1]);
}
}
Monoid query(int a, int b) {
Monoid L = M1, R = M1;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1)
L = f(L, seg[a++]);
if (b & 1)
R = f(seg[--b], R);
}
return f(L, R);
}
Monoid operator[](const int &k) const { return seg[k + sz]; }
};
private:
vector<T> ve;
SegmentTree<pair<T, int>> seg;
vector<T> rv;
vector<T> ri;
bool build = false;
public:
int n;
ruimax(vector<T> &a) : ve(a), n(sz(a)) {
int index = -1;
int ma = MIN(T);
rv.resize(n + 1);
ri.resize(n + 1);
rv[0] = -linf;
ri[0] = -1;
rep(i, n) {
if (chma(ma, a[i])) {
index = i;
}
rv[i + 1] = ma;
ri[i + 1] = index;
}
}
T operator()(int l, int r) {
if (!(l <= r && 0 <= l && r <= n)) {
deb(l, r, n);
assert(0);
}
if (l == 0) {
return rv[r];
} else {
if (!build)
seg.build(ve), build = true;
return seg.query(l, r).first;
}
}
T operator()(int r = inf) { return operator()(0, min(r, n)); }
T operator[](int r) { return operator()(0, r); }
T getv(int l, int r) { return operator()(l, r); }
T geti(int l, int r) {
assert(l <= r && 0 <= l && r <= n);
if (l == 0) {
return ri[r];
} else {
if (!build)
seg.build(ve), build = true;
return seg.query(l, r).second;
}
}
T geti(int r = inf) { return geti(0, min(r, n)); };
T getv(int r = inf) { return getv(0, min(r, n)); };
auto begin() { return rv.begin(); }
auto end() { return rv.end(); }
};
template <class T> struct ruimin {
template <typename Monoid> struct SegmentTree { /*pairで処理*/
int sz;
vector<Monoid> seg;
const Monoid M1 = mp(MAX(T), -1);
Monoid f(Monoid a, Monoid b) { return min(a, b); }
void build(vector<T> &a) {
int n = sz(a);
sz = 1;
while (sz < n)
sz <<= 1;
seg.assign(2 * sz, M1);
rep(i, n) { seg[i + sz] = mp(a[i], i); }
for (int k = sz - 1; k > 0; k--) {
seg[k] = f(seg[k << 1], seg[(k << 1) | 1]);
}
}
Monoid query(int a, int b) {
Monoid L = M1, R = M1;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1)
L = f(L, seg[a++]);
if (b & 1)
R = f(seg[--b], R);
}
return f(L, R);
}
Monoid operator[](const int &k) const { return seg[k + sz]; }
};
private:
vector<T> ve;
SegmentTree<pair<T, int>> seg;
vector<T> rv;
vector<T> ri;
bool build = false;
int n;
public:
ruimin(vector<T> &a) : ve(a), n(sz(a)) {
int index = -1;
int mi = MAX(T);
rv.resize(n + 1);
ri.resize(n + 1);
rv[0] = linf;
ri[0] = -1;
rep(i, n) {
if (chmi(mi, a[i])) {
index = i;
}
rv[i + 1] = mi;
ri[i + 1] = index;
}
}
T operator()(int l, int r) {
assert(l <= r && 0 <= l && r <= n);
if (l == 0) {
return rv[r];
} else {
if (!build)
seg.build(ve), build = true;
return seg.query(l, r).first;
}
}
T operator()(int r = inf) { return operator()(0, min(r, n)); }
T operator[](int r) { return operator()(0, r); }
T getv(int l, int r) { return operator()(l, r); }
T geti(int l, int r) {
assert(l <= r && 0 <= l && r <= n);
if (l == 0) {
return ri[r];
} else {
if (!build)
seg.build(ve), build = true;
return seg.query(l, r).second;
}
}
T geti(int r = inf) { return geti(0, min(r, n)); };
T getv(int r = inf) { return getv(0, min(r, n)); };
auto begin() { return rv.begin(); }
auto end() { return rv.end(); }
};
template <class T> ostream &operator<<(ostream &os, ruiC<T> a) {
fora(v, a.rui) os << v << " ";
return os;
}
template <class T> vector<T> ruiv(vector<T> &a) {
vector<T> ret(a.size() + 1);
rep(i, a.size()) ret[i + 1] = ret[i] + a[i];
return ret;
}
template <class T> ruiC<T> ruic() { return ruiC<T>(); }
template <class T> ruiC<T> ruic(vector<T> &a) {
vector<T> ret = ruiv(a);
return ruiC<T>(ret);
}
vvi() ruib(vi &a) {
vvi(res, 61, sz(a) + 1);
rep(k, 61) {
rep(i, sz(a)) { res[k][i + 1] = res[k][i] + ((a[i] >> k) & 1); }
}
return res;
}
vector<ruiC<int>> ruibc(vi &a) {
vector<ruiC<int>> ret(61);
vvi(res, 61, sz(a));
rep(k, 61) {
rep(i, sz(a)) { res[k][i] = (a[i] >> k) & 1; }
ret[k] = ruic(res[k]);
}
return ret;
}
vector<ll> ruiv(string &a) {
if (sz(a) == 0)
return vi(1);
ll dec = ('0' <= a[0] && a[0] <= '9') ? '0' : 0;
vector<ll> ret(a.size() + 1);
rep(i, a.size()) ret[i + 1] = ret[i] + a[i] - dec;
return ret;
}
ruiC<ll> ruic(string &a) {
vector<ll> ret = ruiv(a);
return ruiC<ll>(ret);
}
// kと同じものの数
template <class T, class U> vi ruiv(T &a, U k) {
vi ret(a.size() + 1);
rep(i, a.size()) ret[i + 1] = ret[i] + (a[i] == k);
return ret;
}
template <class T, class U> ruiC<ll> ruic(T &a, U k) {
vi ret = ruiv(a, k);
return ruiC<ll>(ret);
}
// h query
template <class T> vector<T> imoh(vector<vector<T>> &v, int w) {
vector<T> ret(sz(v));
rep(h, sz(ret)) { ret[h] = v[h][w]; }
rep(i, sz(ret) - 1) { ret[i + 1] += ret[i]; }
return ret;
}
template <class T> vector<T> ruih(vector<vector<T>> &v, int w) {
vector<T> ret(sz(v) + 1);
rep(h, sz(v)) { ret[h + 1] = v[h][w]; }
rep(i, sz(v)) { ret[i + 1] += ret[i]; }
return ret;
}
template <class T> ruiC<T> ruihc(vector<vector<T>> &a, int w) {
vector<T> ret = ruih(a, w);
return ruiC<T>(ret);
}
// xor
template <class T> struct ruixC {
const vector<T> rui;
ruixC(vector<T> &ru) : rui(ru) {}
T operator()(ll l, ll r) {
if (l > r) {
cerr << "ruiXc ";
deb(l, r);
assert(0);
}
return rui[r] ^ rui[l];
}
T operator[](ll i) { return rui[i]; }
T back() { return rui.back(); }
ll size() { return rui.size(); }
};
template <class T> vector<T> ruix(vector<T> &a) {
vector<T> ret(a.size() + 1);
rep(i, a.size()) ret[i + 1] = ret[i] ^ a[i];
return ret;
}
template <class T> ruixC<ll> ruixc(vector<T> &a) {
vi ret = ruix(a);
return ruixC<ll>(ret);
}
template <class T> vector<T> ruim(vector<T> &a) {
vector<T> res(a.size() + 1, 1);
rep(i, a.size()) res[i + 1] = res[i] * a[i];
return res;
}
// 漸化的に最小を1indexで持つ
template <class T> vector<T> ruimi(vector<T> &a) {
ll n = sz(a);
vector<T> ret(n + 1);
rep(i, 1, n) {
ret[i] = a[i - 1];
chmi(ret[i + 1], ret[i]);
}
return ret;
}
// template<class T> T *rrui(vector<T> &a) {
// 右から左にかけての半開区間 (-1 n-1]
template <class T> struct rruiC {
vector<T> rui;
int n;
rruiC(vector<T> &a) : n(sz(a)) {
rui.resize(n + 1);
rer(i, n - 1) { rui[i] = rui[i + 1] + a[i]; }
}
/*[r l)*/
T operator()(int r, int l) {
r++;
l++;
assert(l <= r && l >= 0 && r <= n);
return rui[l] - rui[r];
}
T operator()(int l) { return operator()(n - 1, l); }
T operator[](int i) { return operator()(i); }
};
template <class T> ostream &operator<<(ostream &os, rruiC<T> a) {
fora(v, a.rui) os << v << " ";
return os;
}
#define rrui rruic
template <class T> rruiC<T> rruic(vector<T> &a) { return rruiC<T>(a); }
// 掛け算
template <class T> struct ruimulC {
vector<T> rv;
int n;
ruimulC(vector<T> &a) : rv(a), n(sz(a)) {
rv.resize(n + 1);
rv[0] = 1;
rep(i, n) { rv[i + 1] = a[i] * rv[i]; }
}
ruimulC() : n(0) {
rv.resize(n + 1);
rv[0] = 1;
}
void operator+=(T v) {
rv.push_back(rv.back() * v);
n++;
}
T operator()(int l, int r) {
assert(l <= r && 0 <= l && r <= n);
return rv[r] / rv[l];
}
T operator()(int r = inf) { return operator()(0, min(r, n)); }
T operator[](int r) { return operator()(0, r); }
auto begin() { return rv.begin(); }
auto end() { return rv.end(); }
};
template <class T> ruimulC<T> ruimul(vector<T> &a) { return ruimulC<T>(a); }
template <class T> ruimulC<T> ruimul() {
vector<T> a;
return ruimulC<T>(a);
}
/*@formatter:off*/
template <class T> T *rruim(vector<T> &a) {
ll len = a.size();
T *body = (T *)malloc((len + 1) * sizeof(T));
T *res = body + 1;
res[len - 1] = 1;
rer(i, len - 1) res[i - 1] = res[i] * a[i];
return res;
}
template <class T, class U> void inc(pair<T, U> &a, U v = 1) {
a.first += v, a.second += v;
}
template <class T, class U> void inc(T &a, U v = 1) { a += v; }
template <class T, class U> void inc(vector<T> &a, U v = 1) {
for (auto &u : a)
inc(u, v);
}
template <class T, class U> void dec(T &a, U v = 1) { a -= v; }
template <class T, class U> void dec(vector<T> &a, U v = 1) {
for (auto &u : a)
dec(u, v);
}
template <class U> void dec(string &a, U v = 1) {
for (auto &u : a)
dec(u, v);
}
template <class T, class U, class W>
void dec(vector<T> &a, vector<U> &b, W v = 1) {
for (auto &u : a)
dec(u, v);
for (auto &u : b)
dec(u, v);
}
template <class T, class U, class W>
void dec(vector<T> &a, vector<U> &b, vector<W> &c) {
for (auto &u : a)
dec(u, 1);
for (auto &u : b)
dec(u, 1);
for (auto &u : c)
dec(u, 1);
}
bool ins(ll h, ll w, ll H, ll W) { return h >= 0 && w >= 0 && h < H && w < W; }
bool ins(ll l, ll v, ll r) { return l <= v && v < r; }
template <class T> bool ins(vector<T> &a, ll i, ll j = 0) {
return ins(0, i, sz(a)) && ins(0, j, sz(a));
}
#define inside ins
ll u(ll a) { return a < 0 ? 0 : a; }
template <class T> vector<T> u(const vector<T> &a) {
vector<T> ret = a;
fora(v, ret) v = u(v);
return ret;
}
// 添え字を返す
template <class F> ll goldd_l(ll left, ll right, F calc) {
double GRATIO = 1.6180339887498948482045868343656;
ll lm = left + (ll)((right - left) / (GRATIO + 1.0));
ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0));
ll fl = calc(lm);
ll fr = calc(rm);
while (right - left > 10) {
if (fl < fr) {
right = rm;
rm = lm;
fr = fl;
lm = left + (ll)((right - left) / (GRATIO + 1.0));
fl = calc(lm);
} else {
left = lm;
lm = rm;
fl = fr;
rm = lm + (ll)((right - lm) / (GRATIO + 1.0));
fr = calc(rm);
}
}
ll minScore = MAX(ll);
ll resIndex = left;
for (ll i = left; i < right + 1; ++i) {
ll score = calc(i);
if (minScore > score) {
minScore = score;
resIndex = i;
}
}
return resIndex;
}
template <class F> ll goldt_l(ll left, ll right, F calc) {
double GRATIO = 1.6180339887498948482045868343656;
ll lm = left + (ll)((right - left) / (GRATIO + 1.0));
ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0));
ll fl = calc(lm);
ll fr = calc(rm);
while (right - left > 10) {
if (fl > fr) {
right = rm;
rm = lm;
fr = fl;
lm = left + (ll)((right - left) / (GRATIO + 1.0));
fl = calc(lm);
} else {
left = lm;
lm = rm;
fl = fr;
rm = lm + (ll)((right - lm) / (GRATIO + 1.0));
fr = calc(rm);
}
}
if (left > right) {
ll l = left;
left = right;
right = l;
}
ll maxScore = MIN(ll);
ll resIndex = left;
for (ll i = left; i < right + 1; ++i) {
ll score = calc(i);
if (maxScore < score) {
maxScore = score;
resIndex = i;
}
}
return resIndex;
}
/*loopは200にすればおそらく大丈夫 余裕なら300に*/
template <class F> dou goldd_d(dou left, dou right, F calc, ll loop = 200) {
dou GRATIO = 1.6180339887498948482045868343656;
dou lm = left + ((right - left) / (GRATIO + 1.0));
dou rm = lm + ((right - lm) / (GRATIO + 1.0));
dou fl = calc(lm);
dou fr = calc(rm); /*200にすればおそらく大丈夫*/ /*余裕なら300に*/
ll k = 141;
loop++;
while (--loop) {
if (fl < fr) {
right = rm;
rm = lm;
fr = fl;
lm = left + ((right - left) / (GRATIO + 1.0));
fl = calc(lm);
} else {
left = lm;
lm = rm;
fl = fr;
rm = lm + ((right - lm) / (GRATIO + 1.0));
fr = calc(rm);
}
}
return left;
}
template <class F> dou goldt_d(dou left, dou right, F calc, ll loop = 200) {
double GRATIO = 1.6180339887498948482045868343656;
dou lm = left + ((right - left) / (GRATIO + 1.0));
dou rm = lm + ((right - lm) / (GRATIO + 1.0));
dou fl = calc(lm);
dou fr = calc(rm);
loop++;
while (--loop) {
if (fl > fr) {
right = rm;
rm = lm;
fr = fl;
lm = left + ((right - left) / (GRATIO + 1.0));
fl = calc(lm);
} else {
left = lm;
lm = rm;
fl = fr;
rm = lm + ((right - lm) / (GRATIO + 1.0));
fr = calc(rm);
}
}
return left;
}
// l ~ rを複数の区間に分割し、極致を与えるiを返す time-20 msまで探索
/*@formatter:on*/
template <class F> ll goldd_ls(ll l, ll r, F calc, ll time = 2000) {
auto lim = milliseconds(time - 20);
ll mini = 0, minv = MAX(ll); /*区間をk分割する*/
rep(k, 1, inf) {
auto s = system_clock::now();
ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/
ll nl = l;
ll nr = l + haba;
rep(i, k) {
ll ni = goldd_l(nl, nr, calc);
if (chmi(minv, calc(ni)))
mini = ni;
nl = nr;
nr = nl + haba;
}
auto end = system_clock::now();
auto part = duration_cast<milliseconds>(end - s);
auto elapsed = duration_cast<milliseconds>(end - start_time);
if (elapsed + part * 2 >= lim) {
break;
}
}
return mini;
}
/*@formatter:off*/
template <class F> ll goldt_ls(ll l, ll r, F calc, ll time = 2000) {
auto lim = milliseconds(time - 20);
ll maxi = 0, maxv = MIN(ll); /*区間をk分割する*/
rep(k, 1, inf) {
auto s = system_clock::now();
ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/
ll nl = l;
ll nr = l + haba;
rep(i, k) {
ll ni = goldt_l(nl, nr, calc);
if (chma(maxv, calc(ni)))
maxi = ni;
nl = nr;
nr = nl + haba;
}
auto end = system_clock::now();
auto part = duration_cast<milliseconds>(end - s);
auto elapsed = duration_cast<milliseconds>(end - start_time);
if (elapsed + part * 2 >= lim) {
break;
}
}
return maxi;
}
template <class F>
dou goldd_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を持つ*/
auto lim = milliseconds(time - 20);
dou mini = 0, minv = MAX(dou); /*区間をk分割する*/
rep(k, 1, inf) {
auto s = system_clock::now();
dou haba = (r - l) / k;
dou nl = l;
dou nr = l + haba;
rep(i, k) {
dou ni = goldd_d(nl, nr, calc);
if (chmi(minv, calc(ni)))
mini = ni;
nl = nr;
nr = nl + haba;
}
auto end = system_clock::now();
auto part = duration_cast<milliseconds>(end - s);
auto elapsed = duration_cast<milliseconds>(end - start_time);
if (elapsed + part * 2 >= lim) {
break;
}
}
return mini;
}
template <class F>
dou goldt_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を残している*/
auto lim = milliseconds(time - 20);
dou maxi = 0, maxv = MIN(dou); /*区間をk分割する*/
rep(k, 1, inf) {
auto s = system_clock::now();
dou haba = (r - l) / k;
dou nl = l;
dou nr = l + haba;
rep(i, k) {
dou ni = goldt_d(nl, nr, calc);
if (chma(maxv, calc(ni)))
maxi = ni;
nl = nr;
nr = nl + haba;
}
auto end = system_clock::now();
auto part = duration_cast<milliseconds>(end - s);
auto elapsed = duration_cast<milliseconds>(end - start_time);
if (elapsed + part * 2 >= lim) {
break;
}
}
return maxi;
}
template <class T> T min(vector<vector<T>> &a) {
T res = MAX(T);
rep(i, a.size()) chmi(res, *min_element(all(a[i])));
return res;
}
template <class T> T max(vector<vector<T>> &a) {
T res = MIN(T);
rep(i, a.size()) chma(res, *max_element(all(a[i])));
return res;
}
constexpr bool bget(ll m, ll keta) {
#ifdef _DEBUG
assert(keta <= 62); // オーバーフロー 1^62までしか扱えない
#endif
return (m >> keta) & 1;
}
ll bget(ll m, ll keta, ll sinsuu) {
m /= (ll)pow(sinsuu, keta);
return m % sinsuu;
}
constexpr ll bit(ll n) {
#ifdef _DEBUG
assert(n <= 62); // オーバーフロー 1^62までしか扱えない
#endif
return (1LL << (n));
}
ll bit(ll n, ll sinsuu) { return (ll)pow(sinsuu, n); }
ll mask(ll n) { return (1ll << n) - 1; }
// aをbitに置きなおす
//{0, 2} -> 101
ll biti(vi &a) {
int m = 0;
for (auto &&v : a)
m |= bit(v);
return m;
}
//{1, 1, 0} -> 011
ll bitb(vi &a) {
int m = 0;
rep(i, sz(a)) if (a[i]) m |= bit(i);
return m;
}
#define bcou __builtin_popcountll
// 最下位ビット
ll lbit(ll n) { return n & -n; }
ll lbiti(ll n) { return log2(n & -n); }
// 最上位ビット
ll hbit(ll n) {
n |= (n >> 1);
n |= (n >> 2);
n |= (n >> 4);
n |= (n >> 8);
n |= (n >> 16);
n |= (n >> 32);
return n - (n >> 1);
}
ll hbiti(ll n) { return log2(hbit(n)); }
ll hbitk(ll n) {
ll k = 0;
rer(i, 5) {
ll a = k + (1ll << i);
ll b = 1ll << a;
if (b <= n)
k += 1ll << i;
}
return k;
}
// 初期化は0を渡す
ll nextComb(ll &mask, ll n, ll r) {
if (!mask)
return mask = (1LL << r) - 1;
ll x = mask & -mask; /*最下位の1*/
ll y = mask + x; /*連続した下の1を繰り上がらせる*/
ll res = ((mask & ~y) / x >> 1) | y;
if (bget(res, n))
return mask = 0;
else
return mask = res;
}
// n桁以下でビットがr個立っているもののvectorを返す
vi bitCombList(ll n, ll r) {
vi res;
ll m = 0;
while (nextComb(m, n, r)) {
res.push_back(m);
}
return res;
}
// masの立ってるindexを見る
#define forbit(i, mas) \
for (int forbitj = lbit(mas), forbitm = mas, i = log2(forbitj); forbitm; \
forbitm = forbitj ? forbitm ^ forbitj : 0, forbitj = lbit(forbitm), \
i = log2(forbitj))
char itoal(ll i) { return 'a' + i; }
char itoaL(ll i) { return 'A' + i; }
ll altoi(char c) {
if ('A' <= c && c <= 'Z')
return c - 'A';
return c - 'a';
}
ll ctoi(char c) { return c - '0'; }
char itoc(ll i) { return i + '0'; }
ll vtoi(vi &v) {
ll res = 0;
if (sz(v) > 18) {
debugline("vtoi");
deb(sz(v));
ole();
}
rep(i, sz(v)) {
res *= 10;
res += v[i];
}
return res;
}
vi itov(ll i) {
vi res;
while (i) {
res.push_back(i % 10);
i /= 10;
}
rev(res);
return res;
}
vi stov(string &a) {
ll n = sz(a);
vi ret(n);
rep(i, n) { ret[i] = a[i] - '0'; }
return ret;
}
// 基準を満たさないものは0になる
vi stov(string &a, char one) {
ll n = sz(a);
vi ret(n);
rep(i, n) ret[i] = a[i] == one;
return ret;
}
vector<vector<ll>> ctoi(vector<vector<char>> s, char c) {
ll n = sz(s), m = sz(s[0]);
vector<vector<ll>> res(n, vector<ll>(m));
rep(i, n) rep(j, m) res[i][j] = s[i][j] == c;
return res;
}
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());
//[i] := vを返す
// aは0~n-1で置き換えられる
vi compress(vi &a) {
vi b;
ll len = a.size();
for (ll i = 0; i < len; ++i) {
b.push_back(a[i]);
}
sort(b);
unique(b);
for (ll i = 0; i < len; ++i) {
a[i] = lower_bound(all(b), a[i]) - b.begin();
}
ll blen = sz(b);
vi ret(blen);
rep(i, blen) { ret[i] = b[i]; }
return ret;
}
vi compress(vi &a, umap<ll, ll> &map) {
vi b;
ll len = a.size();
for (ll i = 0; i < len; ++i) {
b.push_back(a[i]);
}
sort(b);
unique(b);
for (ll i = 0; i < len; ++i) {
ll v = a[i];
a[i] = lower_bound(all(b), a[i]) - b.begin();
map[v] = a[i];
}
ll blen = sz(b);
vi ret(blen);
rep(i, blen) { ret[i] = b[i]; }
return ret;
}
vi compress(vi &a, vi &r) {
vi b;
ll len = a.size();
fora(v, a) b.push_back(v);
fora(v, r) b.push_back(v);
sort(b);
unique(b);
for (ll i = 0; i < len; ++i)
a[i] = lower_bound(all(b), a[i]) - b.begin();
for (ll i = 0; i < sz(r); ++i)
r[i] = lower_bound(all(b), r[i]) - b.begin();
ll blen = sz(b);
vi ret(blen);
rep(i, blen) { ret[i] = b[i]; }
return ret;
}
vi compress(vi &a, vi &r, vi &s) {
vi b;
ll len = a.size();
fora(v, a) b.push_back(v);
fora(v, r) b.push_back(v);
fora(v, s) b.push_back(v);
sort(b);
unique(b);
for (ll i = 0; i < len; ++i)
a[i] = lower_bound(all(b), a[i]) - b.begin();
for (ll i = 0; i < sz(r); ++i)
r[i] = lower_bound(all(b), r[i]) - b.begin();
for (ll i = 0; i < sz(s); ++i)
r[i] = lower_bound(all(b), s[i]) - b.begin();
ll blen = sz(b);
vi ret(blen);
rep(i, blen) { ret[i] = b[i]; }
return ret;
}
vi compress(vector<vi> &a) {
vi b;
fora(vv, a) fora(v, vv) b.push_back(v);
sort(b);
unique(b);
fora(vv, a) fora(v, vv) v = lower_bound(all(b), v) - b.begin();
ll blen = sz(b);
vi ret(blen);
rep(i, blen) { ret[i] = b[i]; }
return ret;
}
vi compress(vector<vector<vi>> &a) {
vi b;
fora(vvv, a) fora(vv, vvv) fora(v, vv) b.push_back(v);
sort(b);
unique(b);
fora(vvv, a) fora(vv, vvv) fora(v, vv) v = lower_bound(all(b), v) - b.begin();
ll blen = sz(b);
vi ret(blen);
rep(i, blen) { ret[i] = b[i]; }
return ret;
}
void compress(ll a[], ll len) {
vi b;
for (ll i = 0; i < len; ++i) {
b.push_back(a[i]);
}
sort(b);
unique(b);
for (ll i = 0; i < len; ++i) {
a[i] = lower_bound(all(b), a[i]) - b.begin();
}
}
// 要素が見つからなかったときに困る
#define binarySearch(a, v) (binary_search(all(a), v))
#define lowerIndex(a, v) (lower_bound(all(a), v) - a.begin())
#define upperIndex(a, v) (upper_bound(all(a), v) - a.begin())
#define rlowerIndex(a, v) (upper_bound(all(a), v) - a.begin() - 1)
#define rupperIndex(a, v) (lower_bound(all(a), v) - a.begin() - 1)
template <class T, class U, class W> T lowerBound(vector<T> &a, U v, W banpei) {
auto it = lower_bound(a.begin(), a.end(), v);
if (it == a.end())
return banpei;
else
return *it;
}
template <class T, class U, class W> T lowerBound(ruiC<T> &a, U v, W banpei) {
return lowerBound(a.rui, v, banpei);
}
template <class T, class U, class W> T upperBound(vector<T> &a, U v, W banpei) {
auto it = upper_bound(a.begin(), a.end(), v);
if (it == a.end())
return banpei;
else
return *it;
}
template <class T, class U, class W> T upperBound(ruiC<T> &a, U v, W banpei) {
return upperBound(a.rui, v, banpei);
}
template <class T, class U, class W>
T rlowerBound(vector<T> &a, U v, W banpei) {
auto it = upper_bound(a.begin(), a.end(), v);
if (it == a.begin())
return banpei;
else {
return *(--it);
}
}
template <class T, class U, class W> T rlowerBound(ruiC<T> &a, U v, W banpei) {
return rlowerBound(a.rui, v, banpei);
}
template <class T, class U, class W>
T rupperBound(vector<T> &a, U v, W banpei) {
auto it = lower_bound(a.begin(), a.end(), v);
if (it == a.begin())
return banpei;
else {
return *(--it);
}
}
template <class T, class U, class W> T rupperBound(ruiC<T> &a, U v, W banpei) {
return rupperBound(a.rui, v, banpei);
}
#define next2(a) next(next(a))
#define prev2(a) prev(prev(a))
// 狭義の単調増加列 長さを返す
template <class T> int lis(vector<T> &a) {
int n = sz(a);
vi tail(n + 1, MAX(T));
rep(i, n) {
int id = lowerIndex(tail, a[i]); /**/
tail[id] = a[i];
}
return lowerIndex(tail, MAX(T));
}
template <class T> int lis_eq(vector<T> &a) {
int n = sz(a);
vi tail(n + 1, MAX(T));
rep(i, n) {
int id = upperIndex(tail, a[i]); /**/
tail[id] = a[i];
}
return lowerIndex(tail, MAX(T));
}
// iteratorを返す
// valueが1以上の物を返す 0は見つけ次第削除
// vを減らす場合 (*it).se--でいい
template <class T, class U, class V> auto lower_map(map<T, U> &m, V k) {
auto ret = m.lower_bound(k);
while (ret != m.end() && (*ret).second == 0) {
ret = m.erase(ret);
}
return ret;
}
template <class T, class U, class V> auto upper_map(map<T, U> &m, V k) {
auto ret = m.upper_bound(k);
while (ret != m.end() && (*ret).second == 0) {
ret = m.erase(ret);
}
return ret;
}
// 存在しなければエラー
template <class T, class U, class V> auto rlower_map(map<T, U> &m, V k) {
auto ret = upper_map(m, k);
assert(ret != m.begin());
ret--;
while (1) {
if ((*ret).second != 0)
break;
assert(ret != m.begin());
auto next = ret;
--next;
m.erase(ret);
ret = next;
}
return ret;
}
template <class T, class U, class V> auto rupper_map(map<T, U> &m, V k) {
auto ret = lower_map(m, k);
assert(ret != m.begin());
ret--;
while (1) {
if ((*ret).second != 0)
break;
assert(ret != m.begin());
auto next = ret;
--next;
m.erase(ret);
ret = next;
}
return ret;
}
template <class T> void fin(T s) { cout << s << endl, exit(0); }
// 便利 数学 math
// sub ⊂ top
bool subset(int sub, int top) { return (sub & top) == sub; }
//-180 ~ 180 degree
double atand(double h, double w) { return atan2(h, w) / PI * 180; }
//% -mの場合、最小の正の数を返す
ll mod(ll a, ll m) {
if (m < 0)
m *= -1;
return (a % m + m) % m;
}
ll pow(ll a) { return a * a; };
ll fact(ll v) { return v <= 1 ? 1 : v * fact(v - 1); }
dou factd(int v) {
static vd fact(2, 1);
if (sz(fact) <= v) {
rep(i, sz(fact), v + 1) { fact.push_back(fact.back() * i); }
}
return fact[v];
}
ll comi(ll n, ll r) {
assert(n < 100);
static vvi(pas, 100, 100);
if (pas[0][0])
return pas[n][r];
pas[0][0] = 1;
rep(i, 1, 100) {
pas[i][0] = 1;
rep(j, 1, i + 1) pas[i][j] = pas[i - 1][j - 1] + pas[i - 1][j];
}
return pas[n][r];
}
double comd2(ll n, ll r) {
static vvd(comb, 2020, 2020);
if (comb[0][0] == 0) {
comb[0][0] = 1;
rep(i, 2000) {
comb[i + 1][0] = 1;
rep(j, 1, i + 2) { comb[i + 1][j] = comb[i][j] + comb[i][j - 1]; }
}
}
return comb[n][r];
}
double comd(int n, int r) {
if (r < 0 || r > n)
return 0;
if (n < 2020)
return comd2(n, r);
static vd fact(2, 1);
if (sz(fact) <= n) {
rep(i, sz(fact), n + 1) { fact.push_back(fact.back() * i); }
}
return fact[n] / fact[n - r] / fact[r];
}
ll gcd(ll a, ll b) {
while (b)
a %= b, swap(a, b);
return abs(a);
}
ll gcd(vi b) {
ll res = b[0];
rep(i, 1, sz(b)) res = gcd(b[i], res);
return res;
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll lcm(vi a) {
ll res = a[0];
rep(i, 1, sz(a)) res = lcm(a[i], res);
return res;
}
ll ceil(ll a, ll b) {
if (b == 0) {
debugline("ceil");
deb(a, b);
ole();
return -1;
} else if (a < 0) {
return 0;
} else {
return (a + b - 1) / b;
}
}
ll sig0(ll t) { return t <= 0 ? 0 : ((1 + t) * t) >> 1; }
// ll sig(ll s, ll t) { return ((s + t) * (t - s + 1)) >> 1; }
ll sig(ll s, ll t) {
if (s > t)
swap(s, t);
return sig0(t - s) + s * (t - s + 1);
}
#define tousa_i tosa_i
#define lower_tousa_i lower_tosa_i
#define upper_tousa upper_tosa
#define upper_tousa_i upper_tosa_i
ll tosa_i(ll st, ll ad, ll v) {
assert(((v - st) % ad) == 0);
return (v - st) / ad;
}
ll tosa_s(ll st, ll ad, ll len) { return st * len + sig0(len - 1) * ad; }
// ax + r (x は非負整数) で表せる整数のうち、v 以上となる最小の整数
ll lower_tosa(ll st, ll ad, ll v) {
if (st >= v)
return st;
return (v - st + ad - 1) / ad * ad + st;
}
// 第何項か
ll lower_tosa_i(ll st, ll ad, ll v) {
if (st >= v)
return 0;
return (v - st + ad - 1) / ad;
}
ll upper_tosa(ll st, ll ad, ll v) { return lower_tosa(st, ad, v + 1); }
ll upper_tosa_i(ll st, ll ad, ll v) { return lower_tosa_i(st, ad, v + 1); }
// v * v >= aとなる最小のvを返す
ll sqrt(ll a) {
if (a < 0) {
debugline("sqrt");
deb(a);
ole();
}
ll res = (ll)std::sqrt(a);
while (res * res < a)
++res;
return res;
}
double log(double e, double x) { return log(x) / log(e); }
/*@formatter:off*/
// 機能拡張
template <class T, class U> string to_string(T a, U b) {
string res = "";
res += a;
res += b;
return res;
}
template <class T, class U, class V> string to_string(T a, U b, V c) {
string res = "";
res += a;
res += b;
res += c;
return res;
}
template <class T, class U, class V, class W>
string to_string(T a, U b, V c, W d) {
string res = "";
res += a;
res += b;
res += c;
res += d;
return res;
}
template <class T, class U, class V, class W, class X>
string to_string(T a, U b, V c, W d, X e) {
string res = "";
res += a;
res += b;
res += c;
res += d;
res += e;
return res;
}
constexpr int bsetlen = k5 * 2;
// constexpr int bsetlen = 5050;
#define bset bitset<bsetlen>
bool operator<(bitset<bsetlen> &a, bitset<bsetlen> &b) {
rer(i, bsetlen - 1) {
if (a[i] < b[i])
return true;
if (a[i] > b[i])
return false;
}
return false;
}
bool operator>(bitset<bsetlen> &a, bitset<bsetlen> &b) {
rer(i, bsetlen - 1) {
if (a[i] > b[i])
return true;
if (a[i] < b[i])
return false;
}
return false;
}
bool operator<=(bitset<bsetlen> &a, bitset<bsetlen> &b) {
rer(i, bsetlen - 1) {
if (a[i] < b[i])
return true;
if (a[i] > b[i])
return false;
}
return true;
}
bool operator>=(bitset<bsetlen> &a, bitset<bsetlen> &b) {
rer(i, bsetlen - 1) {
if (a[i] > b[i])
return true;
if (a[i] < b[i])
return false;
}
return true;
}
string operator~(string &a) {
string res = a;
for (auto &&c : res) {
if (c == '0')
c = '1';
else if (c == '1')
c = '0';
else {
cerr << "cant ~" << a << "must bit" << endl;
exit(0);
}
}
return res;
}
ostream &operator<<(ostream &os, bset &a) {
bitset<10> b;
vi list;
rep(i, bsetlen) {
if (a[i])
list.push_back(i), b[i] = 1;
}
os << b << ", " << list;
return os;
}
int hbiti(bset &a) {
rer(i, bsetlen) {
if (a[i])
return i;
}
return -1;
}
#define hk(a, b, c) (a <= b && b < c)
// O(N/64)
bset nap(bset &a, int v) {
bset r = a | a << v;
return r;
}
bset nap(bset &a, bset &v) {
bset r = a;
rep(i, bsetlen) {
if (v[i])
r |= a << i;
}
return r;
}
template <class T> void seth(vector<vector<T>> &S, int w, vector<T> &v) {
assert(sz(S) == sz(v));
assert(w < sz(S[0]));
rep(h, sz(S)) { S[h][w] = v[h]; }
}
template <class T, class U> void operator+=(pair<T, U> &a, pair<T, U> &b) {
a.fi += b.fi;
a.se += b.se;
}
template <class T, class U> pair<T, U> operator+(pair<T, U> &a, pair<T, U> &b) {
return pair<T, U>(a.fi + b.fi, a.se + b.se);
}
template <typename CharT, typename Traits, typename Alloc>
basic_string<CharT, Traits, Alloc>
operator+(const basic_string<CharT, Traits, Alloc> &lhs, const int rv) {
basic_string<CharT, Traits, Alloc> str(lhs);
str.append(to_string(rv));
return str;
}
template <typename CharT, typename Traits, typename Alloc>
void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const int rv) {
lhs += to_string(rv);
}
template <typename CharT, typename Traits, typename Alloc>
basic_string<CharT, Traits, Alloc>
operator+(const basic_string<CharT, Traits, Alloc> &lhs, const signed rv) {
basic_string<CharT, Traits, Alloc> str(lhs);
str.append(to_string(rv));
return str;
}
template <typename CharT, typename Traits, typename Alloc>
void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const signed rv) {
lhs += to_string(rv);
}
template <typename CharT, typename Traits, typename Alloc>
void operator*=(basic_string<CharT, Traits, Alloc> &s, int num) {
auto bek = s;
s = "";
for (; num; num >>= 1) {
if (num & 1) {
s += bek;
}
bek += bek;
}
}
template <class T, class U> void operator+=(queue<T> &a, U v) { a.push(v); }
template <class T, class U> void operator+=(deque<T> &a, U v) {
a.push_back(v);
}
template <class T>
priority_queue<T, vector<T>, greater<T>> &
operator+=(priority_queue<T, vector<T>, greater<T>> &a, vector<T> &v) {
fora(d, v) a.push(d);
return a;
}
template <class T, class U>
priority_queue<T, vector<T>, greater<T>> &
operator+=(priority_queue<T, vector<T>, greater<T>> &a, U v) {
a.push(v);
return a;
}
template <class T, class U>
priority_queue<T> &operator+=(priority_queue<T> &a, U v) {
a.push(v);
return a;
}
template <class T> set<T> &operator+=(set<T> &a, vector<T> v) {
fora(d, v) a.insert(d);
return a;
}
template <class T, class U> auto operator+=(set<T> &a, U v) {
return a.insert(v);
}
template <class T, class U> auto operator-=(set<T> &a, U v) {
return a.erase(v);
}
template <class T, class U> auto operator+=(mset<T> &a, U v) {
return a.insert(v);
}
template <class T, class U>
set<T, greater<T>> &operator+=(set<T, greater<T>> &a, U v) {
a.insert(v);
return a;
}
template <class T, class U> vector<T> &operator+=(vector<T> &a, U v) {
a.push_back(v);
return a;
}
template <class T, class U> vector<T> operator+(const vector<T> &a, U v) {
vector<T> ret = a;
ret += v;
return ret;
}
template <class T, class U> vector<T> operator+(U v, const vector<T> &a) {
vector<T> ret = a;
ret.insert(ret.begin(), v);
return ret;
}
template <class T> vector<T> operator+(vector<T> a, vector<T> b) {
vector<T> ret;
ret = a;
fora(v, b) ret += v;
return ret;
}
template <class T> vector<T> &operator+=(vector<T> &a, vector<T> &b) {
rep(i, sz(b)) { /*こうしないとa+=aで両辺が増え続けてバグる*/
a.push_back(b[i]);
}
return a;
}
template <class T, class U> map<T, U> &operator+=(map<T, U> &a, map<T, U> &b) {
fora(bv, b) { a[bv.first] += bv.second; }
return a;
}
template <class T> vector<T> &operator-=(vector<T> &a, vector<T> &b) {
if (sz(a) != sz(b)) {
debugline("vector<T> operator-=");
deb(a);
deb(b);
exit(0);
}
rep(i, sz(a)) a[i] -= b[i];
return a;
}
template <class T> vector<T> operator-(vector<T> &a, vector<T> &b) {
if (sz(a) != sz(b)) {
debugline("vector<T> operator-");
deb(a);
deb(b);
ole();
}
vector<T> res(sz(a));
rep(i, sz(a)) res[i] = a[i] - b[i];
return res;
}
template <class T, class U> void operator*=(vector<T> &a, U b) {
vector<T> ta = a;
rep(b - 1) { a += ta; }
}
template <typename T> void erase(vector<T> &v, unsigned ll i) {
v.erase(v.begin() + i);
}
template <typename T> void erase(vector<T> &v, unsigned ll s, unsigned ll e) {
v.erase(v.begin() + s, v.begin() + e);
}
template <class T, class U> void erase(map<T, U> &m, ll okl, ll ngr) {
m.erase(m.lower_bound(okl), m.lower_bound(ngr));
}
template <class T> void erase(set<T> &m, ll okl, ll ngr) {
m.erase(m.lower_bound(okl), m.lower_bound(ngr));
}
template <typename T> void erasen(vector<T> &v, unsigned ll s, unsigned ll n) {
v.erase(v.begin() + s, v.begin() + s + n);
}
template <typename T, typename U>
void insert(vector<T> &v, unsigned ll i, U t) {
v.insert(v.begin() + i, t);
}
template <typename T, typename U> void push_front(vector<T> &v, U t) {
v.insert(v.begin(), t);
}
template <typename T, typename U>
void insert(vector<T> &v, unsigned ll i, vector<T> list) {
for (auto &&va : list)
v.insert(v.begin() + i++, va);
}
template <typename T> void insert(set<T> &v, vector<T> list) {
for (auto &&va : list)
v.insert(va);
}
vector<string> split(const string a, const char deli) {
string b = a + deli;
ll l = 0, r = 0, n = b.size();
vector<string> res;
rep(i, n) {
if (b[i] == deli) {
r = i;
if (l < r)
res.push_back(b.substr(l, r - l));
l = i + 1;
}
}
return res;
}
vector<string> split(const string a, const string deli) {
vector<string> res;
ll kn = sz(deli);
std::string::size_type Pos(a.find(deli));
ll l = 0;
while (Pos != std::string::npos) {
if (Pos - l)
res.push_back(a.substr(l, Pos - l));
l = Pos + kn;
Pos = a.find(deli, Pos + kn);
}
if (sz(a) - l)
res.push_back(a.substr(l, sz(a) - l));
return res;
}
void yn(bool a) {
if (a)
cout << "yes" << endl;
else
cout << "no" << endl;
}
void Yn(bool a) {
if (a)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
void YN(bool a) {
if (a)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
void fyn(bool a) {
if (a)
cout << "yes" << endl;
else
cout << "no" << endl;
exit(0);
}
void fYn(bool a) {
if (a)
cout << "Yes" << endl;
else
cout << "No" << endl;
exit(0);
}
void fYN(bool a) {
if (a)
cout << "YES" << endl;
else
cout << "NO" << endl;
exit(0);
}
void Possible(bool a) {
if (a)
cout << "Possible" << endl;
else
cout << "Impossible" << endl;
exit(0);
}
void POSSIBLE(bool a) {
if (a)
cout << "POSSIBLE" << endl;
else
cout << "IMPOSSIBLE" << endl;
exit(0);
}
template <typename T> class fixed_point : T {
public:
explicit constexpr fixed_point(T &&t) noexcept : T(std::forward<T>(t)) {}
template <typename... Args>
constexpr decltype(auto) operator()(Args &&...args) const {
return T::operator()(*this, std::forward<Args>(args)...);
}
};
template <typename T>
static inline constexpr decltype(auto) fix(T &&t) noexcept {
return fixed_point<T>{std::forward<T>(t)};
}
// 未分類
// tou分割する
template <typename T> vector<vector<T>> cut(vector<T> &a, int tou = 2) {
int N = sz(a);
vector<vector<T>> res(tou);
int hab = N / tou;
vi lens(tou, hab);
rep(i, N % tou) { lens[tou - 1 - i]++; }
int l = 0;
rep(i, tou) {
int len = lens[i];
int r = l + len;
res[i].resize(len);
std::copy(a.begin() + l, a.begin() + r, res[i].begin());
l = r;
}
return res;
}
// 長さn毎に分割する
template <typename T> vector<vector<T>> cutn(vector<T> &a, int len) {
int N = sz(a);
vector<vector<T>> res(ceil(N, len));
vi lens(N / len, len);
if (N % len)
lens.push_back(N % len);
int l = 0;
rep(i, sz(lens)) {
int len = lens[i];
int r = l + len;
res[i].resize(len);
std::copy(a.begin() + l, a.begin() + r, res[i].begin());
l = r;
}
return res;
}
vi inds_(vi &a) {
int n = sz(a);
vb was(n);
vi res(n);
rep(i, n) {
assert(!was[a[i]]);
res[a[i]] = i;
was[a[i]] = true;
}
return res;
}
//@起動時
struct initon {
initon() {
cin.tie(0);
ios::sync_with_stdio(false);
cout.setf(ios::fixed);
cout.precision(16);
srand((unsigned)clock() + (unsigned)time(NULL));
};
} initonv; //@formatter:on
#define pre prev
#define nex next
// gra mll pr
// 上下左右
const string udlr = "udlr";
string UDLR = "UDLR"; // x4と連動 UDLR.find('U') := x4[0]
// 右、上が正
constexpr ll h4[] = {1, -1, 0, 0};
constexpr ll w4[] = {0, 0, -1, 1};
constexpr ll h8[] = {0, 1, 0, -1, -1, 1, 1, -1};
constexpr ll w8[] = {1, 0, -1, 0, 1, -1, 1, -1};
int mei_inc(int h, int w, int H, int W, int i) {
while (++i < 4) {
if (inside(h + h4[i], w + w4[i], H, W))
return i;
}
return i;
}
#define mei(nh, nw, h, w) \
for (int i = mei_inc(h, w, H, W, -1), nh = i < 4 ? h + h4[i] : 0, \
nw = i < 4 ? w + w4[i] : 0; \
i < 4; i = mei_inc(h, w, H, W, i), nh = h + h4[i], nw = w + w4[i])
// グラフ内で #undef getid
// #define getidとしているため、ここを書き直したらgraphも書き直す
#define getid_2(h, w) (h * W + w)
#define getid_1(p) (p.first * W + p.second)
#define o_getid(a, b, name, ...) name
#define getid(...) o_getid(__VA_ARGS__, getid_2, getid_1)(__VA_ARGS__)
#define getp(id) mp(id / W, id % W)
#define shuffle(a) \
std::random_device seed_gen; \
std::mt19937 engine(seed_gen()); \
std::shuffle((a).begin(), (a).end(), engine);
// 1980 開始からtime ms経っていたらtrue
bool timeup(int time) {
auto end_time = system_clock::now();
auto part = duration_cast<milliseconds>(end_time - start_time);
auto lim = milliseconds(time);
return part >= lim;
}
// vectorで取れる要素数
// bool=> 1e9 * 8.32
// int => 1e8 * 2.6
// ll => 1e8 * 1.3
vvc(ba);
ll N, M, H, W;
vi A, B, C;
// 16:10
void solve() {
static int dp[3011][3011][2];
static bool was[3011][3011][2];
in(N);
na(A, N);
out(fix([&](auto rec, int l, int r, int te) -> int {
if (l == r)
return 0;
int &d = dp[l][r][te];
if (was[l][r][te])
return d;
was[l][r][te] = 1;
// 最大化
if (te == 0) {
d = -linf;
chma(d, rec(l + 1, r, te ^ 1) + A[l]);
chma(d, rec(l, r - 1, te ^ 1) + A[r - 1]);
} else {
d = linf;
chmi(d, rec(l + 1, r, te ^ 1) - A[l]);
chmi(d, rec(l, r - 1, te ^ 1) - A[r - 1]);
}
return d;
})(0, N, 0));
}
auto my(ll n, vi &a) { return 0; }
auto sister(ll n, vi &a) {
ll ret = 0;
return ret;
}
signed main() {
solve();
#define arg n, a
#ifdef _DEBUG
bool bad = 0;
for (ll i = 0, ok = 1; i < k5 && ok; ++i) {
ll n = rand(1, 8);
vi a = ranv(n, 1, 10);
auto myres = my(arg);
auto res = sister(arg);
ok = myres == res;
if (!ok) {
out(arg);
cerr << "AC : " << res << endl;
cerr << "MY : " << myres << endl;
bad = 1;
break;
}
}
if (!bad) {
// cout << "完璧 : solveを書き直そう" << endl;
// cout << " : そして、solve()を呼び出すのだ" << endl;
// cout << " : cin>>n; na(a,n);も忘れるな" << endl;
}
#endif
return 0;
};
| replace | 4,737 | 4,741 | 4,737 | 4,739 | MLE | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define all(a) a.begin(), a.end()
#define F first
#define S second
#define pb push_back
#define int long long
#define ld long double
#define vi vector<long long>
#define mkp make_pair
#define sz(x) (int)x.size()
#define pi pair<long long, long long>
#define watch(x) \
cout << (#x) << " is " << (x) << endl; \
cout.flush()
#define endl '\n'
#define _ << ' '
#define min3(a, b, c) min(a, min(b, c))
#define min4(a, b, c, d) min(min(a, d), min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define max4(a, b, c, d) max(max(a, d), max(b, c))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
tree_order_statistics_node_update>
idxset;
// find_by_order(i) : iterator to the value at index i, order_of_key(x) : the
// index of value x
int n, sum = 0;
vector<int> v(1000001);
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("A.txt", "r", stdin);
#endif
cin >> n;
int s = 0, x, ans = 0;
for (int i = 0; i < n; i++) {
cin >> v[s];
sum += v[s++];
while (s >= 3 && v[s - 3] <= v[s - 2] && v[s - 2] >= v[s - 1]) {
v[s - 3] = v[s - 3] - v[s - 2] + v[s - 1];
s -= 2;
}
}
int sgn = 1, i = 0, j = s - 1;
while (i <= j) {
if (v[i] > v[j])
ans += sgn * v[i++];
else
ans += sgn * v[j--];
sgn *= -1;
}
cout << ans; // cout << (ans + sum)/2;
return 0;
}
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define all(a) a.begin(), a.end()
#define F first
#define S second
#define pb push_back
#define int long long
#define ld long double
#define vi vector<long long>
#define mkp make_pair
#define sz(x) (int)x.size()
#define pi pair<long long, long long>
#define watch(x) \
cout << (#x) << " is " << (x) << endl; \
cout.flush()
#define endl '\n'
#define _ << ' '
#define min3(a, b, c) min(a, min(b, c))
#define min4(a, b, c, d) min(min(a, d), min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define max4(a, b, c, d) max(max(a, d), max(b, c))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
tree_order_statistics_node_update>
idxset;
// find_by_order(i) : iterator to the value at index i, order_of_key(x) : the
// index of value x
int n, sum = 0;
vector<int> v(1000001);
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
#ifndef ONLINE_JUDGE
// freopen("A.txt", "r", stdin);
#endif
cin >> n;
int s = 0, x, ans = 0;
for (int i = 0; i < n; i++) {
cin >> v[s];
sum += v[s++];
while (s >= 3 && v[s - 3] <= v[s - 2] && v[s - 2] >= v[s - 1]) {
v[s - 3] = v[s - 3] - v[s - 2] + v[s - 1];
s -= 2;
}
}
int sgn = 1, i = 0, j = s - 1;
while (i <= j) {
if (v[i] > v[j])
ans += sgn * v[i++];
else
ans += sgn * v[j--];
sgn *= -1;
}
cout << ans; // cout << (ans + sum)/2;
return 0;
}
| replace | 37 | 38 | 37 | 38 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define mk make_pair
#define fi first
#define se second
#define vll vector<ll>
#define pii pair<ll, ll>
#define vvll vector<vector<ll>>
#define pb push_back
#define sz(v) (v).size()
#define inf 1e18
#define md 1000000007
#define all(v) (v).begin(), (v).end()
#define rep(i, a, b) for (ll i = a; i < b; ++i)
#define tel(a) \
{ cout << a << "\n"; }
#define tell(a, b) \
{ cout << a << " | " << b << "\n"; }
#define telll(a, b, c) \
{ cout << a << " | " << b << " | " << c << "\n"; }
#define teln(v, n) \
{ \
cout << "v- "; \
rep(i, 0, n) cout << v[i] << " "; \
cout << "\n"; \
}
#define IOS \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define tihs \
if (fopen("inp.txt", "r")) \
freopen("inp.txt", "r", stdin), freopen("out.txt", "w", stdout);
using namespace std;
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
#else
#define trace(...)
#endif
#define M 110
#define N 3010
ll n, k, a[M], dp[N][N][2];
ll f(ll left, ll right, ll cond) {
if (left > right)
return 0;
if (dp[left][right][cond] != -1)
return dp[left][right][cond];
ll ans;
if (cond)
ans = max(f(left + 1, right, !cond) + a[left],
f(left, right - 1, !cond) + a[right]);
else
ans = min(f(left + 1, right, !cond) - a[left],
f(left, right - 1, !cond) - a[right]);
return dp[left][right][cond] = ans;
}
int main() {
IOS;
tihs;
memset(dp, -1, sizeof(dp));
cin >> n;
rep(i, 0, n) cin >> a[i];
cout << f(0, n - 1, 1);
return 0;
} | #include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define mk make_pair
#define fi first
#define se second
#define vll vector<ll>
#define pii pair<ll, ll>
#define vvll vector<vector<ll>>
#define pb push_back
#define sz(v) (v).size()
#define inf 1e18
#define md 1000000007
#define all(v) (v).begin(), (v).end()
#define rep(i, a, b) for (ll i = a; i < b; ++i)
#define tel(a) \
{ cout << a << "\n"; }
#define tell(a, b) \
{ cout << a << " | " << b << "\n"; }
#define telll(a, b, c) \
{ cout << a << " | " << b << " | " << c << "\n"; }
#define teln(v, n) \
{ \
cout << "v- "; \
rep(i, 0, n) cout << v[i] << " "; \
cout << "\n"; \
}
#define IOS \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define tihs \
if (fopen("inp.txt", "r")) \
freopen("inp.txt", "r", stdin), freopen("out.txt", "w", stdout);
using namespace std;
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
#else
#define trace(...)
#endif
#define M 110
#define N 3010
ll n, k, a[N], dp[N][N][2];
ll f(ll left, ll right, ll cond) {
if (left > right)
return 0;
if (dp[left][right][cond] != -1)
return dp[left][right][cond];
ll ans;
if (cond)
ans = max(f(left + 1, right, !cond) + a[left],
f(left, right - 1, !cond) + a[right]);
else
ans = min(f(left + 1, right, !cond) - a[left],
f(left, right - 1, !cond) - a[right]);
return dp[left][right][cond] = ans;
}
int main() {
IOS;
tihs;
memset(dp, -1, sizeof(dp));
cin >> n;
rep(i, 0, n) cin >> a[i];
cout << f(0, n - 1, 1);
return 0;
} | replace | 56 | 57 | 56 | 57 | -11 | |
p03171 | C++ | Runtime Error | #define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;
typedef unsigned int uint;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<LL, LL> PP;
#define REP(i, a, n) for (LL i = (a), i##_max = (n); i < i##_max; ++i)
#define REM(i, a, n) for (LL i = (LL)(n)-1, i##min = (a); i >= i##min; --i)
#define ALL(arr) (arr).begin(), (arr).end()
#define FLOAT fixed << setprecision(16)
#define SPEEDUP \
{ \
cin.tie(NULL); \
ios::sync_with_stdio(false); \
}
const int INF = 0x3FFFFFFF;
const LL INFLL = 0x3FFFFFFF3FFFFFFF;
const double INFD = 1.0e+308;
const string INFSTR = "\x7f";
const double EPS = 1.0e-9;
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T, class U>
istream &operator>>(istream &ist, pair<T, U> &right) {
return ist >> right.first >> right.second;
}
template <class T, class U>
ostream &operator<<(ostream &ost, const pair<T, U> &right) {
return ost << right.first << ' ' << right.second;
}
template <class T, class TCompatible, size_t N>
void Fill(T (&dest)[N], const TCompatible &val) {
fill(dest, dest + N, val);
}
template <class T, class TCompatible, size_t M, size_t N>
void Fill(T (&dest)[M][N], const TCompatible &val) {
for (int i = 0; i < M; ++i)
Fill(dest[i], val);
}
template <class T> T Compare(T left, T right) {
return left > right ? 1 : (left < right ? -1 : 0);
}
istream &Ignore(istream &ist) {
string s;
ist >> s;
return ist;
}
bool Inside(int i, int j, int h, int w) {
return i >= 0 && i < h && j >= 0 && j < w;
}
template <class T> T Next() {
T buf;
cin >> buf;
return buf;
}
#ifdef ONLY_MY_ENVIR
#include "BIT.h"
#include "Factorization.h"
#include "FlowSolver.h"
#include "Geometry.h"
#include "Global.h"
#include "Graph.h"
#include "IntMod.h"
#include "Math.h"
#include "Matrix.h"
#include "Position.h"
#include "Range.h"
#include "Rational.h"
#include "SegmentTree.h"
#include "SegmentTree2D.h"
#include "SuffixArray.h"
#include "Tree.h"
#include "UnionFind.h"
#endif
#ifdef __GNUC__
typedef __int128 LLL;
istream &operator>>(istream &ist, __int128 &val) {
LL tmp;
ist >> tmp;
val = tmp;
return ist;
}
ostream &operator<<(ostream &ost, __int128 val) {
LL tmp = val;
ost << tmp;
return ost;
}
#endif
#if 1234567891
#include <array>
#include <random>
#include <unordered_map>
#include <unordered_set>
template <typename T>
using PriorityQ = priority_queue<T, vector<T>, greater<T>>;
// template <class T>
// auto Is(const T& value) { return [value](const auto& comparand) -> bool {
// return comparand == value; }; }
#endif
int N;
int A[3000];
LL DP[3000][3001];
int main() {
cin >> N;
REP(i, 0, N) { cin >> A[i]; }
REP(t, 1, N + 1) {
REP(i, 0, N) {
int j = i + t;
if (j > N)
continue;
bool taro = (N - t) % 2 == 0;
if (taro) {
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;
return 0;
}
| #define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;
typedef unsigned int uint;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<LL, LL> PP;
#define REP(i, a, n) for (LL i = (a), i##_max = (n); i < i##_max; ++i)
#define REM(i, a, n) for (LL i = (LL)(n)-1, i##min = (a); i >= i##min; --i)
#define ALL(arr) (arr).begin(), (arr).end()
#define FLOAT fixed << setprecision(16)
#define SPEEDUP \
{ \
cin.tie(NULL); \
ios::sync_with_stdio(false); \
}
const int INF = 0x3FFFFFFF;
const LL INFLL = 0x3FFFFFFF3FFFFFFF;
const double INFD = 1.0e+308;
const string INFSTR = "\x7f";
const double EPS = 1.0e-9;
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T, class U>
istream &operator>>(istream &ist, pair<T, U> &right) {
return ist >> right.first >> right.second;
}
template <class T, class U>
ostream &operator<<(ostream &ost, const pair<T, U> &right) {
return ost << right.first << ' ' << right.second;
}
template <class T, class TCompatible, size_t N>
void Fill(T (&dest)[N], const TCompatible &val) {
fill(dest, dest + N, val);
}
template <class T, class TCompatible, size_t M, size_t N>
void Fill(T (&dest)[M][N], const TCompatible &val) {
for (int i = 0; i < M; ++i)
Fill(dest[i], val);
}
template <class T> T Compare(T left, T right) {
return left > right ? 1 : (left < right ? -1 : 0);
}
istream &Ignore(istream &ist) {
string s;
ist >> s;
return ist;
}
bool Inside(int i, int j, int h, int w) {
return i >= 0 && i < h && j >= 0 && j < w;
}
template <class T> T Next() {
T buf;
cin >> buf;
return buf;
}
#ifdef ONLY_MY_ENVIR
#include "BIT.h"
#include "Factorization.h"
#include "FlowSolver.h"
#include "Geometry.h"
#include "Global.h"
#include "Graph.h"
#include "IntMod.h"
#include "Math.h"
#include "Matrix.h"
#include "Position.h"
#include "Range.h"
#include "Rational.h"
#include "SegmentTree.h"
#include "SegmentTree2D.h"
#include "SuffixArray.h"
#include "Tree.h"
#include "UnionFind.h"
#endif
#ifdef __GNUC__
typedef __int128 LLL;
istream &operator>>(istream &ist, __int128 &val) {
LL tmp;
ist >> tmp;
val = tmp;
return ist;
}
ostream &operator<<(ostream &ost, __int128 val) {
LL tmp = val;
ost << tmp;
return ost;
}
#endif
#if 1234567891
#include <array>
#include <random>
#include <unordered_map>
#include <unordered_set>
template <typename T>
using PriorityQ = priority_queue<T, vector<T>, greater<T>>;
// template <class T>
// auto Is(const T& value) { return [value](const auto& comparand) -> bool {
// return comparand == value; }; }
#endif
int N;
int A[3000];
LL DP[3001][3001];
int main() {
cin >> N;
REP(i, 0, N) { cin >> A[i]; }
REP(t, 1, N + 1) {
REP(i, 0, N) {
int j = i + t;
if (j > N)
continue;
bool taro = (N - t) % 2 == 0;
if (taro) {
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;
return 0;
}
| replace | 131 | 132 | 131 | 132 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define MOD ((int)(1e9) + 7)
#define fast \
cin.tie(0); \
cout.tie(0); \
ios_base::sync_with_stdio(false)
#define filename "lynext"
#define freop \
freopen(filename ".cik", "w", stdout); \
freopen(filename ".gir", "r", stdin)
const long long int N = ((long long int)1e3) + 5;
const long long int M = ((long long int)1e3) + 5;
const long long int llinf = (long long int)1e18;
const int inf = INT_MAX;
typedef long long int lli;
lli n;
lli arr[N];
lli tot;
lli dp[N][N];
lli f(lli alinanLeft, lli alinanRight, bool taro, lli a) {
if (dp[alinanLeft][alinanRight] != -1)
return dp[alinanLeft][alinanRight];
if (a == n)
return 0;
if (taro) {
return dp[alinanLeft][alinanRight] = max(
f(alinanLeft, alinanRight + 1, false, a + 1) +
arr[n - alinanRight - 1],
f(alinanLeft + 1, alinanRight, false, a + 1) + arr[alinanLeft]);
} else {
return dp[alinanLeft][alinanRight] =
min(f(alinanLeft, alinanRight + 1, true, a + 1),
f(alinanLeft + 1, alinanRight, true, a + 1));
}
return -1;
}
int main() {
fast;
// freop;
cin >> n;
memset(dp, -1, sizeof dp);
for (int i = 0; i < n; i++)
cin >> arr[i], tot += arr[i];
lli ans = f(0, 0, true, 0);
// cout << "Taro : " << ans << "\n";
// cout << "Jiro : " << tot - ans << "\n";
cout << ans - (tot - ans) << "\n";
}
| #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define MOD ((int)(1e9) + 7)
#define fast \
cin.tie(0); \
cout.tie(0); \
ios_base::sync_with_stdio(false)
#define filename "lynext"
#define freop \
freopen(filename ".cik", "w", stdout); \
freopen(filename ".gir", "r", stdin)
const long long int N = ((long long int)4e3) + 5;
const long long int M = ((long long int)1e3) + 5;
const long long int llinf = (long long int)1e18;
const int inf = INT_MAX;
typedef long long int lli;
lli n;
lli arr[N];
lli tot;
lli dp[N][N];
lli f(lli alinanLeft, lli alinanRight, bool taro, lli a) {
if (dp[alinanLeft][alinanRight] != -1)
return dp[alinanLeft][alinanRight];
if (a == n)
return 0;
if (taro) {
return dp[alinanLeft][alinanRight] = max(
f(alinanLeft, alinanRight + 1, false, a + 1) +
arr[n - alinanRight - 1],
f(alinanLeft + 1, alinanRight, false, a + 1) + arr[alinanLeft]);
} else {
return dp[alinanLeft][alinanRight] =
min(f(alinanLeft, alinanRight + 1, true, a + 1),
f(alinanLeft + 1, alinanRight, true, a + 1));
}
return -1;
}
int main() {
fast;
// freop;
cin >> n;
memset(dp, -1, sizeof dp);
for (int i = 0; i < n; i++)
cin >> arr[i], tot += arr[i];
lli ans = f(0, 0, true, 0);
// cout << "Taro : " << ans << "\n";
// cout << "Jiro : " << tot - ans << "\n";
cout << ans - (tot - ans) << "\n";
}
| replace | 15 | 16 | 15 | 16 | 0 | |
p03171 | Python | Time Limit Exceeded | def kukan(n, a):
dp = [[0] * n for _ in range(n)]
for i in range(n):
dp[i][i] = a[i]
for i in range(n - 2, -1, -1):
for j in range(i + 1, n):
dp[i][j] = max(a[i] - dp[i + 1][j], a[j] - dp[i][j - 1])
return dp[0][n - 1]
n = int(input())
a = list(map(int, input().split()))
print(kukan(n, a))
| def kukan(n, a):
dp = [[0] * n for _ in range(n)]
for i in range(n):
dp[i][i] = a[i]
for i in range(n - 2, -1, -1):
for j in range(i + 1, n):
L = a[i] - dp[i + 1][j]
R = a[j] - dp[i][j - 1]
dp[i][j] = L if L > R else R
return dp[0][n - 1]
n = int(input())
a = list(map(int, input().split()))
print(kukan(n, a))
| replace | 6 | 7 | 6 | 9 | TLE | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
// dp[i][j] = max a person can get from strones from i to j
int main() {
long long int n;
cin >> n;
vector<long long int> x(n);
for (long long int i = 0; i < n; i++)
cin >> x[i];
vector<long long int> ab(n + 1);
vector<vector<long long int>> dp(n + 1, ab);
for (long long int i = 1; i < n; i++) {
dp[i][i] = x[i - 1];
if (x[i - 1] > x[i])
dp[i][i + 1] = x[i - 1];
else
dp[i][i + 1] = x[i];
}
dp[0][0] = 0;
dp[n][n] = x[n - 1];
for (long long int len = 2; len <= n; len++) {
for (long long int i = 1; i <= n - len; i++) {
long long int j = i + len;
long long int a = min(dp[i + 2][j], dp[i + 1][j - 1]) + x[i - 1];
long long int b = min(dp[i][j - 2], dp[i + 1][j - 1]) + x[j - 1];
dp[i][j] = max(a, b);
}
}
long long int a = dp[1][n];
long long int b = min(dp[2][n], dp[1][n - 1]);
cout << a - b;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
// dp[i][j] = max a person can get from strones from i to j
int main() {
long long int n;
cin >> n;
vector<long long int> x(n);
for (long long int i = 0; i < n; i++)
cin >> x[i];
if (n == 0) {
cout << 0;
return 0;
}
if (n == 1) {
cout << x[0];
return 0;
}
vector<long long int> ab(n + 1);
vector<vector<long long int>> dp(n + 1, ab);
for (long long int i = 1; i < n; i++) {
dp[i][i] = x[i - 1];
if (x[i - 1] > x[i])
dp[i][i + 1] = x[i - 1];
else
dp[i][i + 1] = x[i];
}
dp[0][0] = 0;
dp[n][n] = x[n - 1];
for (long long int len = 2; len <= n; len++) {
for (long long int i = 1; i <= n - len; i++) {
long long int j = i + len;
long long int a = min(dp[i + 2][j], dp[i + 1][j - 1]) + x[i - 1];
long long int b = min(dp[i][j - 2], dp[i + 1][j - 1]) + x[j - 1];
dp[i][j] = max(a, b);
}
}
long long int a = dp[1][n];
long long int b = min(dp[2][n], dp[1][n - 1]);
cout << a - b;
return 0;
} | insert | 13 | 13 | 13 | 21 | 0 | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define Mx 1000000000005
#define ll long long
#define pi 3.141592653
#define vi vector<ll>
#define Mo 1000000007
#define mp make_pair
#define M map<ll, ll>
#define fr(i, a, b) for (ll i = a; i < b; i++)
#define vii vector<pair<ll, ll>>
#define fi first
#define se second
#define pb push_back
#define ld long double
#define fill(a, x) memset(a, x, sizeof(a))
using namespace std;
bool comp(const pair<ll, ll> &a, const pair<ll, ll> &b) {
return a.second < b.second;
}
/*vi b=v;
sort(b.begin(), b.end());
b.resize(unique(b.begin(), b.end()) - b.begin());
ll t; cin>>t; fr(f,0,t){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
*/
ll func(ll l, ll r, ll a[]) {
if (l == r)
return a[l];
if (r - l == 1)
return max(a[l], a[r]);
ll x = a[l] + min(func(l + 1, r - 1, a), func(l + 2, r, a));
ll y = a[r] + min(func(l, r - 2, a), func(l + 1, r - 1, a));
return max(x, y);
}
int main() {
ll n, sum = 0;
cin >> n;
ll a[n];
fr(i, 0, n) {
cin >> a[i];
sum += a[i];
}
cout << 2 * func(0, n - 1, a) - sum;
} | #include <bits/stdc++.h>
#define Mx 1000000000005
#define ll long long
#define pi 3.141592653
#define vi vector<ll>
#define Mo 1000000007
#define mp make_pair
#define M map<ll, ll>
#define fr(i, a, b) for (ll i = a; i < b; i++)
#define vii vector<pair<ll, ll>>
#define fi first
#define se second
#define pb push_back
#define ld long double
#define fill(a, x) memset(a, x, sizeof(a))
using namespace std;
bool comp(const pair<ll, ll> &a, const pair<ll, ll> &b) {
return a.second < b.second;
}
/*vi b=v;
sort(b.begin(), b.end());
b.resize(unique(b.begin(), b.end()) - b.begin());
ll t; cin>>t; fr(f,0,t){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
*/
ll func(ll l, ll r, ll a[]) {
if (l == r)
return a[l];
if (r - l == 1)
return max(a[l], a[r]);
ll x = a[l] + min(func(l + 1, r - 1, a), func(l + 2, r, a));
ll y = a[r] + min(func(l, r - 2, a), func(l + 1, r - 1, a));
return max(x, y);
}
int main() {
ll n, sum = 0;
cin >> n;
ll a[n];
fr(i, 0, n) {
cin >> a[i];
sum += a[i];
}
ll dp[n][n], x, y, z;
fr(i, 0, n) dp[i][i] = a[i];
fr(l, 2, n + 1) {
fr(i, 0, n - l + 1) {
ll j = i + l - 1;
if (j - i == 1) {
dp[i][j] = max(a[i], a[j]);
continue;
}
x = dp[i + 1][j - 1];
y = dp[i + 2][j];
z = dp[i][j - 2];
dp[i][j] = max(a[i] + min(x, y), a[j] + min(z, x));
}
}
cout << 2 * dp[0][n - 1] - sum;
} | replace | 45 | 46 | 45 | 65 | TLE | |
p03171 | C++ | Time Limit Exceeded | // #############################################################################
#include "bits/stdc++.h"
// #############################################################################
// Macros
#define MOD 1000000007
#define eb emplace_back
#define mp make_pair
#define fr first
#define sc second
#define vec vector
#define len(x) x.size()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define all(v) v.begin(), v.end()
#define alla(a, n) a, a + n
#define sum(v) accumulate(all(v), 0LL)
// Type Defs
typedef long long ll;
typedef unsigned long long ull;
// #############################################################################
using namespace std;
// #############################################################################
// Helpers
void io_handler() {
#ifdef LOCAL_TESTING
freopen("inputf.in", "r", stdin);
freopen("outputf.in", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
template <typename T> void print_matrix(vector<vector<T>> mat) {
for (auto row : mat) {
for (auto e : row) {
cout << e << " ";
}
cout << endl;
}
}
template <typename T> void print_vector(vector<T> v) {
for (auto e : v) {
cout << e << " ";
}
cout << endl;
}
template <typename T> map<T, T> get_count(vector<T> v) {
map<T, T> count;
for (auto e : v) {
if (count.find(e) != count.end()) {
count[e]++;
} else {
count[e] = 1;
}
}
return count;
}
inline ll sub_sum(vec<ll> pre, ll left, ll right) {
return pre[right] - (left > 0 ? pre[left - 1] : 0LL);
}
// #############################################################################
// Solution
void solve() {
ll n;
cin >> n;
vec<ll> score(n);
ll total = 0LL;
vec<vec<ll>> dp(n, vec<ll>(n, 0));
for (ll i = 0LL; i < n; i++) {
cin >> score[i];
total += score[i];
dp[i][i] = score[i];
}
vec<ll> pre(n, 0LL);
pre[0] = score[0];
for (ll i = 1LL; i < n; i++) {
pre[i] = score[i] + pre[i - 1];
}
// DP on intervals
/*
dp[left][right] = Max score Taro can have with subarray [left,right]
*/
for (ll ln = 2; ln < n + 1; ln++) {
for (ll left = 0LL; left < n - ln + 1; left++) {
ll right = left + ln - 1;
dp[left][right] = max(
score[left] + (sub_sum(pre, left + 1, right) - dp[left + 1][right]),
score[right] + (sub_sum(pre, left, right - 1) - dp[left][right - 1]));
}
}
ll ans = dp[0][n - 1] - (total - dp[0][n - 1]);
cout << ans << endl;
}
// #############################################################################
// Driver
int main() {
io_handler();
int t = 1;
// cin>>t;
for (int i = 0; i < t; i++) {
solve();
}
#ifdef LOCAL_TESTING
cout << "⏰ Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}
// #############################################################################
| // #############################################################################
#include "bits/stdc++.h"
// #############################################################################
// Macros
#define MOD 1000000007
#define eb emplace_back
#define mp make_pair
#define fr first
#define sc second
#define vec vector
#define len(x) x.size()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define all(v) v.begin(), v.end()
#define alla(a, n) a, a + n
#define sum(v) accumulate(all(v), 0LL)
// Type Defs
typedef long long ll;
typedef unsigned long long ull;
// #############################################################################
using namespace std;
// #############################################################################
// Helpers
void io_handler() {
#ifdef LOCAL_TESTING
freopen("inputf.in", "r", stdin);
freopen("outputf.in", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
template <typename T> void print_matrix(vector<vector<T>> mat) {
for (auto row : mat) {
for (auto e : row) {
cout << e << " ";
}
cout << endl;
}
}
template <typename T> void print_vector(vector<T> v) {
for (auto e : v) {
cout << e << " ";
}
cout << endl;
}
template <typename T> map<T, T> get_count(vector<T> v) {
map<T, T> count;
for (auto e : v) {
if (count.find(e) != count.end()) {
count[e]++;
} else {
count[e] = 1;
}
}
return count;
}
inline ll sub_sum(vec<ll> pre, ll left, ll right) {
return pre[right] - (left > 0 ? pre[left - 1] : 0LL);
}
// #############################################################################
// Solution
void solve() {
ll n;
cin >> n;
vec<ll> score(n);
ll total = 0LL;
vec<vec<ll>> dp(n, vec<ll>(n, 0));
for (ll i = 0LL; i < n; i++) {
cin >> score[i];
total += score[i];
dp[i][i] = score[i];
}
vec<ll> pre(n, 0LL);
pre[0] = score[0];
for (ll i = 1LL; i < n; i++) {
pre[i] = score[i] + pre[i - 1];
}
// DP on intervals
/*
dp[left][right] = Max score Taro can have with subarray [left,right]
*/
for (ll ln = 2; ln < n + 1; ln++) {
for (ll left = 0LL; left < n - ln + 1; left++) {
ll right = left + ln - 1;
dp[left][right] = max(
score[left] + (pre[right] - (left + 1 > 0 ? pre[left] : 0LL) -
dp[left + 1][right]),
score[right] + (pre[right - 1] - (left > 0 ? pre[left - 1] : 0LL) -
dp[left][right - 1]));
}
}
ll ans = dp[0][n - 1] - (total - dp[0][n - 1]);
cout << ans << endl;
}
// #############################################################################
// Driver
int main() {
io_handler();
int t = 1;
// cin>>t;
for (int i = 0; i < t; i++) {
solve();
}
#ifdef LOCAL_TESTING
cout << "⏰ Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}
// #############################################################################
| replace | 86 | 88 | 86 | 90 | TLE | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long int
#define vi vector<int>
#define pii pair<long long, long long>
#define pll pair<int, int>
#define mod 1000000007
#define hell 998244353
#define all(x) x.begin(), x.end()
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb push_back
#define ff first
#define ss second
#define rep(n) for (int i = 0; i < n; i++)
#define lb lower_bound
#define up upper_bound
int dp[3005][3005];
void solve() {
int n;
cin >> n;
int sum = 0;
vi v(n);
rep(n) {
cin >> v[i];
sum += v[i];
}
for (int gap = 0; gap < n; gap++) {
for (int i = 0, j = gap; j < n; i++, j++) {
// f(i,j)=max(vi,min(f(i+2,j),f(i+1,j-1)),vj+min(f(i+1,j-1),f(i,j-2)))
int x = i + 2 <= j ? dp[i + 2][j] : 0;
int y = i + 1 <= j - 1 ? dp[i + 1][j - 1] : 0;
int z = i <= j - 2 ? dp[i][j - 2] : 0;
dp[i][j] = max(v[i] + min(x, y), v[j] + min(y, z));
}
}
cout << dp[0][n - 1] - (sum - dp[0][n - 1]);
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int T;
T = 1;
while (T--) {
solve();
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long int
#define vi vector<int>
#define pii pair<long long, long long>
#define pll pair<int, int>
#define mod 1000000007
#define hell 998244353
#define all(x) x.begin(), x.end()
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb push_back
#define ff first
#define ss second
#define rep(n) for (int i = 0; i < n; i++)
#define lb lower_bound
#define up upper_bound
int dp[3005][3005];
void solve() {
int n;
cin >> n;
int sum = 0;
vi v(n);
rep(n) {
cin >> v[i];
sum += v[i];
}
for (int gap = 0; gap < n; gap++) {
for (int i = 0, j = gap; j < n; i++, j++) {
// f(i,j)=max(vi,min(f(i+2,j),f(i+1,j-1)),vj+min(f(i+1,j-1),f(i,j-2)))
int x = i + 2 <= j ? dp[i + 2][j] : 0;
int y = i + 1 <= j - 1 ? dp[i + 1][j - 1] : 0;
int z = i <= j - 2 ? dp[i][j - 2] : 0;
dp[i][j] = max(v[i] + min(x, y), v[j] + min(y, z));
}
}
cout << dp[0][n - 1] - (sum - dp[0][n - 1]);
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int T;
T = 1;
while (T--) {
solve();
}
return 0;
} | delete | 50 | 55 | 50 | 50 | -6 | terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
#define err(args...) \
{}
#ifdef DEBUG
#include "_debug.cpp"
#endif
using namespace std;
using ll = long long;
using ld = long double;
template <typename T> using lim = numeric_limits<T>;
template <typename T> istream &operator>>(istream &is, vector<T> &a) {
for (T &x : a) {
is >> x;
}
return is;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
const int N = 3000;
static ll opt[N + 1][N + 1][2];
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= n; j++) {
if (i <= j) {
opt[i][j][0] = max(a[i] + opt[i + 1][j][1], a[j] + opt[i][j - 1][1]);
opt[i][j][1] = min(opt[i + 1][j][0] - a[i], opt[i][j - 1][0] - a[j]);
}
}
}
cout << opt[1][n][0] << endl;
return 0;
}
| #include <bits/stdc++.h>
#define err(args...) \
{}
#ifdef DEBUG
#include "_debug.cpp"
#endif
using namespace std;
using ll = long long;
using ld = long double;
template <typename T> using lim = numeric_limits<T>;
template <typename T> istream &operator>>(istream &is, vector<T> &a) {
for (T &x : a) {
is >> x;
}
return is;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
const int N = 3000;
static ll opt[N + 2][N + 1][2];
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= n; j++) {
if (i <= j) {
opt[i][j][0] = max(a[i] + opt[i + 1][j][1], a[j] + opt[i][j - 1][1]);
opt[i][j][1] = min(opt[i + 1][j][0] - a[i], opt[i][j - 1][0] - a[j]);
}
}
}
cout << opt[1][n][0] << endl;
return 0;
}
| replace | 26 | 27 | 26 | 27 | -11 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define pb push_back
using namespace std;
const int N = 1e6 + 69, INF = 1e9;
const ll INFLL = 1e18;
int n, k;
ll dp[3009][3009], A[N];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> A[i];
}
for (int cnt = 1; cnt <= n; cnt++) {
for (int l = 1; l <= n; l++) {
int r = cnt - l + 1;
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[1][n] << endl;
}
| #include <bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define pb push_back
using namespace std;
const int N = 1e6 + 69, INF = 1e9;
const ll INFLL = 1e18;
int n, k;
ll dp[3009][3009], A[N];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> A[i];
}
for (int cnt = 1; cnt <= n; cnt++) {
for (int l = 1; l <= n; l++) {
int r = l + cnt - 1;
if (r > n)
continue;
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[1][n] << endl;
}
| replace | 26 | 27 | 26 | 29 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
#define mod 1000000007
#define pb push_back
#define ll long long
#define ld long double
#define ft first
#define sd second
using namespace std;
const int N = 1e3 + 9;
ll a[N];
ll dp[N][N][2];
ll solve(int l, int r, bool turn) {
if (l > r)
return 0;
ll &ret = dp[l][r][turn];
if (ret != -1)
return ret;
if (turn) {
ret =
min(solve(l + 1, r, turn ^ 1) - a[l], solve(l, r - 1, turn ^ 1) - a[r]);
} else {
ret =
max(solve(l + 1, r, turn ^ 1) + a[l], solve(l, r - 1, turn ^ 1) + a[r]);
}
return ret;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
memset(dp, -1, sizeof dp);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
cout << solve(1, n, 0);
return 0;
} | #include <bits/stdc++.h>
#define mod 1000000007
#define pb push_back
#define ll long long
#define ld long double
#define ft first
#define sd second
using namespace std;
const int N = 3e3 + 9;
ll a[N];
ll dp[N][N][2];
ll solve(int l, int r, bool turn) {
if (l > r)
return 0;
ll &ret = dp[l][r][turn];
if (ret != -1)
return ret;
if (turn) {
ret =
min(solve(l + 1, r, turn ^ 1) - a[l], solve(l, r - 1, turn ^ 1) - a[r]);
} else {
ret =
max(solve(l + 1, r, turn ^ 1) + a[l], solve(l, r - 1, turn ^ 1) + a[r]);
}
return ret;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
memset(dp, -1, sizeof dp);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
cout << solve(1, n, 0);
return 0;
} | replace | 9 | 10 | 9 | 10 | 0 | |
p03171 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<db, db> pdd;
const int maxn = 305;
const int Mod = 1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const double e = exp(1);
const db PI = acos(-1);
const db ERR = 1e-10;
#define Se second
#define Fi first
#define pb push_back
#define dbg(x) cout << #x << " = " << (x) << endl
#define dbg2(x1, x2) \
cout << #x1 << " = " << x1 << " " << #x2 << " = " << x2 << endl
#define dbg3(x1, x2, x3) \
cout << #x1 << " = " << x1 << " " << #x2 << " = " << x2 << " " << #x3 \
<< " = " << x3 << endl
ll dp[maxn][maxn][2];
ll a[maxn];
int main() {
// ios::sync_with_stdio(false);
// freopen("a.txt","r",stdin);
// freopen("b.txt","w",stdout);
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%lld", &a[i]);
for (int i = n; i >= 1; i--) {
for (int j = i; j <= n; j++) {
if (i == j) {
dp[i][j][0] = a[i];
dp[i][j][1] = -a[i];
} else {
dp[i][j][0] = max(dp[i + 1][j][1] + a[i], dp[i][j - 1][1] + a[j]);
dp[i][j][1] = min(dp[i + 1][j][0] - a[i], dp[i][j - 1][0] - a[j]);
}
}
}
printf("%lld\n", dp[1][n][0]);
// cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" <<
// endl;
return 0;
}
| #include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<db, db> pdd;
const int maxn = 3005;
const int Mod = 1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const double e = exp(1);
const db PI = acos(-1);
const db ERR = 1e-10;
#define Se second
#define Fi first
#define pb push_back
#define dbg(x) cout << #x << " = " << (x) << endl
#define dbg2(x1, x2) \
cout << #x1 << " = " << x1 << " " << #x2 << " = " << x2 << endl
#define dbg3(x1, x2, x3) \
cout << #x1 << " = " << x1 << " " << #x2 << " = " << x2 << " " << #x3 \
<< " = " << x3 << endl
ll dp[maxn][maxn][2];
ll a[maxn];
int main() {
// ios::sync_with_stdio(false);
// freopen("a.txt","r",stdin);
// freopen("b.txt","w",stdout);
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%lld", &a[i]);
for (int i = n; i >= 1; i--) {
for (int j = i; j <= n; j++) {
if (i == j) {
dp[i][j][0] = a[i];
dp[i][j][1] = -a[i];
} else {
dp[i][j][0] = max(dp[i + 1][j][1] + a[i], dp[i][j - 1][1] + a[j]);
dp[i][j][1] = min(dp[i + 1][j][0] - a[i], dp[i][j - 1][0] - a[j]);
}
}
}
printf("%lld\n", dp[1][n][0]);
// cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" <<
// endl;
return 0;
}
| replace | 22 | 23 | 22 | 23 | 0 | |
p03171 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <string>
using namespace std;
long long max(long long a, long long b) { return a > b ? a : b; }
void get_opt_scoredelta(int N, long long *A) {
long long dp[N + 1][N + 1];
// dp[i][j] = score difference between current player and the other player for
// the subsequence ai....aj dp[i][j] = max(a[i] - dp[i+1][j], a[j] -
// dp[i][j-1]) return dp[0][N-1]
for (int k = 0; k < N; k++)
dp[k][k] = A[k];
for (int p = 2; p <= N; p++) {
for (int i = 0; i < N - 1; i++) {
int j = i + p - 1;
dp[i][j] = max(A[i] - dp[i + 1][j], A[j] - dp[i][j - 1]);
}
}
cout << dp[0][N - 1] << endl;
}
int main() {
int N;
cin >> N;
long long A[N];
for (int i = 0; i < N; i++)
cin >> A[i];
/*for(int i=0; i<N; i++)
cout << P[i] << " ";
cout << endl;
*/
get_opt_scoredelta(N, A);
return 0;
}
| #include <algorithm>
#include <iostream>
#include <string>
using namespace std;
long long max(long long a, long long b) { return a > b ? a : b; }
void get_opt_scoredelta(int N, long long *A) {
long long dp[N + 1][N + 1];
// dp[i][j] = score difference between current player and the other player for
// the subsequence ai....aj dp[i][j] = max(a[i] - dp[i+1][j], a[j] -
// dp[i][j-1]) return dp[0][N-1]
for (int k = 0; k < N; k++)
dp[k][k] = A[k];
for (int p = 2; p <= N; p++) {
for (int i = 0; i < N - 1; i++) {
int j = i + p - 1;
if (j < N)
dp[i][j] = max(A[i] - dp[i + 1][j], A[j] - dp[i][j - 1]);
}
}
cout << dp[0][N - 1] << endl;
}
int main() {
int N;
cin >> N;
long long A[N];
for (int i = 0; i < N; i++)
cin >> A[i];
/*for(int i=0; i<N; i++)
cout << P[i] << " ";
cout << endl;
*/
get_opt_scoredelta(N, A);
return 0;
}
| replace | 17 | 18 | 17 | 19 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
#define lli long long int
#define vec2 vector<vector<lli>>
#define fio \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0)
#define FRE \
freopen("explicit.in", "r", stdin); \
freopen("explicit.out", "w", stdout)
#define mp make_pair
#define pll pair<lli, lli>
#define vll vector<lli>
#define pq2 priority_queue<pair<lli, lli>>
#define f first
#define s second
#define pb push_back
#define pf push_front
#define minii LLONG_MAX
#define ld long double
const long double pi = 3.1415926535898;
#define dec greater<lli>()
// const lli mod=1000000007;
const lli mod = 100000000;
using namespace std;
int main() {
fio;
lli n;
cin >> n;
lli a[n];
for (int i = 1; i <= n; i++)
cin >> a[i];
lli dp[300005][300005];
memset(dp, 0, sizeof(dp));
for (int i = n; i >= 1; i--) {
for (int j = i; j <= n; j++) {
dp[i][j] = max(a[i] - dp[i + 1][j], a[j] - dp[i][j - 1]);
}
}
cout << dp[1][n];
return 0;
}
| #include <bits/stdc++.h>
#define lli long long int
#define vec2 vector<vector<lli>>
#define fio \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0)
#define FRE \
freopen("explicit.in", "r", stdin); \
freopen("explicit.out", "w", stdout)
#define mp make_pair
#define pll pair<lli, lli>
#define vll vector<lli>
#define pq2 priority_queue<pair<lli, lli>>
#define f first
#define s second
#define pb push_back
#define pf push_front
#define minii LLONG_MAX
#define ld long double
const long double pi = 3.1415926535898;
#define dec greater<lli>()
// const lli mod=1000000007;
const lli mod = 100000000;
using namespace std;
int main() {
fio;
lli n;
cin >> n;
lli a[n];
for (int i = 1; i <= n; i++)
cin >> a[i];
lli dp[3005][3005];
memset(dp, 0, sizeof(dp));
for (int i = n; i >= 1; i--) {
for (int j = i; j <= n; j++) {
dp[i][j] = max(a[i] - dp[i + 1][j], a[j] - dp[i][j - 1]);
}
}
cout << dp[1][n];
return 0;
}
| replace | 33 | 34 | 33 | 34 | -11 | |
p03171 | C++ | Runtime Error | // In the name of GOD
#include <bits/stdc++.h>
#define ll long long
#define pp pair<int, int>
using namespace std;
const int N = 3e2 + 10, MOD = 1e9 + 7;
const ll INF = 1e18;
vector<int> a(N);
ll dp[N][N];
bool vis[N][N] = {0};
ll f(int l, int r) {
if (vis[l][r])
return dp[l][r];
if (l == r)
return a[l];
ll t1 = a[l] - f(l + 1, r);
ll t2 = a[r] - f(l, r - 1);
dp[l][r] = max(t1, t2);
vis[l][r] = true;
return dp[l][r];
}
void solve() {
int n;
cin >> n;
for (int i = 0; i < n; ++i)
cin >> a[i];
cout << f(0, n - 1);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tt = 1;
// cin >> tt;
for (int tc = 1; tc <= tt; ++tc) {
solve();
}
} | // In the name of GOD
#include <bits/stdc++.h>
#define ll long long
#define pp pair<int, int>
using namespace std;
const int N = 3e3 + 10, MOD = 1e9 + 7;
const ll INF = 1e18;
vector<int> a(N);
ll dp[N][N];
bool vis[N][N] = {0};
ll f(int l, int r) {
if (vis[l][r])
return dp[l][r];
if (l == r)
return a[l];
ll t1 = a[l] - f(l + 1, r);
ll t2 = a[r] - f(l, r - 1);
dp[l][r] = max(t1, t2);
vis[l][r] = true;
return dp[l][r];
}
void solve() {
int n;
cin >> n;
for (int i = 0; i < n; ++i)
cin >> a[i];
cout << f(0, n - 1);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tt = 1;
// cin >> tt;
for (int tc = 1; tc <= tt; ++tc) {
solve();
}
} | replace | 6 | 7 | 6 | 7 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define mod ((int)1e9 + 7)
#define lim 1000000000000000007
#define lim1 18446744073709551615 // Unsigned
#define sq(a) ((a) * (a))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define mms(v, i) memset(v, i, sizeof(v))
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define REPN(i, a, b) for (int i = a; i >= b; i--)
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> PL;
typedef pair<ll, int> PLI;
typedef pair<int, ll> PIL;
typedef pair<int, pair<int, int>> pii;
typedef pair<double, double> pdd;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
indexed_set;
ll power(ll a, ll b) {
ll res = 1;
while (b) {
if (b % 2 == 1)
res = (res * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return res;
}
ll gcdll(ll a, ll b) {
if (b == 0)
return a;
return gcdll(b, a % b);
}
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
const int N = (int)1e5 + 5;
const int M = (int)1e3 + 5;
const int Q = 301;
const int logN = 19;
ll dp[M][M];
void solve() {
int n;
cin >> n;
vector<int> a(n + 1);
REP(i, 1, n) { cin >> a[i]; }
REPN(i, n, 1) {
dp[i][i] = a[i];
REP(j, i + 1, n) {
dp[i][j] = max(a[j] - dp[i][j - 1], a[i] - dp[i + 1][j]);
}
}
cout << dp[1][n];
}
int main() {
// freopen("output.txt","r",stdin);
// freopen("output.txt","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T = 1;
// cin>>T;
REP(TC, 1, T) {
// cout<<"Case #"<<TC<<": ";
solve();
cout << "\n";
}
}
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define mod ((int)1e9 + 7)
#define lim 1000000000000000007
#define lim1 18446744073709551615 // Unsigned
#define sq(a) ((a) * (a))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define mms(v, i) memset(v, i, sizeof(v))
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define REPN(i, a, b) for (int i = a; i >= b; i--)
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> PL;
typedef pair<ll, int> PLI;
typedef pair<int, ll> PIL;
typedef pair<int, pair<int, int>> pii;
typedef pair<double, double> pdd;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
indexed_set;
ll power(ll a, ll b) {
ll res = 1;
while (b) {
if (b % 2 == 1)
res = (res * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return res;
}
ll gcdll(ll a, ll b) {
if (b == 0)
return a;
return gcdll(b, a % b);
}
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
const int N = (int)1e5 + 5;
const int M = (int)3e3 + 5;
const int Q = 301;
const int logN = 19;
ll dp[M][M];
void solve() {
int n;
cin >> n;
vector<int> a(n + 1);
REP(i, 1, n) { cin >> a[i]; }
REPN(i, n, 1) {
dp[i][i] = a[i];
REP(j, i + 1, n) {
dp[i][j] = max(a[j] - dp[i][j - 1], a[i] - dp[i + 1][j]);
}
}
cout << dp[1][n];
}
int main() {
// freopen("output.txt","r",stdin);
// freopen("output.txt","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T = 1;
// cin>>T;
REP(TC, 1, T) {
// cout<<"Case #"<<TC<<": ";
solve();
cout << "\n";
}
}
| replace | 66 | 67 | 66 | 67 | 0 | |
p03171 | C++ | Time Limit Exceeded |
#include <bits/stdc++.h>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#define endl "\n"
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(), c.end()
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb emplace_back
#define f first
#define s second
using namespace std;
int main() {
std::ios::sync_with_stdio(false);
int T = 1;
// cin>>T;
// cin.ignore(); must be there when using getline(cin, s)
while (T--) {
ll n;
cin >> n;
ll a[n];
for (ll i = 0; i < n; i++)
cin >> a[i];
ll dp[n + 1][n + 1];
for (ll l = n - 1; l >= 0; l++) {
for (ll 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];
}
return 0;
}
|
#include <bits/stdc++.h>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#define endl "\n"
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(), c.end()
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb emplace_back
#define f first
#define s second
using namespace std;
int main() {
std::ios::sync_with_stdio(false);
int T = 1;
// cin>>T;
// cin.ignore(); must be there when using getline(cin, s)
while (T--) {
ll n;
cin >> n;
ll a[n];
for (ll i = 0; i < n; i++)
cin >> a[i];
ll dp[n + 1][n + 1];
for (ll l = n - 1; l >= 0; l--) {
for (ll 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];
}
return 0;
}
| replace | 36 | 37 | 36 | 37 | TLE | |
p03171 | C++ | Runtime Error |
/** Which of the favors of your Lord will you deny ? **/
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define PII pair<int, int>
#define PLL pair<LL, LL>
#define MP make_pair
#define F first
#define S second
#define ALL(x) (x).begin(), (x).end()
#define DBG(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << endl
#define READ freopen("alu.txt", "r", stdin)
#define WRITE freopen("vorta.txt", "w", stdout)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <class TIn>
using indexed_set = tree<TIn, null_type, less<TIn>, rb_tree_tag,
tree_order_statistics_node_update>;
/**
PBDS
-------------------------------------------------
1) insert(value)
2) erase(value)
3) order_of_key(value) // 0 based indexing
4) *find_by_order(position) // 0 based indexing
**/
template <class T1, class T2> ostream &operator<<(ostream &os, pair<T1, T2> &p);
template <class T> ostream &operator<<(ostream &os, vector<T> &v);
template <class T> ostream &operator<<(ostream &os, set<T> &v);
inline void optimizeIO() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
const int nmax = 3e3 + 7;
const LL LINF = 1e17;
template <class T> string to_str(T x) {
stringstream ss;
ss << x;
return ss.str();
}
// bool cmp(const PII &A,const PII &B)
//{
//
// }
LL dp[nmax][nmax];
int main() {
optimizeIO();
int n;
cin >> n;
vector<LL> v(n);
for (int i = 0; i < n; i++)
cin >> v[i];
/// can't do this as we need to start from segment of length 1 in iterative dp
// for(int l=0; l<n; l++)
// for(int r=n-1; r>=l; r--)
for (int l = n - 1; l >= 0; l--) {
for (int r = l; r < n; r++) {
if (l == r)
dp[l][r] = v[l];
else
dp[l][r] = max(v[l] - dp[l - 1][r], v[r] - dp[l][r - 1]);
}
}
cout << dp[0][n - 1] << endl;
return 0;
}
/**
**/
template <class T1, class T2>
ostream &operator<<(ostream &os, pair<T1, T2> &p) {
os << "{" << p.first << ", " << p.second << "} ";
return os;
}
template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
os << "[ ";
for (int i = 0; i < v.size(); i++) {
os << v[i] << " ";
}
os << " ]";
return os;
}
template <class T> ostream &operator<<(ostream &os, set<T> &v) {
os << "[ ";
for (T i : v) {
os << i << " ";
}
os << " ]";
return os;
}
|
/** Which of the favors of your Lord will you deny ? **/
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define PII pair<int, int>
#define PLL pair<LL, LL>
#define MP make_pair
#define F first
#define S second
#define ALL(x) (x).begin(), (x).end()
#define DBG(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << endl
#define READ freopen("alu.txt", "r", stdin)
#define WRITE freopen("vorta.txt", "w", stdout)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <class TIn>
using indexed_set = tree<TIn, null_type, less<TIn>, rb_tree_tag,
tree_order_statistics_node_update>;
/**
PBDS
-------------------------------------------------
1) insert(value)
2) erase(value)
3) order_of_key(value) // 0 based indexing
4) *find_by_order(position) // 0 based indexing
**/
template <class T1, class T2> ostream &operator<<(ostream &os, pair<T1, T2> &p);
template <class T> ostream &operator<<(ostream &os, vector<T> &v);
template <class T> ostream &operator<<(ostream &os, set<T> &v);
inline void optimizeIO() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
const int nmax = 3e3 + 7;
const LL LINF = 1e17;
template <class T> string to_str(T x) {
stringstream ss;
ss << x;
return ss.str();
}
// bool cmp(const PII &A,const PII &B)
//{
//
// }
LL dp[nmax][nmax];
int main() {
optimizeIO();
int n;
cin >> n;
vector<LL> v(n);
for (int i = 0; i < n; i++)
cin >> v[i];
/// can't do this as we need to start from segment of length 1 in iterative dp
// for(int l=0; l<n; l++)
// for(int r=n-1; r>=l; r--)
for (int l = n - 1; l >= 0; l--) {
for (int r = l; r < n; r++) {
if (l == r)
dp[l][r] = v[l];
else
dp[l][r] = max(v[l] - dp[l + 1][r], v[r] - dp[l][r - 1]);
}
}
cout << dp[0][n - 1] << endl;
return 0;
}
/**
**/
template <class T1, class T2>
ostream &operator<<(ostream &os, pair<T1, T2> &p) {
os << "{" << p.first << ", " << p.second << "} ";
return os;
}
template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
os << "[ ";
for (int i = 0; i < v.size(); i++) {
os << v[i] << " ";
}
os << " ]";
return os;
}
template <class T> ostream &operator<<(ostream &os, set<T> &v) {
os << "[ ";
for (T i : v) {
os << i << " ";
}
os << " ]";
return os;
}
| replace | 81 | 82 | 81 | 82 | -11 | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#include <chrono>
#include <cstdio>
#include <random>
using namespace std;
#define DRACARYS \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cin.exceptions(cin.failbit); \
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a) memset(a, -1, sizeof(a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
#define PI 3.14159265
const long long int MAXN = 3e3 + 10;
const long long int MINN = 1e5 + 10;
const long long int inf = 1e18 + 7;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef stack<int> st;
ll n, a[MAXN], dp[MAXN][MAXN], s;
ll foo(int l, int r) {
if (l + 1 == r) {
dp[l][r] = max(a[l], a[r]);
return dp[l][r];
}
if (l == r) {
dp[l][r] = a[l];
return dp[l][r];
}
ll x, y, z;
x = (dp[l + 1][r - 1] == -1 ? foo(l + 1, r - 1) : dp[l + 1][r - 1]);
y = (dp[l + 2][r] == -1 ? foo(l + 2, r) : dp[l + 2][r]);
z = (dp[l][r - 2] == -1 ? foo(l, r - 2) : dp[l][r - 2]);
return max(a[l] + min(x, y), a[r] + min(x, z));
}
int main() {
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
DRACARYS
cin >> n;
fill(dp);
repA(i, 1, n) cin >> a[i], s += a[i];
cout << 2 * foo(1, n) - s << endl;
return 0;
}
| #include <bits/stdc++.h>
#include <chrono>
#include <cstdio>
#include <random>
using namespace std;
#define DRACARYS \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cin.exceptions(cin.failbit); \
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a) memset(a, -1, sizeof(a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
#define PI 3.14159265
const long long int MAXN = 3e3 + 10;
const long long int MINN = 1e5 + 10;
const long long int inf = 1e18 + 7;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef stack<int> st;
ll n, a[MAXN], dp[MAXN][MAXN], s;
ll foo(int l, int r) {
if (l + 1 == r) {
dp[l][r] = max(a[l], a[r]);
return dp[l][r];
}
if (l == r) {
dp[l][r] = a[l];
return dp[l][r];
}
ll x, y, z;
x = (dp[l + 1][r - 1] == -1 ? foo(l + 1, r - 1) : dp[l + 1][r - 1]);
y = (dp[l + 2][r] == -1 ? foo(l + 2, r) : dp[l + 2][r]);
z = (dp[l][r - 2] == -1 ? foo(l, r - 2) : dp[l][r - 2]);
dp[l][r] = max(a[l] + min(x, y), a[r] + min(x, z));
return dp[l][r];
}
int main() {
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
DRACARYS
cin >> n;
fill(dp);
repA(i, 1, n) cin >> a[i], s += a[i];
cout << 2 * foo(1, n) - s << endl;
return 0;
}
| replace | 48 | 49 | 48 | 50 | TLE | |
p03171 | C++ | Runtime Error | /*
_oo0oo_
o8888888o
88" . "88
(| -_- |)
0\ = /0
___/`---'\___
.' \\| |// '.
/ \\||| : |||// \
/ _||||| -:- |||||- \
| | \\\ - /// | |
| \_| ''\---/'' |_/ |
\ .-\__ '-' ___/-. /
___'. .' /--.--\ `. .'___
."" '< `.___\_<|>_/___.' >' "".
| | : `- \`.;`\ _ /`;.`/ - ` : | |
\ \ `_. \_ __\ /__ _/ .-` / /
=====`-.____`.___ \_____/___.-`___.-'=====
`=---='
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vikash Kumar @ Codechef/codeforces
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include <bits/stdc++.h>
#define ll long long int
#define ll_MAX LLONG_MAX
#define ll_MIN LLONG_MIN
#define pi 3.14159265358979323846
#define count_1(n) __builtin_popcountll(n)
#define MOD 1000000007
#define MAX 1e9
#define MIN -1e9
#define itoc(c) ((char)(((int)'0') + c))
#define mid(s, e) (s + (e - s) / 2)
const int MX = 10010896;
const int lmt = 3164;
const int N = 10000001;
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
using namespace std;
ll extgcd(ll a, ll b, ll &x, ll &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
} else {
int g = extgcd(b, a % b, y, x);
y -= a / b * x;
return g;
}
}
ll modpow(ll a, ll b) {
ll res = 1;
a %= MOD;
for (; b; b >>= 1) {
if (b & 1)
res = res * a % MOD;
a = a * a % MOD;
}
return res;
}
const ll MXX = 3005;
ll dp[MXX][MXX];
inline void solve() {
/*_Start_*/
ll n;
cin >> n;
ll a[n];
for (ll i = 0; i < n; i++)
cin >> a[i];
for (ll l = 0; l < n; l++) {
for (ll r = l; r >= 0; 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[n - 1][0] << endl;
/*_End_*/
}
int main() {
cout << fixed << setprecision(12);
freopen("input.txt", "r", stdin);
fastio;
ll t = 1, tc = 1;
// cin>>t;
while (t--) {
solve();
}
return 0;
} | /*
_oo0oo_
o8888888o
88" . "88
(| -_- |)
0\ = /0
___/`---'\___
.' \\| |// '.
/ \\||| : |||// \
/ _||||| -:- |||||- \
| | \\\ - /// | |
| \_| ''\---/'' |_/ |
\ .-\__ '-' ___/-. /
___'. .' /--.--\ `. .'___
."" '< `.___\_<|>_/___.' >' "".
| | : `- \`.;`\ _ /`;.`/ - ` : | |
\ \ `_. \_ __\ /__ _/ .-` / /
=====`-.____`.___ \_____/___.-`___.-'=====
`=---='
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vikash Kumar @ Codechef/codeforces
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include <bits/stdc++.h>
#define ll long long int
#define ll_MAX LLONG_MAX
#define ll_MIN LLONG_MIN
#define pi 3.14159265358979323846
#define count_1(n) __builtin_popcountll(n)
#define MOD 1000000007
#define MAX 1e9
#define MIN -1e9
#define itoc(c) ((char)(((int)'0') + c))
#define mid(s, e) (s + (e - s) / 2)
const int MX = 10010896;
const int lmt = 3164;
const int N = 10000001;
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
using namespace std;
ll extgcd(ll a, ll b, ll &x, ll &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
} else {
int g = extgcd(b, a % b, y, x);
y -= a / b * x;
return g;
}
}
ll modpow(ll a, ll b) {
ll res = 1;
a %= MOD;
for (; b; b >>= 1) {
if (b & 1)
res = res * a % MOD;
a = a * a % MOD;
}
return res;
}
const ll MXX = 3005;
ll dp[MXX][MXX];
inline void solve() {
/*_Start_*/
ll n;
cin >> n;
ll a[n];
for (ll i = 0; i < n; i++)
cin >> a[i];
for (ll l = 0; l < n; l++) {
for (ll r = l; r >= 0; 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[n - 1][0] << endl;
/*_End_*/
}
int main() {
cout << fixed << setprecision(12);
// freopen("input.txt", "r", stdin);
fastio;
ll t = 1, tc = 1;
// cin>>t;
while (t--) {
solve();
}
return 0;
} | replace | 89 | 90 | 89 | 90 | -11 | |
p03171 | C++ | Time Limit Exceeded | #define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
//
#define int long long
#define pb(x) push_back(x)
#define m0(x) memset((x), 0, sizeof(x))
#define mm(x) memset((x), -1, sizeof(x))
// container
#define ALL(x) (x).begin(), (x).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(i, c) \
for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define UNIQUE(v) (v).erase(unique((v).begin(), (v).end()), (v).end());
#define PERM(c) \
sort(ALL(c)); \
for (bool c##p = 1; c##p; c##p = next_permutation(ALL(c)))
// debug
#define GET_VAR_NAME(variable) #variable
#define test(x) cout << GET_VAR_NAME(x) << " = " << x << endl;
// bit_macro
#define bit(n) (1LL << (n))
#define bitset(a, b) (a) |= (1 << (b))
#define bitunset(a, b) (a) |= ~(1 << (b))
#define bitcheck(a, b) ((((a) >> (b)) & 1) == 1)
#define bitcount(a) __builtin_popcountll((a))
// typedef
typedef long long lint;
typedef unsigned long long ull;
typedef complex<long double> Complex;
typedef pair<int, int> P;
typedef tuple<int, int, int> TP;
typedef vector<int> vec;
typedef vector<vec> mat;
// constant
const int INF = (int)1e18;
const int MOD = (int)1e9 + 7;
const double EPS = (double)1e-10;
const int dx[] = {-1, 0, 0, 1, 0, -1, -1, 1, 1};
const int dy[] = {0, -1, 1, 0, 0, 1, -1, 1, -1};
//
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
//
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
//
struct Accelerate_Cin {
Accelerate_Cin() {
cin.tie(0);
ios::sync_with_stdio(0);
cout << fixed << setprecision(20);
};
};
int N;
vec a;
int dp[3030][3030];
bool flag[3030][3030];
int rec(int l, int r, int turn) {
if (flag[l][r])
return dp[l][r];
if (r < l || r == -1 || l == N)
return 0;
int res;
if (turn == 0) {
res = max(rec(l, r - 1, (turn + 1) % 2) + a[r],
rec(l + 1, r, (turn + 1) % 2) + a[l]);
} else {
res = min(rec(l, r - 1, (turn + 1) % 2) - a[r],
rec(l + 1, r, (turn + 1) % 2) - a[l]);
}
dp[l][r] = true;
return dp[l][r] = res;
}
signed main() {
cin >> N;
for (int i = 0; i < N; i++) {
int tmp;
cin >> tmp;
a.pb(tmp);
}
m0(flag);
cout << rec(0, N - 1, 0) << endl;
return 0;
}
| #define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
//
#define int long long
#define pb(x) push_back(x)
#define m0(x) memset((x), 0, sizeof(x))
#define mm(x) memset((x), -1, sizeof(x))
// container
#define ALL(x) (x).begin(), (x).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(i, c) \
for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define UNIQUE(v) (v).erase(unique((v).begin(), (v).end()), (v).end());
#define PERM(c) \
sort(ALL(c)); \
for (bool c##p = 1; c##p; c##p = next_permutation(ALL(c)))
// debug
#define GET_VAR_NAME(variable) #variable
#define test(x) cout << GET_VAR_NAME(x) << " = " << x << endl;
// bit_macro
#define bit(n) (1LL << (n))
#define bitset(a, b) (a) |= (1 << (b))
#define bitunset(a, b) (a) |= ~(1 << (b))
#define bitcheck(a, b) ((((a) >> (b)) & 1) == 1)
#define bitcount(a) __builtin_popcountll((a))
// typedef
typedef long long lint;
typedef unsigned long long ull;
typedef complex<long double> Complex;
typedef pair<int, int> P;
typedef tuple<int, int, int> TP;
typedef vector<int> vec;
typedef vector<vec> mat;
// constant
const int INF = (int)1e18;
const int MOD = (int)1e9 + 7;
const double EPS = (double)1e-10;
const int dx[] = {-1, 0, 0, 1, 0, -1, -1, 1, 1};
const int dy[] = {0, -1, 1, 0, 0, 1, -1, 1, -1};
//
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
//
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
//
struct Accelerate_Cin {
Accelerate_Cin() {
cin.tie(0);
ios::sync_with_stdio(0);
cout << fixed << setprecision(20);
};
};
int N;
vec a;
int dp[3030][3030];
bool flag[3030][3030];
int rec(int l, int r, int turn) {
if (flag[l][r])
return dp[l][r];
if (r < l || r == -1 || l == N)
return 0;
int res;
if (turn == 0) {
res = max(rec(l, r - 1, (turn + 1) % 2) + a[r],
rec(l + 1, r, (turn + 1) % 2) + a[l]);
} else {
res = min(rec(l, r - 1, (turn + 1) % 2) - a[r],
rec(l + 1, r, (turn + 1) % 2) - a[l]);
}
flag[l][r] = true;
return dp[l][r] = res;
}
signed main() {
cin >> N;
for (int i = 0; i < N; i++) {
int tmp;
cin >> tmp;
a.pb(tmp);
}
m0(flag);
cout << rec(0, N - 1, 0) << endl;
return 0;
}
| replace | 95 | 96 | 95 | 96 | TLE | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using pcc = pair<char, char>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
using tuplis = pair<ll, pll>;
const ll LINF = 0x1fffffffffffffff;
const ll MOD = 1000000007;
const ll MODD = 0x3b800001;
const int INF = 0x3fffffff;
const double DINF = numeric_limits<double>::infinity();
const vector<pii> four = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
#define _overload4(_1, _2, _3, _4, name, ...) name
#define _overload3(_1, _2, _3, name, ...) name
#define _rep1(n) _rep2(i, n)
#define _rep2(i, n) _rep3(i, 0, n)
#define _rep3(i, a, b) for (ll i = a; i < b; ++i)
#define _rep4(i, a, b, c) for (ll i = a; i < b; i += c)
#define rep(...) \
_overload4(__VA_ARGS__, _rep4, _rep3, _rep2, _rep1)(__VA_ARGS__)
#define _rrep1(n) _rrep2(i, n)
#define _rrep2(i, n) _rrep3(i, 0, n)
#define _rrep3(i, a, b) for (ll i = b - 1; i >= a; i--)
#define _rrep4(i, a, b, c) for (ll i = a + (b - a - 1) / c * c; i >= a; i -= c)
#define rrep(...) \
_overload4(__VA_ARGS__, _rrep4, _rrep3, _rrep2, _rrep1)(__VA_ARGS__)
#define each(i, a) for (auto &&i : a)
#define sum(...) accumulate(range(__VA_ARGS__), 0LL)
#define dsum(...) accumulate(range(__VA_ARGS__), double(0))
#define _range(i) (i).begin(), (i).end()
#define _range2(i, k) (i).begin(), (i).begin() + k
#define _range3(i, a, b) (i).begin() + a, (i).begin() + b
#define range(...) \
_overload3(__VA_ARGS__, _range3, _range2, _range)(__VA_ARGS__)
#define _rrange(i) (i).rbegin(), (i).rend()
#define _rrange2(i, k) (i).rbegin(), (i).rbegin() + k
#define _rrange3(i, a, b) (i).rbegin() + a, (i).rbegin() + b
#define rrange(...) \
_overload3(__VA_ARGS__, _rrange3, _rrange2, _rrange)(__VA_ARGS__)
#define yes(i) out(i ? "yes" : "no")
#define Yes(i) out(i ? "Yes" : "No")
#define YES(i) out(i ? "YES" : "NO")
#define Possible(i) out(i ? "Possible" : "Impossible")
#define elif else if
#define unless(a) if (!(a))
#define mp make_pair
#define INT(...) \
int __VA_ARGS__; \
in(__VA_ARGS__)
#define LL(...) \
ll __VA_ARGS__; \
in(__VA_ARGS__)
#define STR(...) \
string __VA_ARGS__; \
in(__VA_ARGS__)
#define CHR(...) \
char __VA_ARGS__; \
in(__VA_ARGS__)
#define DBL(...) \
double __VA_ARGS__; \
in(__VA_ARGS__)
#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, ...) \
vector<vector<type>> name(h, vector<type>(__VA_ARGS__)); \
in(name)
template <class T> inline constexpr T gcd(T a, T b) {
if (!a || !b)
return 0;
return a % b ? gcd(b, a % b) : b;
}
template <class T> inline constexpr T lcm(T a, T b) {
return a * b / gcd(a, b);
}
template <class T> inline constexpr T min(vector<T> &v) {
return *min_element(range(v));
}
inline char min(string &v) { return *min_element(range(v)); }
template <class T> inline constexpr T max(vector<T> &v) {
return *max_element(range(v));
}
inline char max(string &v) { return *max_element(range(v)); }
inline ll pow(ll a, ll b) {
if (b == 0)
return 1;
ll ans = pow(a, b / 2);
return ans * ans * (b & 1 ? a : 1);
}
inline ll modpow(ll a, ll b, ll mod) {
if (b == 0)
return 1;
ll ans = pow(a, b / 2);
return ans * ans * (b & 1 ? a : 1) % mod;
}
template <typename T> inline bool update_min(T &mn, const T &cnt) {
if (mn > cnt) {
mn = cnt;
return 1;
} else
return 0;
}
template <typename T> inline bool update_max(T &mx, const T &cnt) {
if (mx < cnt) {
mx = cnt;
return 1;
} else
return 0;
}
inline void in() {}
template <class T> istream &operator>>(istream &is, vector<T> &vec);
template <class T, size_t size>
istream &operator>>(istream &is, array<T, size> &vec);
template <class T, class L> istream &operator>>(istream &is, pair<T, L> &p);
template <class T> ostream &operator<<(ostream &os, vector<T> &vec);
template <class T, class L> ostream &operator<<(ostream &os, pair<T, L> &p);
template <class T> istream &operator>>(istream &is, vector<T> &vec) {
for (T &x : vec)
is >> x;
return is;
}
template <class T, class L> istream &operator>>(istream &is, pair<T, L> &p) {
is >> p.first;
is >> p.second;
return is;
}
template <class T> ostream &operator<<(ostream &os, vector<T> &vec) {
os << vec[0];
rep(i, 1, vec.size()) { os << ' ' << vec[i]; }
return os;
}
template <class T> ostream &operator<<(ostream &os, deque<T> &deq) {
os << deq[0];
rep(i, 1, deq.size()) { os << ' ' << deq[i]; }
return os;
}
template <class T, class L> ostream &operator<<(ostream &os, pair<T, L> &p) {
os << p.first << " " << p.second;
return os;
}
template <class T, class L> pair<T, L> operator+(pair<T, L> a, pair<T, L> b) {
return {a.first + b.first, a.second + b.second};
}
template <class Head, class... Tail>
inline void in(Head &&head, Tail &&...tail) {
cin >> head;
in(move(tail)...);
}
template <class T> inline bool out(T t) {
cout << t << '\n';
return 0;
}
inline bool out() {
cout << '\n';
return 0;
}
template <class Head, class... Tail> inline bool out(Head head, Tail... tail) {
cout << head << ' ';
out(move(tail)...);
return 0;
}
template <class T> inline void err(T t) { cerr << t << '\n'; }
inline void err() { cerr << '\n'; }
template <class Head, class... Tail> inline void err(Head head, Tail... tail) {
cerr << head << ' ';
out(move(tail)...);
}
vv(ll, mem, 3001, 3001, LINF);
vec(ll, c, 3001);
ll dp(ll a, ll b) {
if (a > b)
return mem[a][b] = 0;
if (mem[a][b] != LINF)
return mem[a][b];
if (a > b)
return mem[a][b] = 0;
return mem[a][b] = max(c[a] - dp(a + 1, b), c[b] - dp(a, b - 1));
}
signed main() {
LL(n);
c.resize(n);
in(c);
out(dp(0, n - 1));
}
| #include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using pcc = pair<char, char>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
using tuplis = pair<ll, pll>;
const ll LINF = 0x1fffffffffffffff;
const ll MOD = 1000000007;
const ll MODD = 0x3b800001;
const int INF = 0x3fffffff;
const double DINF = numeric_limits<double>::infinity();
const vector<pii> four = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
#define _overload4(_1, _2, _3, _4, name, ...) name
#define _overload3(_1, _2, _3, name, ...) name
#define _rep1(n) _rep2(i, n)
#define _rep2(i, n) _rep3(i, 0, n)
#define _rep3(i, a, b) for (ll i = a; i < b; ++i)
#define _rep4(i, a, b, c) for (ll i = a; i < b; i += c)
#define rep(...) \
_overload4(__VA_ARGS__, _rep4, _rep3, _rep2, _rep1)(__VA_ARGS__)
#define _rrep1(n) _rrep2(i, n)
#define _rrep2(i, n) _rrep3(i, 0, n)
#define _rrep3(i, a, b) for (ll i = b - 1; i >= a; i--)
#define _rrep4(i, a, b, c) for (ll i = a + (b - a - 1) / c * c; i >= a; i -= c)
#define rrep(...) \
_overload4(__VA_ARGS__, _rrep4, _rrep3, _rrep2, _rrep1)(__VA_ARGS__)
#define each(i, a) for (auto &&i : a)
#define sum(...) accumulate(range(__VA_ARGS__), 0LL)
#define dsum(...) accumulate(range(__VA_ARGS__), double(0))
#define _range(i) (i).begin(), (i).end()
#define _range2(i, k) (i).begin(), (i).begin() + k
#define _range3(i, a, b) (i).begin() + a, (i).begin() + b
#define range(...) \
_overload3(__VA_ARGS__, _range3, _range2, _range)(__VA_ARGS__)
#define _rrange(i) (i).rbegin(), (i).rend()
#define _rrange2(i, k) (i).rbegin(), (i).rbegin() + k
#define _rrange3(i, a, b) (i).rbegin() + a, (i).rbegin() + b
#define rrange(...) \
_overload3(__VA_ARGS__, _rrange3, _rrange2, _rrange)(__VA_ARGS__)
#define yes(i) out(i ? "yes" : "no")
#define Yes(i) out(i ? "Yes" : "No")
#define YES(i) out(i ? "YES" : "NO")
#define Possible(i) out(i ? "Possible" : "Impossible")
#define elif else if
#define unless(a) if (!(a))
#define mp make_pair
#define INT(...) \
int __VA_ARGS__; \
in(__VA_ARGS__)
#define LL(...) \
ll __VA_ARGS__; \
in(__VA_ARGS__)
#define STR(...) \
string __VA_ARGS__; \
in(__VA_ARGS__)
#define CHR(...) \
char __VA_ARGS__; \
in(__VA_ARGS__)
#define DBL(...) \
double __VA_ARGS__; \
in(__VA_ARGS__)
#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, ...) \
vector<vector<type>> name(h, vector<type>(__VA_ARGS__)); \
in(name)
template <class T> inline constexpr T gcd(T a, T b) {
if (!a || !b)
return 0;
return a % b ? gcd(b, a % b) : b;
}
template <class T> inline constexpr T lcm(T a, T b) {
return a * b / gcd(a, b);
}
template <class T> inline constexpr T min(vector<T> &v) {
return *min_element(range(v));
}
inline char min(string &v) { return *min_element(range(v)); }
template <class T> inline constexpr T max(vector<T> &v) {
return *max_element(range(v));
}
inline char max(string &v) { return *max_element(range(v)); }
inline ll pow(ll a, ll b) {
if (b == 0)
return 1;
ll ans = pow(a, b / 2);
return ans * ans * (b & 1 ? a : 1);
}
inline ll modpow(ll a, ll b, ll mod) {
if (b == 0)
return 1;
ll ans = pow(a, b / 2);
return ans * ans * (b & 1 ? a : 1) % mod;
}
template <typename T> inline bool update_min(T &mn, const T &cnt) {
if (mn > cnt) {
mn = cnt;
return 1;
} else
return 0;
}
template <typename T> inline bool update_max(T &mx, const T &cnt) {
if (mx < cnt) {
mx = cnt;
return 1;
} else
return 0;
}
inline void in() {}
template <class T> istream &operator>>(istream &is, vector<T> &vec);
template <class T, size_t size>
istream &operator>>(istream &is, array<T, size> &vec);
template <class T, class L> istream &operator>>(istream &is, pair<T, L> &p);
template <class T> ostream &operator<<(ostream &os, vector<T> &vec);
template <class T, class L> ostream &operator<<(ostream &os, pair<T, L> &p);
template <class T> istream &operator>>(istream &is, vector<T> &vec) {
for (T &x : vec)
is >> x;
return is;
}
template <class T, class L> istream &operator>>(istream &is, pair<T, L> &p) {
is >> p.first;
is >> p.second;
return is;
}
template <class T> ostream &operator<<(ostream &os, vector<T> &vec) {
os << vec[0];
rep(i, 1, vec.size()) { os << ' ' << vec[i]; }
return os;
}
template <class T> ostream &operator<<(ostream &os, deque<T> &deq) {
os << deq[0];
rep(i, 1, deq.size()) { os << ' ' << deq[i]; }
return os;
}
template <class T, class L> ostream &operator<<(ostream &os, pair<T, L> &p) {
os << p.first << " " << p.second;
return os;
}
template <class T, class L> pair<T, L> operator+(pair<T, L> a, pair<T, L> b) {
return {a.first + b.first, a.second + b.second};
}
template <class Head, class... Tail>
inline void in(Head &&head, Tail &&...tail) {
cin >> head;
in(move(tail)...);
}
template <class T> inline bool out(T t) {
cout << t << '\n';
return 0;
}
inline bool out() {
cout << '\n';
return 0;
}
template <class Head, class... Tail> inline bool out(Head head, Tail... tail) {
cout << head << ' ';
out(move(tail)...);
return 0;
}
template <class T> inline void err(T t) { cerr << t << '\n'; }
inline void err() { cerr << '\n'; }
template <class Head, class... Tail> inline void err(Head head, Tail... tail) {
cerr << head << ' ';
out(move(tail)...);
}
vv(ll, mem, 3001, 3001, LINF);
vec(ll, c, 3001);
ll dp(ll a, ll b) {
if (a > b)
return 0;
if (mem[a][b] != LINF)
return mem[a][b];
if (a > b)
return mem[a][b] = 0;
return mem[a][b] = max(c[a] - dp(a + 1, b), c[b] - dp(a, b - 1));
}
signed main() {
LL(n);
c.resize(n);
in(c);
out(dp(0, n - 1));
}
| replace | 180 | 181 | 180 | 181 | -6 | corrupted size vs. prev_size
|
p03171 | C++ | Time Limit Exceeded | #include <algorithm>
#include <stdio.h>
using namespace std;
const int MAXN = 3005;
int a[MAXN];
long long dp[MAXN][MAXN];
bool used[MAXN][MAXN];
int n;
long long solve(int l, int r) {
if (l > r)
return 0;
if (used[l][r])
return dp[l][r];
long long &ans = dp[l][r];
if ((r - l) % 2 == (n - 1) % 2) {
ans = max(a[l] + solve(l + 1, r), a[r] + solve(l, r - 1));
} else {
ans = min(-a[l] + solve(l + 1, r), -a[r] + solve(l, r - 1));
}
return ans;
}
int main(void) {
scanf(" %d", &n);
for (int i = 0; i < n; i++) {
scanf(" %d", &a[i]);
}
long long ans = solve(0, n - 1);
printf("%lld\n", ans);
return 0;
}
| #include <algorithm>
#include <stdio.h>
using namespace std;
const int MAXN = 3005;
int a[MAXN];
long long dp[MAXN][MAXN];
bool used[MAXN][MAXN];
int n;
long long solve(int l, int r) {
if (l > r)
return 0;
if (used[l][r])
return dp[l][r];
long long &ans = dp[l][r];
if ((r - l) % 2 == (n - 1) % 2) {
ans = max(a[l] + solve(l + 1, r), a[r] + solve(l, r - 1));
} else {
ans = min(-a[l] + solve(l + 1, r), -a[r] + solve(l, r - 1));
}
used[l][r] = true;
return ans;
}
int main(void) {
scanf(" %d", &n);
for (int i = 0; i < n; i++) {
scanf(" %d", &a[i]);
}
long long ans = solve(0, n - 1);
printf("%lld\n", ans);
return 0;
}
| insert | 23 | 23 | 23 | 24 | TLE | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define endl '\n'
const int mod = 1e9 + 7, u = 1e3 + 5;
int n, arr[u], dp[u][u];
int solve(int low, int high) {
if (dp[low][high] == -1) {
if (low == high) {
dp[low][high] = arr[low];
} else {
dp[low][high] = max(arr[low] - solve(low + 1, high),
arr[high] - solve(low, high - 1));
}
}
return dp[low][high];
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
dp[i][j] = -1;
}
}
cout << solve(1, n);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define endl '\n'
const int mod = 1e9 + 7, u = 3e3 + 5;
int n, arr[u], dp[u][u];
int solve(int low, int high) {
if (dp[low][high] == -1) {
if (low == high) {
dp[low][high] = arr[low];
} else {
dp[low][high] = max(arr[low] - solve(low + 1, high),
arr[high] - solve(low, high - 1));
}
}
return dp[low][high];
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
dp[i][j] = -1;
}
}
cout << solve(1, n);
return 0;
} | replace | 7 | 8 | 7 | 8 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long
using namespace std;
#define ll long long
#define dd double
#define fio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define mod (ll)(998244353)
#define sz(a) (ll) a.size()
#define all(a) a.begin(), a.end()
#define fr(i, a, b) for (ll i = a; i < b; i++)
#define frr(i, a, b) for (ll i = a - 1; i >= b; i--)
#define tc(t) \
ll t; \
cin >> t; \
while (t--)
#define pb emplace_back
#define ee emplace
#define rr return 0
#define mp make_pair
#define pr pair<ll, ll>
#define ff first
#define ss second
#define pie 3.1415926535
#define inf LLONG_MAX
ll mult(ll a, ll b, ll p = mod) { return ((a % p) * (b % p)) % p; }
ll add(ll a, ll b, ll p = mod) { return (a % p + b % p) % p; }
ll fpow(ll x, ll y) {
ll res = 1;
x = x % mod;
if (x == 0)
return 0;
while (y > 0) {
if (y & 1LL)
res = (res * x) % mod;
y = y >> 1LL;
x = (x * x) % mod;
}
return res;
}
ll inv(ll a, ll p = mod) { return fpow(a, p - 2); }
bool sa(const pair<ll, ll> &a, const pair<ll, ll> &b) {
return (a.second < b.second);
}
bool fd(const pair<ll, ll> &a, const pair<ll, ll> &b) {
return (a.first > b.first);
}
bool sd(const pair<ll, ll> &a, const pair<ll, ll> &b) {
return (a.second > b.second);
}
ll dx[4] = {0, 0, 1, -1};
ll dy[4] = {1, -1, 0, 0};
bool valid(ll x, ll y, ll n, ll m) {
if (x < 0 || y < 0) {
return false;
} else if (x >= n || y >= m) {
return false;
} else
return true;
}
#define MAXN 1000001
ll spf[MAXN];
void sieve() {
spf[1] = 1;
for (ll i = 2; i < MAXN; i++)
// marking smallest prime factor for every
// number to be itself.
spf[i] = i;
// separately marking spf for every even
// number as 2
for (ll i = 4; i < MAXN; i += 2)
spf[i] = 2;
for (ll i = 3; i * i < MAXN; i++) {
// checking if i is prime
if (spf[i] == i) {
// marking SPF for all numbers divisible by i
for (ll j = i * i; j < MAXN; j += i)
// marking spf[j] if it is not
// previously marked
if (spf[j] == j)
spf[j] = i;
}
}
}
ll dp[3001][3001][2];
ll solve(ll *a, ll i, ll j, ll turn) {
if (i > j)
return 0;
if (dp[i][j][turn] != -1)
return dp[i][j][turn];
if (turn == 1)
return dp[i][j][turn] =
max(a[i] + solve(a, i + 1, j, 0), a[j] + solve(a, i, j - 1, 0));
else
return dp[i][j][turn] = min(solve(a, i + 1, j, 1), solve(a, i, j - 1, 1));
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll n;
cin >> n;
ll a[n];
ll turn = 0, p1 = 0, p2 = 0;
ll start = 0, end = n - 1;
memset(dp, -1, sizeof dp);
ll sum = 0;
for (ll i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
ll scoreX = solve(a, 0, n - 1, 1);
ll scoreY = sum - scoreX;
cout << scoreX - scoreY;
return 0;
}
| #include <bits/stdc++.h>
#define ll long long
using namespace std;
#define ll long long
#define dd double
#define fio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define mod (ll)(998244353)
#define sz(a) (ll) a.size()
#define all(a) a.begin(), a.end()
#define fr(i, a, b) for (ll i = a; i < b; i++)
#define frr(i, a, b) for (ll i = a - 1; i >= b; i--)
#define tc(t) \
ll t; \
cin >> t; \
while (t--)
#define pb emplace_back
#define ee emplace
#define rr return 0
#define mp make_pair
#define pr pair<ll, ll>
#define ff first
#define ss second
#define pie 3.1415926535
#define inf LLONG_MAX
ll mult(ll a, ll b, ll p = mod) { return ((a % p) * (b % p)) % p; }
ll add(ll a, ll b, ll p = mod) { return (a % p + b % p) % p; }
ll fpow(ll x, ll y) {
ll res = 1;
x = x % mod;
if (x == 0)
return 0;
while (y > 0) {
if (y & 1LL)
res = (res * x) % mod;
y = y >> 1LL;
x = (x * x) % mod;
}
return res;
}
ll inv(ll a, ll p = mod) { return fpow(a, p - 2); }
bool sa(const pair<ll, ll> &a, const pair<ll, ll> &b) {
return (a.second < b.second);
}
bool fd(const pair<ll, ll> &a, const pair<ll, ll> &b) {
return (a.first > b.first);
}
bool sd(const pair<ll, ll> &a, const pair<ll, ll> &b) {
return (a.second > b.second);
}
ll dx[4] = {0, 0, 1, -1};
ll dy[4] = {1, -1, 0, 0};
bool valid(ll x, ll y, ll n, ll m) {
if (x < 0 || y < 0) {
return false;
} else if (x >= n || y >= m) {
return false;
} else
return true;
}
#define MAXN 1000001
ll spf[MAXN];
void sieve() {
spf[1] = 1;
for (ll i = 2; i < MAXN; i++)
// marking smallest prime factor for every
// number to be itself.
spf[i] = i;
// separately marking spf for every even
// number as 2
for (ll i = 4; i < MAXN; i += 2)
spf[i] = 2;
for (ll i = 3; i * i < MAXN; i++) {
// checking if i is prime
if (spf[i] == i) {
// marking SPF for all numbers divisible by i
for (ll j = i * i; j < MAXN; j += i)
// marking spf[j] if it is not
// previously marked
if (spf[j] == j)
spf[j] = i;
}
}
}
ll dp[3001][3001][2];
ll solve(ll *a, ll i, ll j, ll turn) {
if (i > j)
return 0;
if (dp[i][j][turn] != -1)
return dp[i][j][turn];
if (turn == 1)
return dp[i][j][turn] =
max(a[i] + solve(a, i + 1, j, 0), a[j] + solve(a, i, j - 1, 0));
else
return dp[i][j][turn] = min(solve(a, i + 1, j, 1), solve(a, i, j - 1, 1));
}
int main() {
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
ll n;
cin >> n;
ll a[n];
ll turn = 0, p1 = 0, p2 = 0;
ll start = 0, end = n - 1;
memset(dp, -1, sizeof dp);
ll sum = 0;
for (ll i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
ll scoreX = solve(a, 0, n - 1, 1);
ll scoreY = sum - scoreX;
cout << scoreX - scoreY;
return 0;
}
| replace | 104 | 108 | 104 | 108 | -11 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int N = 3e3 + 5;
ll a[N], dp[N][N];
int main() {
int n, i, j, L, R;
scanf("%d", &n);
for (i = 1; i <= n; i++)
scanf("%lld", a[i]);
for (L = n; i >= 1; L--) {
for (R = L; R <= n; R++) {
dp[L][R] = max(a[L] - dp[L + 1][R], a[R] - dp[L][R - 1]);
}
}
printf("%lld\n", dp[1][n]);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int N = 3e3 + 5;
ll a[N], dp[N][N];
int main() {
int n, i, j, L, R;
scanf("%d", &n);
for (i = 1; i <= n; i++)
scanf("%lld", &a[i]);
for (L = n; L >= 1; L--) {
for (R = L; R <= n; R++) {
dp[L][R] = max(a[L] - dp[L + 1][R], a[R] - dp[L][R - 1]);
}
}
printf("%lld\n", dp[1][n]);
return 0;
} | replace | 10 | 12 | 10 | 12 | -11 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define pb push_back
typedef long long ll;
typedef long double ld;
int main() {
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
ll N = 3e2 + 10, n, i, j;
cin >> n;
ll a[n], dp[N][N];
for (i = 0; i < n; i++) {
cin >> a[i];
}
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
dp[i][j] = 0;
}
}
for (i = n - 1; i >= 0; i--) {
for (j = i; j < n; j++) {
dp[i][j] = max(a[i] - dp[i + 1][j], a[j] - dp[i][j - 1]);
}
}
cout << dp[0][n - 1] << "\n";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define pb push_back
typedef long long ll;
typedef long double ld;
int main() {
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
ll N = 3e3 + 10, n, i, j;
cin >> n;
ll a[n], dp[N][N];
for (i = 0; i < n; i++) {
cin >> a[i];
}
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
dp[i][j] = 0;
}
}
for (i = n - 1; i >= 0; i--) {
for (j = i; j < n; j++) {
dp[i][j] = max(a[i] - dp[i + 1][j], a[j] - dp[i][j - 1]);
}
}
cout << dp[0][n - 1] << "\n";
return 0;
} | replace | 11 | 12 | 11 | 12 | 0 | |
p03171 | C++ | Runtime Error | #include <iostream>
using namespace std;
typedef long long ll;
ll dp[3001][3001] = {};
ll a[3001];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = n; i >= 1; i--) {
for (int j = i; j <= n; j++) {
int taken = n - i - j - 1;
if (taken % 2) {
// Jiro
dp[i][j] = min(dp[i + 1][j] - a[i], dp[i][j - 1] - a[j]);
} else {
// Taro
dp[i][j] = max(dp[i + 1][j] + a[i], dp[i][j - 1] + a[j]);
}
}
}
cout << dp[1][n] << endl;
return 0;
} | #include <iostream>
using namespace std;
typedef long long ll;
ll dp[3002][3002] = {};
ll a[3001];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = n; i >= 1; i--) {
for (int j = i; j <= n; j++) {
int taken = n - i - j - 1;
if (taken % 2) {
// Jiro
dp[i][j] = min(dp[i + 1][j] - a[i], dp[i][j - 1] - a[j]);
} else {
// Taro
dp[i][j] = max(dp[i + 1][j] + a[i], dp[i][j - 1] + a[j]);
}
}
}
cout << dp[1][n] << endl;
return 0;
} | replace | 4 | 5 | 4 | 5 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define ll long long int
#define off \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
ll dp[3000][3000];
int main() {
off;
int n;
cin >> n;
ll arr[n];
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
for (int i = 0; i <= n; ++i) {
dp[i][i] = arr[i];
}
for (int i = 1; i < n; ++i) {
for (int j = 0; j < n && j + i < n; ++j) {
dp[j][j + i] =
max(arr[j] - dp[j + 1][j + i], -dp[j][j + i - 1] + arr[j + i]);
}
}
cout << dp[0][n - 1];
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define ll long long int
#define off \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
ll dp[3000][3000];
int main() {
off;
int n;
cin >> n;
ll arr[n];
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
for (int i = 0; i < n; ++i) {
dp[i][i] = arr[i];
}
for (int i = 1; i < n; ++i) {
for (int j = 0; j < n && j + i < n; ++j) {
dp[j][j + i] =
max(arr[j] - dp[j + 1][j + i], -dp[j][j + i - 1] + arr[j + i]);
}
}
cout << dp[0][n - 1];
return 0;
}
| replace | 22 | 23 | 22 | 23 | 0 | |
p03171 | C++ | Runtime Error | // L - Deque https://atcoder.jp/contests/dp/tasks/dp_l
/* 入力例
4
10 80 90 30
// →出力 10
*/
#include <bits/stdc++.h>
using namespace std;
int N;
int a[4];
long long int mem[4][4];
int memflag[4][4];
long long int dp(int l, int r) {
if (memflag[l][r] == 1) {
return mem[l][r];
} else if (l == r) {
mem[l][r] = a[l];
memflag[l][r] = 1;
return mem[l][r];
} else {
mem[l][r] = max(dp(l + 1, r) * (-1) + a[l], dp(l, r - 1) * (-1) + a[r]);
memflag[l][r] = 1;
return mem[l][r];
}
}
int main(void) {
cin >> N;
for (int i = 0; i < N; i++) {
cin >> a[i];
for (int j = 0; j < N; j++) {
memflag[i][j] = 0;
}
}
cout << dp(0, N - 1);
cout << "";
return 0;
}
| // L - Deque https://atcoder.jp/contests/dp/tasks/dp_l
/* 入力例
4
10 80 90 30
// →出力 10
*/
#include <bits/stdc++.h>
using namespace std;
int N;
int a[3000];
long long int mem[3000][3000];
int memflag[3000][3000];
long long int dp(int l, int r) {
if (memflag[l][r] == 1) {
return mem[l][r];
} else if (l == r) {
mem[l][r] = a[l];
memflag[l][r] = 1;
return mem[l][r];
} else {
mem[l][r] = max(dp(l + 1, r) * (-1) + a[l], dp(l, r - 1) * (-1) + a[r]);
memflag[l][r] = 1;
return mem[l][r];
}
}
int main(void) {
cin >> N;
for (int i = 0; i < N; i++) {
cin >> a[i];
for (int j = 0; j < N; j++) {
memflag[i][j] = 0;
}
}
cout << dp(0, N - 1);
cout << "";
return 0;
}
| replace | 10 | 13 | 10 | 13 | 0 | |
p03171 | C++ | Time Limit Exceeded | #pragma warning(disable : 4996)
#pragma comment(linker, "/STACK:336777216")
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
#define int long long int
typedef complex<long double> cd;
const long double pi = acos(-1);
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<db, db> pdd;
typedef vector<int> vi;
typedef vector<vector<int>> matrix;
#define m1 make_pair
#define pb push_back
#define flush fflush(stdout)
#define IOS \
std::ios::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define reset(x, v) memset(x, v, sizeof(x))
#define fi first
#define se second
#define endl "\n"
#define debug(x) (cerr << #x << ": " << x << "\n")
#define setbits(x) __builtin_popcount(x)
#define setbitsll(x) __builtin_popcountll(x)
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
inline ll gcd(ll a, ll b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
inline ll power(ll a, ll n, ll m) {
if (n == 0)
return 1;
ll p = power(a, n / 2, m);
p = (p % m * p % m) % m;
if (n % 2)
return (p % m * a % m) % m;
else
return p;
}
const double EPS = 1e-9;
const ll MOD = 998244353;
const ll hell = 1000000007;
const int INF = 1e18;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
std::mt19937 rng(seed);
double startTime;
double getCurrentTime() {
return ((double)clock() - startTime) / CLOCKS_PER_SEC;
}
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
const int N = 100005;
int dp[3005][3005][2];
int n;
int a[3005];
int solve(int i, int j, int k) {
if (j < i)
return dp[i][j][k] = 0;
if (i == j) {
if (k)
return dp[i][j][k] = -a[j];
else
return dp[i][j][k] = a[j];
}
dp[i][j][k] = 0;
if (!k) {
dp[i][j][k] = max(a[i] + solve(i + 1, j, !k), a[j] + solve(i, j - 1, !k));
} else {
dp[i][j][k] = min(-a[i] + solve(i + 1, j, !k), -a[j] + solve(i, j - 1, !k));
}
return dp[i][j][k];
}
void solve() {
memset(dp, -1, sizeof(dp));
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
cout << solve(0, n - 1, 0) << endl;
}
int32_t main() {
IOS;
int t;
t = 1;
// cin>>t;
while (t--) {
solve();
}
return 0;
}
| #pragma warning(disable : 4996)
#pragma comment(linker, "/STACK:336777216")
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
#define int long long int
typedef complex<long double> cd;
const long double pi = acos(-1);
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<db, db> pdd;
typedef vector<int> vi;
typedef vector<vector<int>> matrix;
#define m1 make_pair
#define pb push_back
#define flush fflush(stdout)
#define IOS \
std::ios::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define reset(x, v) memset(x, v, sizeof(x))
#define fi first
#define se second
#define endl "\n"
#define debug(x) (cerr << #x << ": " << x << "\n")
#define setbits(x) __builtin_popcount(x)
#define setbitsll(x) __builtin_popcountll(x)
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
inline ll gcd(ll a, ll b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
inline ll power(ll a, ll n, ll m) {
if (n == 0)
return 1;
ll p = power(a, n / 2, m);
p = (p % m * p % m) % m;
if (n % 2)
return (p % m * a % m) % m;
else
return p;
}
const double EPS = 1e-9;
const ll MOD = 998244353;
const ll hell = 1000000007;
const int INF = 1e18;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
std::mt19937 rng(seed);
double startTime;
double getCurrentTime() {
return ((double)clock() - startTime) / CLOCKS_PER_SEC;
}
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
const int N = 100005;
int dp[3005][3005][2];
int n;
int a[3005];
int solve(int i, int j, int k) {
if (j < i)
return dp[i][j][k] = 0;
if (i == j) {
if (k)
return dp[i][j][k] = -a[j];
else
return dp[i][j][k] = a[j];
}
if (dp[i][j][k] != -1)
return dp[i][j][k];
dp[i][j][k] = 0;
if (!k) {
dp[i][j][k] = max(a[i] + solve(i + 1, j, !k), a[j] + solve(i, j - 1, !k));
} else {
dp[i][j][k] = min(-a[i] + solve(i + 1, j, !k), -a[j] + solve(i, j - 1, !k));
}
return dp[i][j][k];
}
void solve() {
memset(dp, -1, sizeof(dp));
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
cout << solve(0, n - 1, 0) << endl;
}
int32_t main() {
IOS;
int t;
t = 1;
// cin>>t;
while (t--) {
solve();
}
return 0;
}
| insert | 91 | 91 | 91 | 93 | TLE | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
#define pb push_back
#define endl '\n'
#define pll pair<int, int>
#define vll vector<int>
#define all(a) (a).begin(), (a).end()
#define x first
#define y second
#define hell 1000000007
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / gcd((a), (b))
#define int int64_t
#define ios \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, i, j, k, l, sum = 0, flag = 0, ans = 0, a[101], v[101], m;
int dp[3005][3005][2];
int is[3005][3005][2];
int f(int i, int j, int turn) {
if (i > j)
return 0;
if (i == j)
return (turn == 1 ? a[i] : -a[i]);
if (is[i][j][turn] != -1)
return dp[i][j][turn];
is[i][j][turn] = 1;
if (turn) {
return dp[i][j][turn] =
max(a[i] + f(i + 1, j, turn ^ 1), a[j] + f(i, j - 1, turn ^ 1));
} else {
return dp[i][j][turn] = min(-a[i] + f(i + 1, j, turn ^ 1),
-a[j] + f(i, j - 1, turn ^ 1));
}
}
void solve() {
memset(is, -1, sizeof dp);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
cout << f(0, n - 1, 1) << endl;
}
signed main() {
ios int test = 1;
// cin>>test;
while (test--) {
solve();
}
return 0;
} | #include <bits/stdc++.h>
#define pb push_back
#define endl '\n'
#define pll pair<int, int>
#define vll vector<int>
#define all(a) (a).begin(), (a).end()
#define x first
#define y second
#define hell 1000000007
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / gcd((a), (b))
#define int int64_t
#define ios \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, i, j, k, l, sum = 0, flag = 0, ans = 0, a[3001], v[101], m;
int dp[3005][3005][2];
int is[3005][3005][2];
int f(int i, int j, int turn) {
if (i > j)
return 0;
if (i == j)
return (turn == 1 ? a[i] : -a[i]);
if (is[i][j][turn] != -1)
return dp[i][j][turn];
is[i][j][turn] = 1;
if (turn) {
return dp[i][j][turn] =
max(a[i] + f(i + 1, j, turn ^ 1), a[j] + f(i, j - 1, turn ^ 1));
} else {
return dp[i][j][turn] = min(-a[i] + f(i + 1, j, turn ^ 1),
-a[j] + f(i, j - 1, turn ^ 1));
}
}
void solve() {
memset(is, -1, sizeof dp);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
cout << f(0, n - 1, 1) << endl;
}
signed main() {
ios int test = 1;
// cin>>test;
while (test--) {
solve();
}
return 0;
} | replace | 21 | 22 | 21 | 22 | -11 | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll N = 3090, OO = 1e9;
ll a[N], n, dp[N][N], sum;
ll Dp(int i, int j) {
if (i > j) {
return 0;
}
ll &ret = dp[i][j];
ret = max(a[i] - Dp(i + 1, j), a[j] - Dp(i, j - 1));
return ret;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i], sum += a[i];
}
memset(dp, -1, sizeof dp);
cout << Dp(0, n - 1);
return 0;
}
| #include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll N = 3090, OO = 1e9;
ll a[N], n, dp[N][N], sum;
ll Dp(int i, int j) {
if (i > j) {
return 0;
}
ll &ret = dp[i][j];
if (ret != -1)
return ret;
ret = 0;
ret = max(a[i] - Dp(i + 1, j), a[j] - Dp(i, j - 1));
return ret;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i], sum += a[i];
}
memset(dp, -1, sizeof dp);
cout << Dp(0, n - 1);
return 0;
}
| insert | 12 | 12 | 12 | 15 | TLE | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int N;
cin >> N;
vector<int> a(N);
for (int i = 0; i < N; i++) {
cin >> a[i];
}
vector<vector<ll>> dp(N + 1, vector<ll>(N + 1, 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) = max(dp[i,j-1)+a[i] , a[i]+dp[i+1, j))
dp[i][j] = max(dp[i][j - 1] + a[j - 1], a[i] + dp[i + 1][j]);
} else {
// dp[i,j) = min(dp[i,j-1)-a[i] , -a[i]+dp[i+1, j))
dp[i][j] = min(dp[i][j - 1] - a[j - 1], -a[i] + dp[i + 1][j]);
}
}
}
cout << dp[0][N] << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int N;
cin >> N;
vector<int> a(N);
for (int i = 0; i < N; i++) {
cin >> a[i];
}
vector<vector<ll>> dp(N + 1, vector<ll>(N + 1, 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) = max(dp[i,j-1)+a[i] , a[i]+dp[i+1, j))
dp[i][j] = max(dp[i][j - 1] + a[j - 1], a[i] + dp[i + 1][j]);
} else {
// dp[i,j) = min(dp[i,j-1)-a[i] , -a[i]+dp[i+1, j))
dp[i][j] = min(dp[i][j - 1] - a[j - 1], -a[i] + dp[i + 1][j]);
}
}
}
cout << dp[0][N] << endl;
return 0;
}
| replace | 14 | 15 | 14 | 15 | -11 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> isPair;
using vvll = vector<vector<ll>>;
using vll = vector<ll>;
#define Pll pair<ll, ll>
#define vpll vector<pll>
#define sll set<ll>
#define mll map<ll, ll>
const ll inf = 1e18;
const ll maxs = 1e5;
const ll mod = 1e9 + 7;
#define mp make_pair
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define ff first
#define ss second
#define LB lower_bound
#define UB upper_bound
#define line cout << endl;
#define fr(i, a, b) for (int i = a; i <= b; ++i)
#define all(n) n.begin(), n.end()
#define present(s, x) (s.find(x) != s.end())
#define cpresent(s, x) (find(all(s), x) != s.end())
#define tr(container, it) \
for (__typeof(container.begin()) it = container.begin(); \
it != container.end(); it++)
#define fastio() \
; \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define mst(x, a) memset(x, a, sizeof(x))
ll n;
ll a[1003];
ll dp[1003][1003];
ll solve(ll l, ll r, ll sum) {
if (dp[l][r] != -1)
return dp[l][r];
if (l == r)
return dp[l][r] = a[l];
if (l + 1 == r)
return dp[l + 1][r] = max(a[l], a[r]);
return dp[l][r] = max(sum - solve(l + 1, r, sum - a[l]),
sum - solve(l, r - 1, sum - a[r]));
}
int main() {
fastio();
ll t = 1;
// cin>>t;
while (t--) {
cin >> n;
ll sum = 0;
for (ll i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
memset(dp, -1, sizeof dp);
ll first = solve(0, n - 1, sum);
ll second = sum - first;
cout << first - second;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> isPair;
using vvll = vector<vector<ll>>;
using vll = vector<ll>;
#define Pll pair<ll, ll>
#define vpll vector<pll>
#define sll set<ll>
#define mll map<ll, ll>
const ll inf = 1e18;
const ll maxs = 1e5;
const ll mod = 1e9 + 7;
#define mp make_pair
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define ff first
#define ss second
#define LB lower_bound
#define UB upper_bound
#define line cout << endl;
#define fr(i, a, b) for (int i = a; i <= b; ++i)
#define all(n) n.begin(), n.end()
#define present(s, x) (s.find(x) != s.end())
#define cpresent(s, x) (find(all(s), x) != s.end())
#define tr(container, it) \
for (__typeof(container.begin()) it = container.begin(); \
it != container.end(); it++)
#define fastio() \
; \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define mst(x, a) memset(x, a, sizeof(x))
ll n;
ll a[3003];
ll dp[3003][3003];
ll solve(ll l, ll r, ll sum) {
if (dp[l][r] != -1)
return dp[l][r];
if (l == r)
return dp[l][r] = a[l];
if (l + 1 == r)
return dp[l + 1][r] = max(a[l], a[r]);
return dp[l][r] = max(sum - solve(l + 1, r, sum - a[l]),
sum - solve(l, r - 1, sum - a[r]));
}
int main() {
fastio();
ll t = 1;
// cin>>t;
while (t--) {
cin >> n;
ll sum = 0;
for (ll i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
memset(dp, -1, sizeof dp);
ll first = solve(0, n - 1, sum);
ll second = sum - first;
cout << first - second;
}
return 0;
} | replace | 45 | 47 | 45 | 47 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> isPair;
using vvll = vector<vector<ll>>;
using vll = vector<ll>;
#define Pll pair<ll, ll>
#define vpll vector<pll>
#define sll set<ll>
#define mll map<ll, ll>
const ll inf = 1e18;
const ll maxs = 100005;
const ll mod = 1e9 + 7;
#define mp make_pair
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define ff first
#define ss second
#define LB lower_bound
#define UB upper_bound
#define line cout << endl;
#define fr(i, a, b) for (int i = a; i <= b; ++i)
#define all(n) n.begin(), n.end()
#define present(s, x) (s.find(x) != s.end())
#define cpresent(s, x) (find(all(s), x) != s.end())
#define tr(container, it) \
for (__typeof(container.begin()) it = container.begin(); \
it != container.end(); it++)
#define fastio() \
; \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define mst(x, a) memset(x, a, sizeof(x))
ll n;
ll a[maxs];
isPair dp[1003][1003];
void solve() {
for (ll i = 0; i < n; i++)
dp[i][i] = mp(a[i], 0);
for (ll j = 1; j < n; j++) {
ll k = j;
for (ll i = 0; i < n - j; i++) {
dp[i][k].ff = max(a[i] + dp[i + 1][k].ss, a[k] + dp[i][k - 1].ss);
if (dp[i][k].ff == a[i] + dp[i + 1][k].ss)
dp[i][k].ss = dp[i + 1][k].ff;
else
dp[i][k].ss = dp[i][k - 1].ff;
k++;
}
}
/*for(ll i=0;i<n;i++)
{
for(ll j=0;j<n;j++)
cout<<"("<<dp[i][j].ff<<", "<<dp[i][j].ss<<")\t";
line;
}*/
cout << dp[0][n - 1].ff - dp[0][n - 1].ss;
}
int main() {
fastio();
ll t = 1;
// cin>>t;
while (t--) {
cin >> n;
for (ll i = 0; i < n; i++)
cin >> a[i];
solve();
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> isPair;
using vvll = vector<vector<ll>>;
using vll = vector<ll>;
#define Pll pair<ll, ll>
#define vpll vector<pll>
#define sll set<ll>
#define mll map<ll, ll>
const ll inf = 1e18;
const ll maxs = 100005;
const ll mod = 1e9 + 7;
#define mp make_pair
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define ff first
#define ss second
#define LB lower_bound
#define UB upper_bound
#define line cout << endl;
#define fr(i, a, b) for (int i = a; i <= b; ++i)
#define all(n) n.begin(), n.end()
#define present(s, x) (s.find(x) != s.end())
#define cpresent(s, x) (find(all(s), x) != s.end())
#define tr(container, it) \
for (__typeof(container.begin()) it = container.begin(); \
it != container.end(); it++)
#define fastio() \
; \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define mst(x, a) memset(x, a, sizeof(x))
ll n;
ll a[maxs];
isPair dp[3003][3003];
void solve() {
for (ll i = 0; i < n; i++)
dp[i][i] = mp(a[i], 0);
for (ll j = 1; j < n; j++) {
ll k = j;
for (ll i = 0; i < n - j; i++) {
dp[i][k].ff = max(a[i] + dp[i + 1][k].ss, a[k] + dp[i][k - 1].ss);
if (dp[i][k].ff == a[i] + dp[i + 1][k].ss)
dp[i][k].ss = dp[i + 1][k].ff;
else
dp[i][k].ss = dp[i][k - 1].ff;
k++;
}
}
/*for(ll i=0;i<n;i++)
{
for(ll j=0;j<n;j++)
cout<<"("<<dp[i][j].ff<<", "<<dp[i][j].ss<<")\t";
line;
}*/
cout << dp[0][n - 1].ff - dp[0][n - 1].ss;
}
int main() {
fastio();
ll t = 1;
// cin>>t;
while (t--) {
cin >> n;
for (ll i = 0; i < n; i++)
cin >> a[i];
solve();
}
return 0;
} | replace | 46 | 47 | 46 | 47 | 0 | |
p03171 | C++ | Runtime Error | // deque
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
const int mod = 1e9 + 7;
const int inf = (1 << 30);
string toBin(ll a) {
int i;
string s1 = "";
while (a > 0) {
if (a & 1) {
s1 = "1" + s1;
} else {
s1 = "0" + s1;
}
a >>= 1;
}
return s1;
}
ll dp[5000][5000];
ll fun(ll arr[], ll s, ll e, bool t) {
if (s > e) {
return 0;
}
if (dp[s][e] != -1) {
return dp[s][e];
}
ll c = 0;
if (t) {
ll a = arr[s] + fun(arr, s + 1, e, !t);
ll b = arr[e] + fun(arr, s, e - 1, !t);
c = max(a, b);
} else {
ll a = fun(arr, s + 1, e, !t);
ll b = fun(arr, s, e - 1, !t);
c = min(a, b);
}
return dp[s][e] = c;
}
void solve() {
ll i, n, sum = 0;
cin >> n;
ll arr[n];
for (i = 0; i < n; i++) {
cin >> arr[i];
sum += arr[i];
}
memset(dp, -1, sizeof(dp));
ll x = fun(arr, 0, n - 1, true);
sum = accumulate(arr, arr + n, 0LL);
cout << (2 * x - sum) << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("../input.txt", "r", stdin);
freopen("../output.txt", "w", stdout);
#endif
solve();
return 0;
} | // deque
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
const int mod = 1e9 + 7;
const int inf = (1 << 30);
string toBin(ll a) {
int i;
string s1 = "";
while (a > 0) {
if (a & 1) {
s1 = "1" + s1;
} else {
s1 = "0" + s1;
}
a >>= 1;
}
return s1;
}
ll dp[5000][5000];
ll fun(ll arr[], ll s, ll e, bool t) {
if (s > e) {
return 0;
}
if (dp[s][e] != -1) {
return dp[s][e];
}
ll c = 0;
if (t) {
ll a = arr[s] + fun(arr, s + 1, e, !t);
ll b = arr[e] + fun(arr, s, e - 1, !t);
c = max(a, b);
} else {
ll a = fun(arr, s + 1, e, !t);
ll b = fun(arr, s, e - 1, !t);
c = min(a, b);
}
return dp[s][e] = c;
}
void solve() {
ll i, n, sum = 0;
cin >> n;
ll arr[n];
for (i = 0; i < n; i++) {
cin >> arr[i];
sum += arr[i];
}
memset(dp, -1, sizeof(dp));
ll x = fun(arr, 0, n - 1, true);
sum = accumulate(arr, arr + n, 0LL);
cout << (2 * x - sum) << endl;
}
int main() {
solve();
return 0;
} | delete | 56 | 62 | 56 | 56 | -11 | |
p03171 | C++ | Runtime Error | #define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define FOR(i, n) for (int i = 0; i < (n); i++)
#define sz(c) ((int)c.size())
#define ten(n) ((int)1e##n)
using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
template <typename...> static inline int getchar_unlocked(void) {
return getchar();
}
template <typename...> static inline void putchar_unlocked(int c) {
putchar(c);
}
#define mygc(c) (c) = getchar_unlocked()
#define mypc(c) putchar_unlocked(c)
void reader(int &x) {
int k, m = 0;
x = 0;
for (;;) {
mygc(k);
if (k == '-') {
m = 1;
break;
}
if ('0' <= k && k <= '9') {
x = k - '0';
break;
}
}
for (;;) {
mygc(k);
if (k < '0' || k > '9')
break;
x = x * 10 + k - '0';
}
if (m)
x = -x;
}
void reader(ll &x) {
int k, m = 0;
x = 0;
for (;;) {
mygc(k);
if (k == '-') {
m = 1;
break;
}
if ('0' <= k && k <= '9') {
x = k - '0';
break;
}
}
for (;;) {
mygc(k);
if (k < '0' || k > '9')
break;
x = x * 10 + k - '0';
}
if (m)
x = -x;
}
int reader(char c[]) {
int i, s = 0;
for (;;) {
mygc(i);
if (i != ' ' && i != '\n' && i != '\r' && i != '\t' && i != EOF)
break;
}
c[s++] = i;
for (;;) {
mygc(i);
if (i == ' ' || i == '\n' || i == '\r' || i == '\t' || i == EOF)
break;
c[s++] = i;
}
c[s] = '\0';
return s;
}
int reader(string &c) {
int i;
for (;;) {
mygc(i);
if (i != ' ' && i != '\n' && i != '\r' && i != '\t' && i != EOF)
break;
}
c.push_back(i);
for (;;) {
mygc(i);
if (i == ' ' || i == '\n' || i == '\r' || i == '\t' || i == EOF)
break;
c.push_back(i);
};
return sz(c);
}
template <class T, class S> void reader(T &x, S &y) {
reader(x);
reader(y);
}
template <class T, class S, class U> void reader(T &x, S &y, U &z) {
reader(x);
reader(y);
reader(z);
}
template <class T, class S, class U, class V>
void reader(T &x, S &y, U &z, V &w) {
reader(x);
reader(y);
reader(z);
reader(w);
}
void writer(int x, char c) {
int s = 0, m = 0;
char f[10];
if (x < 0)
m = 1, x = -x;
while (x)
f[s++] = x % 10, x /= 10;
if (!s)
f[s++] = 0;
if (m)
mypc('-');
while (s--)
mypc(f[s] + '0');
mypc(c);
}
void writer(ll x, char c) {
int s = 0, m = 0;
char f[20];
if (x < 0)
m = 1, x = -x;
while (x)
f[s++] = x % 10, x /= 10;
if (!s)
f[s++] = 0;
if (m)
mypc('-');
while (s--)
mypc(f[s] + '0');
mypc(c);
}
void writer(const char c[]) {
int i;
for (i = 0; c[i] != '\0'; i++)
mypc(c[i]);
}
void writer(const string &x, char c) {
int i;
for (i = 0; x[i] != '\0'; i++)
mypc(x[i]);
mypc(c);
}
void writer(const char x[], char c) {
int i;
for (i = 0; x[i] != '\0'; i++)
mypc(x[i]);
mypc(c);
}
template <class T> void writerLn(T x) { writer(x, '\n'); }
template <class T, class S> void writerLn(T x, S y) {
writer(x, ' ');
writer(y, '\n');
}
template <class T, class S, class U> void writerLn(T x, S y, U z) {
writer(x, ' ');
writer(y, ' ');
writer(z, '\n');
}
template <class T, class S, class U, class V>
void writerLn(T x, S y, U z, V v) {
writer(x, ' ');
writer(y, ' ');
writer(z, ' ');
writer(v, '\n');
}
template <class T> void writerArr(T x[], int n) {
if (!n) {
mypc('\n');
return;
}
FOR(i, n - 1) writer(x[i], ' ');
writer(x[n - 1], '\n');
}
template <class T> void writerArr(vector<T> &x) {
writerArr(x.data(), (int)x.size());
}
template <class T> void chmin(T &a, const T &b) {
if (a > b)
a = b;
}
template <class T> void chmax(T &a, const T &b) {
if (a < b)
a = b;
}
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
ll mod_pow(ll a, ll n, ll mod) {
ll ret = 1;
ll p = a % mod;
while (n) {
if (n & 1)
ret = ret * p % mod;
p = p * p % mod;
n >>= 1;
}
return ret;
}
template <class T> T extgcd(T a, T b, T &x, T &y) {
for (T u = y = 1, v = x = 0; a;) {
T q = b / a;
swap(x -= q * u, u);
swap(y -= q * v, v);
swap(b -= q * a, a);
}
return b;
}
ll mod_inv(ll a, ll m) {
ll x, y;
extgcd<ll>(a, m, x, y);
return (m + x % m) % m;
}
#ifdef _DEBUG
template <typename... Args>
void debugPrintf(const char *format, Args const &...args) {
fprintf(stdout, format, args...);
fflush(stdout);
}
#else
#define debugPrintf(...)
#endif
ll a[100];
ll memo[3001][3001][2];
ll dfs(int l, int r, bool f) {
ll &ret = memo[l][r][f];
if (ret != 0x8f8f8f8f8f8f8f8f)
return ret;
if (l == r)
return ret = 0;
if (f) {
ll l1 = dfs(l + 1, r, !f) + a[l];
ll l2 = dfs(l, r - 1, !f) + a[r - 1];
return ret = max(l1, l2);
} else {
ll l1 = dfs(l + 1, r, !f) - a[l];
ll l2 = dfs(l, r - 1, !f) - a[r - 1];
return ret = min(l1, l2);
}
}
int main() {
int n;
reader(n);
FOR(i, n) reader(a[i]);
memset(memo, 0x8f, sizeof(memo));
ll ans = dfs(0, n, true);
writerLn(ans);
return 0;
}
| #define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define FOR(i, n) for (int i = 0; i < (n); i++)
#define sz(c) ((int)c.size())
#define ten(n) ((int)1e##n)
using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
template <typename...> static inline int getchar_unlocked(void) {
return getchar();
}
template <typename...> static inline void putchar_unlocked(int c) {
putchar(c);
}
#define mygc(c) (c) = getchar_unlocked()
#define mypc(c) putchar_unlocked(c)
void reader(int &x) {
int k, m = 0;
x = 0;
for (;;) {
mygc(k);
if (k == '-') {
m = 1;
break;
}
if ('0' <= k && k <= '9') {
x = k - '0';
break;
}
}
for (;;) {
mygc(k);
if (k < '0' || k > '9')
break;
x = x * 10 + k - '0';
}
if (m)
x = -x;
}
void reader(ll &x) {
int k, m = 0;
x = 0;
for (;;) {
mygc(k);
if (k == '-') {
m = 1;
break;
}
if ('0' <= k && k <= '9') {
x = k - '0';
break;
}
}
for (;;) {
mygc(k);
if (k < '0' || k > '9')
break;
x = x * 10 + k - '0';
}
if (m)
x = -x;
}
int reader(char c[]) {
int i, s = 0;
for (;;) {
mygc(i);
if (i != ' ' && i != '\n' && i != '\r' && i != '\t' && i != EOF)
break;
}
c[s++] = i;
for (;;) {
mygc(i);
if (i == ' ' || i == '\n' || i == '\r' || i == '\t' || i == EOF)
break;
c[s++] = i;
}
c[s] = '\0';
return s;
}
int reader(string &c) {
int i;
for (;;) {
mygc(i);
if (i != ' ' && i != '\n' && i != '\r' && i != '\t' && i != EOF)
break;
}
c.push_back(i);
for (;;) {
mygc(i);
if (i == ' ' || i == '\n' || i == '\r' || i == '\t' || i == EOF)
break;
c.push_back(i);
};
return sz(c);
}
template <class T, class S> void reader(T &x, S &y) {
reader(x);
reader(y);
}
template <class T, class S, class U> void reader(T &x, S &y, U &z) {
reader(x);
reader(y);
reader(z);
}
template <class T, class S, class U, class V>
void reader(T &x, S &y, U &z, V &w) {
reader(x);
reader(y);
reader(z);
reader(w);
}
void writer(int x, char c) {
int s = 0, m = 0;
char f[10];
if (x < 0)
m = 1, x = -x;
while (x)
f[s++] = x % 10, x /= 10;
if (!s)
f[s++] = 0;
if (m)
mypc('-');
while (s--)
mypc(f[s] + '0');
mypc(c);
}
void writer(ll x, char c) {
int s = 0, m = 0;
char f[20];
if (x < 0)
m = 1, x = -x;
while (x)
f[s++] = x % 10, x /= 10;
if (!s)
f[s++] = 0;
if (m)
mypc('-');
while (s--)
mypc(f[s] + '0');
mypc(c);
}
void writer(const char c[]) {
int i;
for (i = 0; c[i] != '\0'; i++)
mypc(c[i]);
}
void writer(const string &x, char c) {
int i;
for (i = 0; x[i] != '\0'; i++)
mypc(x[i]);
mypc(c);
}
void writer(const char x[], char c) {
int i;
for (i = 0; x[i] != '\0'; i++)
mypc(x[i]);
mypc(c);
}
template <class T> void writerLn(T x) { writer(x, '\n'); }
template <class T, class S> void writerLn(T x, S y) {
writer(x, ' ');
writer(y, '\n');
}
template <class T, class S, class U> void writerLn(T x, S y, U z) {
writer(x, ' ');
writer(y, ' ');
writer(z, '\n');
}
template <class T, class S, class U, class V>
void writerLn(T x, S y, U z, V v) {
writer(x, ' ');
writer(y, ' ');
writer(z, ' ');
writer(v, '\n');
}
template <class T> void writerArr(T x[], int n) {
if (!n) {
mypc('\n');
return;
}
FOR(i, n - 1) writer(x[i], ' ');
writer(x[n - 1], '\n');
}
template <class T> void writerArr(vector<T> &x) {
writerArr(x.data(), (int)x.size());
}
template <class T> void chmin(T &a, const T &b) {
if (a > b)
a = b;
}
template <class T> void chmax(T &a, const T &b) {
if (a < b)
a = b;
}
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
ll mod_pow(ll a, ll n, ll mod) {
ll ret = 1;
ll p = a % mod;
while (n) {
if (n & 1)
ret = ret * p % mod;
p = p * p % mod;
n >>= 1;
}
return ret;
}
template <class T> T extgcd(T a, T b, T &x, T &y) {
for (T u = y = 1, v = x = 0; a;) {
T q = b / a;
swap(x -= q * u, u);
swap(y -= q * v, v);
swap(b -= q * a, a);
}
return b;
}
ll mod_inv(ll a, ll m) {
ll x, y;
extgcd<ll>(a, m, x, y);
return (m + x % m) % m;
}
#ifdef _DEBUG
template <typename... Args>
void debugPrintf(const char *format, Args const &...args) {
fprintf(stdout, format, args...);
fflush(stdout);
}
#else
#define debugPrintf(...)
#endif
ll a[3000];
ll memo[3001][3001][2];
ll dfs(int l, int r, bool f) {
ll &ret = memo[l][r][f];
if (ret != 0x8f8f8f8f8f8f8f8f)
return ret;
if (l == r)
return ret = 0;
if (f) {
ll l1 = dfs(l + 1, r, !f) + a[l];
ll l2 = dfs(l, r - 1, !f) + a[r - 1];
return ret = max(l1, l2);
} else {
ll l1 = dfs(l + 1, r, !f) - a[l];
ll l2 = dfs(l, r - 1, !f) - a[r - 1];
return ret = min(l1, l2);
}
}
int main() {
int n;
reader(n);
FOR(i, n) reader(a[i]);
memset(memo, 0x8f, sizeof(memo));
ll ans = dfs(0, n, true);
writerLn(ans);
return 0;
}
| replace | 260 | 261 | 260 | 261 | -11 | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
long long f(vector<long long> &v, int i, int j, vector<vector<long long>> dp) {
if (i == j)
return v[i];
if (dp[i][j] != -1)
return dp[i][j];
dp[i][j] = max(v[i] - f(v, i + 1, j, dp), v[j] - f(v, i, j - 1, dp));
return dp[i][j];
}
int main() {
int N;
cin >> N;
vector<long long> v(N, 0);
for (int i = 0; i < N; i++) {
cin >> v[i];
}
vector<vector<long long>> dp(N, vector<long long>(N, -1));
cout << f(v, 0, N - 1, dp) << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
long long f(vector<long long> &v, int i, int j, vector<vector<long long>> &dp) {
if (i == j)
return v[i];
if (dp[i][j] != -1)
return dp[i][j];
dp[i][j] = max(v[i] - f(v, i + 1, j, dp), v[j] - f(v, i, j - 1, dp));
return dp[i][j];
}
int main() {
int N;
cin >> N;
vector<long long> v(N, 0);
for (int i = 0; i < N; i++) {
cin >> v[i];
}
vector<vector<long long>> dp(N, vector<long long>(N, -1));
cout << f(v, 0, N - 1, dp) << endl;
return 0;
} | replace | 3 | 4 | 3 | 4 | TLE | |
p03171 | C++ | Runtime Error | //============================================================================
// Name : ProjG.cpp
// Author : Yong Kiat
// Version :
// Copyright : By Algotae.
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <bits/stdc++.h>
using namespace std;
const int mxN = 3000;
int n, a[mxN];
long long dp[mxN][mxN];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
for (int j = i - 1; j >= 0; j--) {
dp[i][j] = max(a[i - 1] - dp[i - 1][j], a[j] - dp[i][j + 1]);
}
}
cout << dp[n][0];
}
| //============================================================================
// Name : ProjG.cpp
// Author : Yong Kiat
// Version :
// Copyright : By Algotae.
// Description : Hello World in C++, Ansi-style
//============================================================================
#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_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
for (int j = i - 1; j >= 0; j--) {
dp[i][j] = max(a[i - 1] - dp[i - 1][j], a[j] - dp[i][j + 1]);
}
}
cout << dp[n][0];
}
| replace | 14 | 15 | 14 | 15 | 0 | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll dp[3000][3000];
bool dpv[3000][3000];
vector<int> deq(3000);
ll solve(int l, int r, int flag) { // flag = 1 -> Taro
if (dpv[l][r])
return dp[l][r];
if (l > r)
return 0;
if (l == r) {
dpv[l][r] = true;
if (flag)
return dp[l][r] = deq[l];
else
return dp[l][r] = -deq[l];
}
if (flag) {
return dp[l][r] = max((ll)deq[l] + solve(l + 1, r, 1 - flag),
(ll)deq[r] + solve(l, r - 1, 1 - flag));
} else {
return dp[l][r] = min(solve(l + 1, r, 1 - flag) - (ll)deq[l],
solve(l, r - 1, 1 - flag) - (ll)deq[r]);
}
}
int main() {
int n, tp;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> deq[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
dpv[i][j] = false;
}
}
cout << solve(0, n - 1, 1) << "\n";
} | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll dp[3000][3000];
bool dpv[3000][3000];
vector<int> deq(3000);
ll solve(int l, int r, int flag) { // flag = 1 -> Taro
if (dpv[l][r])
return dp[l][r];
if (l > r)
return 0;
if (l == r) {
dpv[l][r] = true;
if (flag)
return dp[l][r] = deq[l];
else
return dp[l][r] = -deq[l];
}
dpv[l][r] = true;
if (flag) {
return dp[l][r] = max((ll)deq[l] + solve(l + 1, r, 1 - flag),
(ll)deq[r] + solve(l, r - 1, 1 - flag));
} else {
return dp[l][r] = min(solve(l + 1, r, 1 - flag) - (ll)deq[l],
solve(l, r - 1, 1 - flag) - (ll)deq[r]);
}
}
int main() {
int n, tp;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> deq[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
dpv[i][j] = false;
}
}
cout << solve(0, n - 1, 1) << "\n";
} | insert | 22 | 22 | 22 | 23 | TLE | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define f first
#define s second
#define mp make_pair
#define sz size()
#define ll long long
int n;
bool was[3010][3010];
ll dp[3010][3010];
int a[3010];
ll calc(int l, int r) {
if (l > r)
return 0;
if (l == r)
return a[r];
if (was[l][r])
return dp[l][r];
dp[l][r] = max(a[l] - calc(l + 1, r),
a[r] - calc(l, r - 1)); // max(áåð¸ì ïåðâûé, áåð¸ì ïîñë.)
// cout << "podotrezok " << l << ' ' << r << " answer is " << dp[l][r] <<
//'\n';
return dp[l][r];
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
scanf("%lld", a + i);
ll ans = calc(1, n);
printf("%lld", ans);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define f first
#define s second
#define mp make_pair
#define sz size()
#define ll long long
int n;
bool was[3010][3010];
ll dp[3010][3010];
int a[3010];
ll calc(int l, int r) {
if (l > r)
return 0;
if (l == r)
return a[r];
if (was[l][r])
return dp[l][r];
was[l][r] = 1;
dp[l][r] = max(a[l] - calc(l + 1, r),
a[r] - calc(l, r - 1)); // max(áåð¸ì ïåðâûé, áåð¸ì ïîñë.)
// cout << "podotrezok " << l << ' ' << r << " answer is " << dp[l][r] <<
//'\n';
return dp[l][r];
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
scanf("%lld", a + i);
ll ans = calc(1, n);
printf("%lld", ans);
return 0;
} | insert | 22 | 22 | 22 | 23 | TLE | |
p03171 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef pair<ll, ll> P;
typedef vector<P> vpl;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1 << 30;
const ll linf = 1LL << 62;
const int MAX = 2100000;
ll dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const double pi = acos(-1);
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
if (a > b)
a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
if (a < b)
a = b;
}
template <typename T1, typename T2> inline void print2(T1 a, T2 b) {
cout << a << " " << b << endl;
}
template <typename T1, typename T2, typename T3>
inline void print3(T1 a, T2 b, T3 c) {
cout << a << " " << b << " " << c << endl;
}
const int mod = 1e9 + 7;
ll n;
vl a(3005);
ll dp[3005][3005];
bool calc[3005][3005];
ll rec(ll l, ll r) {
if (l == r)
return a[l];
if (calc[l][r])
return dp[l][r];
dp[l][r] = max(a[l] - rec(l + 1, r), a[r] - rec(l, r - 1));
return dp[l][r];
}
int main() {
cin >> n;
rep(i, n) cin >> a[i];
cout << rec(0, n - 1) << endl;
} | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef pair<ll, ll> P;
typedef vector<P> vpl;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1 << 30;
const ll linf = 1LL << 62;
const int MAX = 2100000;
ll dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const double pi = acos(-1);
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
if (a > b)
a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
if (a < b)
a = b;
}
template <typename T1, typename T2> inline void print2(T1 a, T2 b) {
cout << a << " " << b << endl;
}
template <typename T1, typename T2, typename T3>
inline void print3(T1 a, T2 b, T3 c) {
cout << a << " " << b << " " << c << endl;
}
const int mod = 1e9 + 7;
ll n;
vl a(3005);
ll dp[3005][3005];
bool calc[3005][3005];
ll rec(ll l, ll r) {
if (l == r)
return a[l];
if (calc[l][r])
return dp[l][r];
dp[l][r] = max(a[l] - rec(l + 1, r), a[r] - rec(l, r - 1));
calc[l][r] = true;
return dp[l][r];
}
int main() {
cin >> n;
rep(i, n) cin >> a[i];
cout << rec(0, n - 1) << endl;
} | insert | 59 | 59 | 59 | 60 | TLE | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define int long long
using namespace std;
int n;
vector<int> vec;
int dp[3000][3000];
int solve(int i, int j) {
if (i == j) {
return vec[i];
}
if (dp[i][j] != -1)
return dp[i][j];
int ans = max(vec[i] - solve(i + 1, j), vec[j] - solve(i, j - 1));
return ans;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
vec.resize(n);
for (int i = 0; i < n; i++) {
cin >> vec[i];
}
for (int i = 0; i < 3000; i++) {
for (int j = 0; j < 3000; j++) {
dp[i][j] = -1;
}
}
cout << solve(0, n - 1);
return 0;
}
| #include <bits/stdc++.h>
#define int long long
using namespace std;
int n;
vector<int> vec;
int dp[3000][3000];
int solve(int i, int j) {
if (i == j) {
return vec[i];
}
if (dp[i][j] != -1)
return dp[i][j];
int ans = max(vec[i] - solve(i + 1, j), vec[j] - solve(i, j - 1));
return dp[i][j] = ans;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
vec.resize(n);
for (int i = 0; i < n; i++) {
cin >> vec[i];
}
for (int i = 0; i < 3000; i++) {
for (int j = 0; j < 3000; j++) {
dp[i][j] = -1;
}
}
cout << solve(0, n - 1);
return 0;
}
| replace | 16 | 17 | 16 | 17 | TLE | |
p03171 | C++ | Runtime Error | // #include<bits/stdc++.h>
// using namespace std;
// typedef vector <int> vi;
// typedef pair< int ,int > pii;
// #define all(v) v.begin(),v.end()
// #define PB push_back
// #define T(val) while(val--)
// #define FIN for(i=0;i<n;i++)
// #define FIS for(i=0;i<s.length();i++)
// #define AIN for(i=0;i<n;i++){cin>>a[i];}
// #define F(i,k,n) for(i=k;i<n;i++)
// #define FG(i,k,n,x) for(i=k;i<n;i+=x)
// #define YES cout<<"YES\n";
// #define NO cout<<"NO\n";
// #define ll long long
// #define MOD 1000000007
// #define deb(x) cout << #x << "=" << x << endl;
// #define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl;
// #define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
// //sort(arr, arr+n, greater<int>()); For sorting in reverse order
// double power(double A,ll n){
// double p=1;
// ll k=1;
// while(k<=n){
// if(n&k)
// p=p*A;
// k=k*2;A=A*A;
// }
// return p;
// }
// int solve(){
// ll n;
// cin>>n;
// double p;
// cin>>p;
// cout << fixed << setprecision(12) ;
// cout<<power((1-p),n)<<"\n";
// return 0;
// }
// int main(){
// fast
// int t=1;
// //cin>>t;
// T(t){
// solve();
// }
// return 0;
// }
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define all(v) v.begin(), v.end()
#define PB push_back
#define T(val) while (val--)
#define FIN for (i = 0; i < n; i++)
#define FIS for (i = 0; i < s.length(); i++)
#define AIN \
for (i = 0; i < n; i++) { \
cin >> a[i]; \
}
#define F(i, k, n) for (i = k; i < n; i++)
#define FG(i, k, n, x) for (i = k; i < n; i += x)
#define YES cout << "YES\n";
#define NO cout << "NO\n";
#define ll long long
#define MOD 1000000007
#define deb(x) cout << #x << "=" << x << endl;
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl;
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
// sort(arr, arr+n, greater<int>()); For sorting in reverse order
int solve() {
ll n;
cin >> n;
ll a[n], i, tot = 0;
FIN {
cin >> a[i];
tot += a[i];
}
ll dp[n + 1][n + 1][2];
// dp[i][i][0]=a[i];||dp[i][i][1]=0;
for (i = 0; i < n; i++) {
for (ll j = 0; j < n; j++) {
if (i == 0) {
dp[j][j][0] = a[j];
dp[j][j][1] = 0;
continue;
}
dp[j][j + i][0] =
max(a[j] + dp[j + 1][j + i][1], a[j + i] + dp[j][j + i - 1][1]);
dp[j][j + i][1] = min(dp[j + 1][j + i][0], dp[j][j + i - 1][0]);
}
}
ll x = dp[0][n - 1][0];
cout << 2 * x - tot << "\n";
return 0;
}
int main() {
fast int t = 1;
// cin>>t;
T(t) { solve(); }
return 0;
} | // #include<bits/stdc++.h>
// using namespace std;
// typedef vector <int> vi;
// typedef pair< int ,int > pii;
// #define all(v) v.begin(),v.end()
// #define PB push_back
// #define T(val) while(val--)
// #define FIN for(i=0;i<n;i++)
// #define FIS for(i=0;i<s.length();i++)
// #define AIN for(i=0;i<n;i++){cin>>a[i];}
// #define F(i,k,n) for(i=k;i<n;i++)
// #define FG(i,k,n,x) for(i=k;i<n;i+=x)
// #define YES cout<<"YES\n";
// #define NO cout<<"NO\n";
// #define ll long long
// #define MOD 1000000007
// #define deb(x) cout << #x << "=" << x << endl;
// #define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl;
// #define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
// //sort(arr, arr+n, greater<int>()); For sorting in reverse order
// double power(double A,ll n){
// double p=1;
// ll k=1;
// while(k<=n){
// if(n&k)
// p=p*A;
// k=k*2;A=A*A;
// }
// return p;
// }
// int solve(){
// ll n;
// cin>>n;
// double p;
// cin>>p;
// cout << fixed << setprecision(12) ;
// cout<<power((1-p),n)<<"\n";
// return 0;
// }
// int main(){
// fast
// int t=1;
// //cin>>t;
// T(t){
// solve();
// }
// return 0;
// }
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define all(v) v.begin(), v.end()
#define PB push_back
#define T(val) while (val--)
#define FIN for (i = 0; i < n; i++)
#define FIS for (i = 0; i < s.length(); i++)
#define AIN \
for (i = 0; i < n; i++) { \
cin >> a[i]; \
}
#define F(i, k, n) for (i = k; i < n; i++)
#define FG(i, k, n, x) for (i = k; i < n; i += x)
#define YES cout << "YES\n";
#define NO cout << "NO\n";
#define ll long long
#define MOD 1000000007
#define deb(x) cout << #x << "=" << x << endl;
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl;
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
// sort(arr, arr+n, greater<int>()); For sorting in reverse order
int solve() {
ll n;
cin >> n;
ll a[n], i, tot = 0;
FIN {
cin >> a[i];
tot += a[i];
}
ll dp[n + 1][n + 1][2];
// dp[i][i][0]=a[i];||dp[i][i][1]=0;
for (i = 0; i < n; i++) {
for (ll j = 0; j + i < n; j++) {
if (i == 0) {
dp[j][j][0] = a[j];
dp[j][j][1] = 0;
continue;
}
dp[j][j + i][0] =
max(a[j] + dp[j + 1][j + i][1], a[j + i] + dp[j][j + i - 1][1]);
dp[j][j + i][1] = min(dp[j + 1][j + i][0], dp[j][j + i - 1][0]);
}
}
ll x = dp[0][n - 1][0];
cout << 2 * x - tot << "\n";
return 0;
}
int main() {
fast int t = 1;
// cin>>t;
T(t) { solve(); }
return 0;
} | replace | 88 | 89 | 88 | 89 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 3002;
ll dp[MAXN][MAXN][2];
bool vis[MAXN][MAXN];
int A[MAXN], N;
// ll solve(int i, int j, int p, int diff) {
// if (j == i)
// { if (p == 1)
// return diff + A[i];
// else return diff - A[i];
// }
// //if (j < i) return diff;
// if (dp[i][j][p][diff] != -1)return dp[i][j][p][diff];
// int X = 0, Y = 0;
// if (p == 1)
// {
// X = solve(i, j - 1, p ^ 1, diff + A[j]);
// Y = solve(i + 1, j, p ^ 1, diff + A[i]);
// } else {
// X = solve(i, j - 1, p ^ 1, diff - A[j]);
// Y = solve(i + 1, j, p ^ 1, diff - A[i]);
// }
// if (p == 1)
// dp[i][j][p][diff] = max(X, Y);
// else dp[i][j][p][diff] = min(X, Y);
// if (p == 1)
// return max(X, Y);
// else return min(X, Y);
// }
ll solve(int i, int j, int p) {
if (j == i) {
if (p == 1)
return A[i];
else
return -A[i];
}
// if (j < i) return diff;
if (dp[i][j][p] != -1)
return dp[i][j][p];
if (p == 1) {
dp[i][j][p] =
max(A[j] + solve(i, j - 1, p ^ 1), A[i] + solve(i + 1, j, p ^ 1));
} else {
dp[i][j][p] =
min(-A[j] + solve(i, j - 1, p ^ 1), -A[i] + solve(i + 1, j, p ^ 1));
}
// if (p == 1)
// dp[i][j][p][diff] = max(X, Y);
// else dp[i][j][p][diff] = min(X, Y);
// if (p == 1)
// return max(X, Y);
// else return min(X, Y);
return dp[i][j][p];
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
memset(dp, -1, sizeof(dp));
cin >> N;
for (int i = 1; i <= N; i++) {
cin >> A[i];
}
cout << solve(1, N, 1);
// cout << abs(X - Y) << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 3002;
ll dp[MAXN][MAXN][2];
bool vis[MAXN][MAXN];
int A[MAXN], N;
// ll solve(int i, int j, int p, int diff) {
// if (j == i)
// { if (p == 1)
// return diff + A[i];
// else return diff - A[i];
// }
// //if (j < i) return diff;
// if (dp[i][j][p][diff] != -1)return dp[i][j][p][diff];
// int X = 0, Y = 0;
// if (p == 1)
// {
// X = solve(i, j - 1, p ^ 1, diff + A[j]);
// Y = solve(i + 1, j, p ^ 1, diff + A[i]);
// } else {
// X = solve(i, j - 1, p ^ 1, diff - A[j]);
// Y = solve(i + 1, j, p ^ 1, diff - A[i]);
// }
// if (p == 1)
// dp[i][j][p][diff] = max(X, Y);
// else dp[i][j][p][diff] = min(X, Y);
// if (p == 1)
// return max(X, Y);
// else return min(X, Y);
// }
ll solve(int i, int j, int p) {
if (j == i) {
if (p == 1)
return A[i];
else
return -A[i];
}
// if (j < i) return diff;
if (dp[i][j][p] != -1)
return dp[i][j][p];
if (p == 1) {
dp[i][j][p] =
max(A[j] + solve(i, j - 1, p ^ 1), A[i] + solve(i + 1, j, p ^ 1));
} else {
dp[i][j][p] =
min(-A[j] + solve(i, j - 1, p ^ 1), -A[i] + solve(i + 1, j, p ^ 1));
}
// if (p == 1)
// dp[i][j][p][diff] = max(X, Y);
// else dp[i][j][p][diff] = min(X, Y);
// if (p == 1)
// return max(X, Y);
// else return min(X, Y);
return dp[i][j][p];
}
int main() {
memset(dp, -1, sizeof(dp));
cin >> N;
for (int i = 1; i <= N; i++) {
cin >> A[i];
}
cout << solve(1, N, 1);
// cout << abs(X - Y) << endl;
return 0;
} | replace | 69 | 73 | 69 | 70 | -11 | |
p03171 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
ll N;
ll A[3000];
ll dp[3000 + 1][3000 + 1];
ll taro(ll i, ll j);
ll jiro(ll i, ll j);
ll taro(ll i, ll j) {
// cout << "taro: " << i << " : " << j << endl;
if (i > j) {
return 0;
}
dp[i][j] = max(A[i] + jiro(i + 1, j), A[j] + jiro(i, j - 1));
return dp[i][j];
}
ll jiro(ll i, ll j) {
// cout << "jiro: " << i << " : " << j << endl;
if (i > j) {
return 0;
}
return min(taro(i + 1, j) - A[i], taro(i, j - 1) - A[j]);
}
int main() {
cin >> N;
for (ll i = 0; i < N; i++) {
ll a;
cin >> a;
A[i] = a;
}
for (ll i = 0; i < N; i++) {
for (ll j = 0; j < N; j++) {
dp[i][j] = -1;
}
}
cout << taro(0, N - 1) << endl;
} | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
ll N;
ll A[3000];
ll dp[3000 + 1][3000 + 1];
ll taro(ll i, ll j);
ll jiro(ll i, ll j);
ll taro(ll i, ll j) {
// cout << "taro: " << i << " : " << j << endl;
if (i > j) {
return 0;
}
if (dp[i][j] != -1) {
return dp[i][j];
}
dp[i][j] = max(A[i] + jiro(i + 1, j), A[j] + jiro(i, j - 1));
return dp[i][j];
}
ll jiro(ll i, ll j) {
// cout << "jiro: " << i << " : " << j << endl;
if (i > j) {
return 0;
}
return min(taro(i + 1, j) - A[i], taro(i, j - 1) - A[j]);
}
int main() {
cin >> N;
for (ll i = 0; i < N; i++) {
ll a;
cin >> a;
A[i] = a;
}
for (ll i = 0; i < N; i++) {
for (ll j = 0; j < N; j++) {
dp[i][j] = -1;
}
}
cout << taro(0, N - 1) << endl;
} | insert | 18 | 18 | 18 | 21 | TLE | |
p03171 | C++ | Time Limit Exceeded | #include <iostream>
#define SM 3005
using namespace std;
long long n, a[SM], F[SM][SM][3];
long Try(long L, long R, int x) {
if (L > R)
return 0;
if (x == 1) {
F[L][R][x] = Try(L + 1, R, (x + 1) % 2) + a[L];
F[L][R][x] = max(F[L][R][x], Try(L, R - 1, (x + 1) % 2) + a[R]);
} else {
F[L][R][x] = Try(L + 1, R, (x + 1) % 2) - a[L];
F[L][R][x] = min(F[L][R][x], Try(L, R - 1, (x + 1) % 2) - a[R]);
}
return F[L][R][x];
}
int main() {
long long i, j, x;
cin >> n;
for (i = 1; i <= n; ++i)
cin >> a[i];
cout << Try(1, n, 1);
return 0;
}
| #include <iostream>
#define SM 3005
using namespace std;
long long n, a[SM], F[SM][SM][3];
long Try(long L, long R, int x) {
if (L > R)
return 0;
if (F[L][R][x] != 0)
return F[L][R][x];
if (x == 1) {
F[L][R][x] = Try(L + 1, R, (x + 1) % 2) + a[L];
F[L][R][x] = max(F[L][R][x], Try(L, R - 1, (x + 1) % 2) + a[R]);
} else {
F[L][R][x] = Try(L + 1, R, (x + 1) % 2) - a[L];
F[L][R][x] = min(F[L][R][x], Try(L, R - 1, (x + 1) % 2) - a[R]);
}
return F[L][R][x];
}
int main() {
long long i, j, x;
cin >> n;
for (i = 1; i <= n; ++i)
cin >> a[i];
cout << Try(1, n, 1);
return 0;
}
| insert | 7 | 7 | 7 | 9 | TLE | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ui unsigned ll
#define us unsigned short
#define IOS ios_base::sync_with_stdio(0)
#define mp(x, y) make_pair(x, y)
#define pb push_back
#define f first
#define INF pow(10, 14) + 7
#define s second
ll N = 300000;
vector<bool> visited(N + 1, false);
// bool visited[505][505];
// use ((x%n)+n)%n when x can be negative
bool isprime(ll x) {
for (ll i = 2; i <= sqrt(x); i++) {
if (x % i == 0)
return false;
}
return true;
}
void findfact(ll x, map<ll, vector<ll>> &m) {
for (ll i = 3; i * i <= x; i += 2) {
if (x % i == 0) {
if (i == x / i)
m[x].push_back(i);
else {
m[x].push_back(i);
m[x].push_back(x / i);
}
break;
}
}
return;
}
struct cmp {
bool operator()(const pair<ll, ll> &a, const pair<ll, ll> &b) {
if (a.f > b.f)
return false;
else if (a.f < b.f)
return true;
if (a.second < b.second)
return false;
return true;
}
};
bool isPalindrome(string t) {
ll st = 0;
ll end = t.size() - 1;
while (st <= end) {
if (t[st] != t[end])
return false;
st++;
end--;
}
return true;
}
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
bool pow2(ll x) {
/* First x in the below expression is for the case when x is 0 */
return x && (!(x & (x - 1)));
}
/*void dfs(vector<vector<char>>&s,ll i,ll j,ll n,ll m,ll &t,ll k,ll count)
{
if(i<0||j<0||i==n||j==m||visited[i][j]||s[i][j]=='#'||t>=count-k)
return;
visited[i][j]=true;
s[i][j]='*';
t++;
if(i>=1) dfs(s,i-1,j,n,m,t,k,count);
if(j>=1) dfs(s,i,j-1,n,m,t,k,count);
if(i<n-1) dfs(s,i+1,j,n,m,t,k,count);
if(j<m-1) dfs(s,i,j+1,n,m,t,k,count);
}*/
ull factorial(ull n) { return (n == 1 || n == 0) ? 1 : n * factorial(n - 1); }
ull binomialCoeff(ull n, ull k) {
ull res = 1;
// Since C(n, k) = C(n, n-k)
if (k > n - k)
k = n - k;
// Calculate value of
// [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
for (ull i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
vector<ll> hp;
void primeFactors(ll n) {
// Prll the number of 2s that divide n
while (n % 2 == 0) {
hp.push_back(2);
n = n / 2;
}
// n must be odd at this poll. So we can skip
// one element (Note i = i +2)
for (ll i = 3; i <= sqrt(n); i = i + 2) {
// While i divides n, prll i and divide n
while (n % i == 0) {
hp.pb(i);
n = n / i;
}
}
if (n > 2)
hp.pb(n);
// This condition is to handle the case when n
// is a prime number greater than 2
}
ll minFlipsMonoIncr(string S) { // flips to make binary string increasing
ll flip = 0;
ll ones = 0;
bool flag = false;
for (ll i = 0; i < S.size(); i++) {
if (S[i] - '0' == 1)
flag = true;
if (flag && S[i] - '0' == 0)
flip++;
else if (S[i] == '1')
ones++;
if (flip > ones)
flip = ones;
}
return flip;
}
ll kadane(vector<ll> arr) {
ll curr = 0;
ll maxsum = INT_MIN;
for (ll i = 0; i < arr.size(); i++) {
curr += arr[i];
maxsum = max(maxsum, curr);
curr = max(curr, (ll)0);
}
return maxsum;
}
ll func(vector<vector<ll>> &dp, vector<ll> arr, ll start, ll end, bool flag) {
if (start == end)
return dp[start][start] = arr[start];
if (dp[start][end] != -1)
return dp[start][end];
return dp[start][end] = max(arr[start] - func(dp, arr, start + 1, end, !flag),
arr[end] - func(dp, arr, start, end - 1, !flag));
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input", "r", stdin);
freopen("output", "w", stdout);
freopen("error", "w", stderr);
#endif
IOS;
ll T = 1;
// cin>>T;
while (T--) {
ll n, k;
cin >> n;
vector<ll> arr(n);
for (ll i = 0; i < n; i++) {
cin >> arr[i];
}
bool flag = true;
vector<vector<ll>> dp(n, vector<ll>(n, -1));
func(dp, arr, 0, arr.size() - 1, flag);
cout << dp[0][n - 1];
// cout <<setprecision(9) << ans << '\n';
}
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ui unsigned ll
#define us unsigned short
#define IOS ios_base::sync_with_stdio(0)
#define mp(x, y) make_pair(x, y)
#define pb push_back
#define f first
#define INF pow(10, 14) + 7
#define s second
ll N = 300000;
vector<bool> visited(N + 1, false);
// bool visited[505][505];
// use ((x%n)+n)%n when x can be negative
bool isprime(ll x) {
for (ll i = 2; i <= sqrt(x); i++) {
if (x % i == 0)
return false;
}
return true;
}
void findfact(ll x, map<ll, vector<ll>> &m) {
for (ll i = 3; i * i <= x; i += 2) {
if (x % i == 0) {
if (i == x / i)
m[x].push_back(i);
else {
m[x].push_back(i);
m[x].push_back(x / i);
}
break;
}
}
return;
}
struct cmp {
bool operator()(const pair<ll, ll> &a, const pair<ll, ll> &b) {
if (a.f > b.f)
return false;
else if (a.f < b.f)
return true;
if (a.second < b.second)
return false;
return true;
}
};
bool isPalindrome(string t) {
ll st = 0;
ll end = t.size() - 1;
while (st <= end) {
if (t[st] != t[end])
return false;
st++;
end--;
}
return true;
}
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
bool pow2(ll x) {
/* First x in the below expression is for the case when x is 0 */
return x && (!(x & (x - 1)));
}
/*void dfs(vector<vector<char>>&s,ll i,ll j,ll n,ll m,ll &t,ll k,ll count)
{
if(i<0||j<0||i==n||j==m||visited[i][j]||s[i][j]=='#'||t>=count-k)
return;
visited[i][j]=true;
s[i][j]='*';
t++;
if(i>=1) dfs(s,i-1,j,n,m,t,k,count);
if(j>=1) dfs(s,i,j-1,n,m,t,k,count);
if(i<n-1) dfs(s,i+1,j,n,m,t,k,count);
if(j<m-1) dfs(s,i,j+1,n,m,t,k,count);
}*/
ull factorial(ull n) { return (n == 1 || n == 0) ? 1 : n * factorial(n - 1); }
ull binomialCoeff(ull n, ull k) {
ull res = 1;
// Since C(n, k) = C(n, n-k)
if (k > n - k)
k = n - k;
// Calculate value of
// [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
for (ull i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
vector<ll> hp;
void primeFactors(ll n) {
// Prll the number of 2s that divide n
while (n % 2 == 0) {
hp.push_back(2);
n = n / 2;
}
// n must be odd at this poll. So we can skip
// one element (Note i = i +2)
for (ll i = 3; i <= sqrt(n); i = i + 2) {
// While i divides n, prll i and divide n
while (n % i == 0) {
hp.pb(i);
n = n / i;
}
}
if (n > 2)
hp.pb(n);
// This condition is to handle the case when n
// is a prime number greater than 2
}
ll minFlipsMonoIncr(string S) { // flips to make binary string increasing
ll flip = 0;
ll ones = 0;
bool flag = false;
for (ll i = 0; i < S.size(); i++) {
if (S[i] - '0' == 1)
flag = true;
if (flag && S[i] - '0' == 0)
flip++;
else if (S[i] == '1')
ones++;
if (flip > ones)
flip = ones;
}
return flip;
}
ll kadane(vector<ll> arr) {
ll curr = 0;
ll maxsum = INT_MIN;
for (ll i = 0; i < arr.size(); i++) {
curr += arr[i];
maxsum = max(maxsum, curr);
curr = max(curr, (ll)0);
}
return maxsum;
}
ll func(vector<vector<ll>> &dp, vector<ll> &arr, ll start, ll end, bool flag) {
if (start == end)
return dp[start][start] = arr[start];
if (dp[start][end] != -1)
return dp[start][end];
return dp[start][end] = max(arr[start] - func(dp, arr, start + 1, end, !flag),
arr[end] - func(dp, arr, start, end - 1, !flag));
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input", "r", stdin);
freopen("output", "w", stdout);
freopen("error", "w", stderr);
#endif
IOS;
ll T = 1;
// cin>>T;
while (T--) {
ll n, k;
cin >> n;
vector<ll> arr(n);
for (ll i = 0; i < n; i++) {
cin >> arr[i];
}
bool flag = true;
vector<vector<ll>> dp(n, vector<ll>(n, -1));
func(dp, arr, 0, arr.size() - 1, flag);
cout << dp[0][n - 1];
// cout <<setprecision(9) << ans << '\n';
}
} | replace | 146 | 147 | 146 | 147 | TLE | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define inp 10
#define check exit(0)
#define nl cout << endl;
#define mod 1000000007
#define ll long long int
#define trace(x) cerr << #x << " : " << x << endl;
#define deb(v) \
for (int i = 0; i < v.size(); i++) { \
cout << v[i]; \
(i == v.size() - 1) ? cout << "\n" : cout << " "; \
}
#define jaldi \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define ordered_set \
tree<int, null_type, less_equal<int>, rb_tree_tag, \
tree_order_statistics_node_update>
using namespace __gnu_pbds;
using namespace std;
// Pick yourself up, 'cause...
int main() {
jaldi
int n;
cin >> n;
vector<int> v(n);
for (int &x : v)
cin >> x;
ll dp[inp][inp];
for (int l = n - 1; l >= 0; l--) {
for (int r = l; r < n; r++) {
if (r == l) {
dp[l][r] = v[l];
continue;
}
dp[l][r] = max(v[l] - dp[l + 1][r], v[r] - dp[l][r - 1]);
}
}
cout << dp[0][n - 1];
return 0;
}
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define inp 3005
#define check exit(0)
#define nl cout << endl;
#define mod 1000000007
#define ll long long int
#define trace(x) cerr << #x << " : " << x << endl;
#define deb(v) \
for (int i = 0; i < v.size(); i++) { \
cout << v[i]; \
(i == v.size() - 1) ? cout << "\n" : cout << " "; \
}
#define jaldi \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define ordered_set \
tree<int, null_type, less_equal<int>, rb_tree_tag, \
tree_order_statistics_node_update>
using namespace __gnu_pbds;
using namespace std;
// Pick yourself up, 'cause...
int main() {
jaldi
int n;
cin >> n;
vector<int> v(n);
for (int &x : v)
cin >> x;
ll dp[inp][inp];
for (int l = n - 1; l >= 0; l--) {
for (int r = l; r < n; r++) {
if (r == l) {
dp[l][r] = v[l];
continue;
}
dp[l][r] = max(v[l] - dp[l + 1][r], v[r] - dp[l][r - 1]);
}
}
cout << dp[0][n - 1];
return 0;
}
| replace | 4 | 5 | 4 | 5 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
typedef long long ll;
using namespace std;
typedef pair<int, int> pii;
#define endl '\n'
#define F first
#define S second
const int MAX_N = 3e3 + 3;
int n;
int a[MAX_N];
ll dp[MAX_N][MAX_N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int d = 1, j; d <= n; d++)
for (int i = 1; i <= n; i++) {
j = i + d - 1;
if ((n - d) & 1)
dp[i][j] = min(dp[i + 1][j] - a[i], dp[i][j - 1] - a[j]);
else
dp[i][j] = max(dp[i + 1][j] + a[i], dp[i][j - 1] + a[j]);
}
printf("%lld\n", dp[1][n]);
return 0;
}
| #include <bits/stdc++.h>
typedef long long ll;
using namespace std;
typedef pair<int, int> pii;
#define endl '\n'
#define F first
#define S second
const int MAX_N = 3e3 + 3;
int n;
int a[MAX_N];
ll dp[MAX_N][MAX_N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int d = 1, j; d <= n; d++)
for (int i = 1; i + d - 1 <= n; i++) {
j = i + d - 1;
if ((n - d) & 1)
dp[i][j] = min(dp[i + 1][j] - a[i], dp[i][j - 1] - a[j]);
else
dp[i][j] = max(dp[i + 1][j] + a[i], dp[i][j - 1] + a[j]);
}
printf("%lld\n", dp[1][n]);
return 0;
}
| replace | 18 | 19 | 18 | 19 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define FOR(i, a, n) for (int i = a; i < n; i++)
#define mp make_pair
#define RFOR(i, a, n) for (int i = n; i > a; i--)
#define pii pair<long long int, long long int>
#define ub upper_bound
#define lb lower_bound
#define MOD 1000000007
typedef long long int ll;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
ll n;
cin >> n;
vector<ll> vec;
ll a;
ll dp[301][301];
for (int i = 0; i < n; i++) {
cin >> a;
vec.pb(a);
}
for (int l = n - 1; l >= 0; l--) {
for (int r = l; r < n; r++) {
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] << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define FOR(i, a, n) for (int i = a; i < n; i++)
#define mp make_pair
#define RFOR(i, a, n) for (int i = n; i > a; i--)
#define pii pair<long long int, long long int>
#define ub upper_bound
#define lb lower_bound
#define MOD 1000000007
typedef long long int ll;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
ll n;
cin >> n;
vector<ll> vec;
ll a;
ll dp[3001][3001];
for (int i = 0; i < n; i++) {
cin >> a;
vec.pb(a);
}
for (int l = n - 1; l >= 0; l--) {
for (int r = l; r < n; r++) {
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] << endl;
} | replace | 18 | 19 | 18 | 19 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX_N = 3001;
int N;
ll a[MAX_N];
ll dp[MAX_N][MAX_N][2];
int main() {
scanf("%d", &N);
for (int i = 0; i < N; ++i)
scanf("%lld", &a[i]);
for (int i = 0; i < N; ++i)
dp[i][i][0] = dp[i][i][1] = a[i];
for (int d = 1; d < N; ++d) {
for (int p = 0; p < 2; ++p) {
for (int i = 0; i < N; ++i) {
int j = i + d;
dp[i][j][p] =
max(a[i] - dp[i + 1][j][1 - p], a[j] - dp[i][j - 1][1 - p]);
}
}
}
printf("%lld\n", dp[0][N - 1][0]);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX_N = 3001;
int N;
ll a[MAX_N];
ll dp[MAX_N][MAX_N][2];
int main() {
scanf("%d", &N);
for (int i = 0; i < N; ++i)
scanf("%lld", &a[i]);
for (int i = 0; i < N; ++i)
dp[i][i][0] = dp[i][i][1] = a[i];
for (int d = 1; d < N; ++d) {
for (int p = 0; p < 2; ++p) {
for (int i = 0; i + d < N; ++i) {
int j = i + d;
dp[i][j][p] =
max(a[i] - dp[i + 1][j][1 - p], a[j] - dp[i][j - 1][1 - p]);
}
}
}
printf("%lld\n", dp[0][N - 1][0]);
return 0;
} | replace | 22 | 23 | 22 | 23 | -11 | |
p03171 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define w while
#define loop(i, l, r) for (int i = l; i < r; i++)
#define ci cin >>
#define co cout <<
#define mm 1000000007
#define ll long long
using namespace std;
ll int helper(vector<vector<ll int>> &dp, vector<ll int> vec, int start,
int last) {
if (start > last) {
return 0;
}
if (start == last)
return vec[start];
if (last - start == 1)
return max(vec[last], vec[start]);
if (dp[start][last] != -1)
return dp[start][last];
ll int a = vec[start] + min(helper(dp, vec, start + 2, last),
helper(dp, vec, start + 1, last - 1));
ll int b = vec[last] + min(helper(dp, vec, start + 1, last - 1),
helper(dp, vec, start, last - 2));
dp[start][last] = max(a, b);
return max(a, b);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll int n;
cin >> n;
ll int sum = 0;
vector<ll int> vec(n);
for (int i = 0; i < n; i++) {
cin >> vec[i];
sum += vec[i];
}
vector<vector<ll int>> dp(n, vector<ll int>(n, -1));
ll int taro = helper(dp, vec, 0, n - 1);
cout << (taro - sum + taro);
}
| #include <bits/stdc++.h>
#define w while
#define loop(i, l, r) for (int i = l; i < r; i++)
#define ci cin >>
#define co cout <<
#define mm 1000000007
#define ll long long
using namespace std;
ll int helper(vector<vector<ll int>> &dp, vector<ll int> &vec, int start,
int last) {
if (start > last) {
return 0;
}
if (start == last)
return vec[start];
if (last - start == 1)
return max(vec[last], vec[start]);
if (dp[start][last] != -1)
return dp[start][last];
ll int a = vec[start] + min(helper(dp, vec, start + 2, last),
helper(dp, vec, start + 1, last - 1));
ll int b = vec[last] + min(helper(dp, vec, start + 1, last - 1),
helper(dp, vec, start, last - 2));
dp[start][last] = max(a, b);
return max(a, b);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll int n;
cin >> n;
ll int sum = 0;
vector<ll int> vec(n);
for (int i = 0; i < n; i++) {
cin >> vec[i];
sum += vec[i];
}
vector<vector<ll int>> dp(n, vector<ll int>(n, -1));
ll int taro = helper(dp, vec, 0, n - 1);
cout << (taro - sum + taro);
}
| replace | 8 | 9 | 8 | 9 | TLE | |
p03171 | C++ | Runtime Error | #define _CRT_SECURE_NO_WARNINGS
// #include <stdafx.h>
// #include "targetver.h"
#include <stdio.h>
// #include <tchar.h>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <unordered_map>
#include <unordered_set>
#endif>
#define LL long long
#define MOD 1000000007
#define INF 1000000000
#define LINF 10000000000000000
using namespace std;
const int N = 2e3 + 10;
LL a[N];
LL b[N];
LL pr[N];
LL dp[N][N];
vector<int> vec;
LL n;
LL solve(LL l, LL r) {
if (l == r)
return a[l];
if (dp[l][r] != 0)
return dp[l][r];
LL p1 = a[l] + ((pr[r] - pr[l]) - solve(l + 1, r));
LL p2 = a[r] + ((pr[r - 1] - pr[l - 1]) - solve(l, r - 1));
dp[l][r] = max(p1, p2);
return dp[l][r];
}
int main() {
scanf("%lld", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
pr[i] = pr[i - 1] + a[i];
}
LL x = solve(1, n);
cout << x - (pr[n] - x);
// cin >> n;
} | #define _CRT_SECURE_NO_WARNINGS
// #include <stdafx.h>
// #include "targetver.h"
#include <stdio.h>
// #include <tchar.h>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <unordered_map>
#include <unordered_set>
#endif>
#define LL long long
#define MOD 1000000007
#define INF 1000000000
#define LINF 10000000000000000
using namespace std;
const int N = 5e3 + 10;
LL a[N];
LL b[N];
LL pr[N];
LL dp[N][N];
vector<int> vec;
LL n;
LL solve(LL l, LL r) {
if (l == r)
return a[l];
if (dp[l][r] != 0)
return dp[l][r];
LL p1 = a[l] + ((pr[r] - pr[l]) - solve(l + 1, r));
LL p2 = a[r] + ((pr[r - 1] - pr[l - 1]) - solve(l, r - 1));
dp[l][r] = max(p1, p2);
return dp[l][r];
}
int main() {
scanf("%lld", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
pr[i] = pr[i - 1] + a[i];
}
LL x = solve(1, n);
cout << x - (pr[n] - x);
// cin >> n;
} | replace | 67 | 68 | 67 | 68 | 0 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
ll pref_sum[3000][3000], dp[3000][3000];
int a[3000];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
memset(pref_sum, 0, sizeof(pref_sum));
memset(dp, 0, sizeof(dp));
for (int i = 0; i < n; i++) {
pref_sum[i][i] = a[i];
dp[i][i] = a[i];
for (int j = i + 1; j < n; j++)
pref_sum[i][j] = pref_sum[i][j - 1] + a[j];
}
int i = 0, j = 1;
int jstart = 1;
while (!(i == 0 && j == n - 1)) {
dp[i][j] = pref_sum[i][j] - min(dp[i][j - 1], dp[i + 1][j]);
i++;
j++;
if (j == n) {
i = 0;
jstart++;
j = jstart;
}
}
ll scoreB = min(dp[0][n - 2], dp[1][n - 1]);
ll scoreA = pref_sum[0][n - 1] - scoreB;
cout << scoreA - scoreB << endl;
}
| #include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
ll pref_sum[3000][3000], dp[3000][3000];
int a[3000];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
if (n == 1) {
cout << a[0] << endl;
return 0;
}
memset(pref_sum, 0, sizeof(pref_sum));
memset(dp, 0, sizeof(dp));
for (int i = 0; i < n; i++) {
pref_sum[i][i] = a[i];
dp[i][i] = a[i];
for (int j = i + 1; j < n; j++)
pref_sum[i][j] = pref_sum[i][j - 1] + a[j];
}
int i = 0, j = 1;
int jstart = 1;
while (!(i == 0 && j == n - 1)) {
dp[i][j] = pref_sum[i][j] - min(dp[i][j - 1], dp[i + 1][j]);
i++;
j++;
if (j == n) {
i = 0;
jstart++;
j = jstart;
}
}
ll scoreB = min(dp[0][n - 2], dp[1][n - 1]);
ll scoreA = pref_sum[0][n - 1] - scoreB;
cout << scoreA - scoreB << endl;
}
| insert | 10 | 10 | 10 | 14 | -11 | |
p03171 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
vector<vector<long long int>> ve;
long long int recurse(long long int *arr, int i, int j) {
if (i > j) {
return 0;
} else if (i == j) {
ve[i][j] = arr[i];
// cout<<i<<" "<<j<<" "<<ve[i][j]<<endl;
return arr[i];
} else if ((i + 1) == j) {
long long int maximum = max(arr[i], arr[j]);
ve[i][j] = maximum;
return maximum;
} else if (ve[i][j] != 0) {
return ve[i][j];
} else {
long long int max1 = recurse(arr, i + 1, j - 1) + arr[i];
long long int max2 = recurse(arr, i + 2, j) + arr[i];
long long int max3 = recurse(arr, i + 1, j - 1) + arr[j];
long long int max4 = recurse(arr, i, j - 2) + arr[j];
ve[i][j] = min(max1, max2);
long long int fake = min(max3, max4);
ve[i][j] = max(ve[i][j], fake);
}
}
int main(void) {
int n;
cin >> n;
ve.resize(n);
for (int i = 0; i < n; i++) {
ve[i].resize(n);
}
long long int arr[n];
for (int i = 0; i < n; i++)
cin >> arr[i];
long long int ans = recurse(arr, 0, n - 1);
long long int sum = 0;
for (int i = 0; i < n; i++)
sum += arr[i];
long long int fake = sum - ans;
cout << (ans - fake) << endl;
} | #include <bits/stdc++.h>
using namespace std;
vector<vector<long long int>> ve;
long long int recurse(long long int *arr, int i, int j) {
if (i > j) {
return 0;
} else if (i == j) {
ve[i][j] = arr[i];
// cout<<i<<" "<<j<<" "<<ve[i][j]<<endl;
return arr[i];
} else if ((i + 1) == j) {
long long int maximum = max(arr[i], arr[j]);
ve[i][j] = maximum;
return maximum;
} else if (ve[i][j] != 0) {
return ve[i][j];
} else {
long long int max1 = recurse(arr, i + 1, j - 1) + arr[i];
long long int max2 = recurse(arr, i + 2, j) + arr[i];
long long int max3 = recurse(arr, i + 1, j - 1) + arr[j];
long long int max4 = recurse(arr, i, j - 2) + arr[j];
ve[i][j] = min(max1, max2);
long long int fake = min(max3, max4);
ve[i][j] = max(ve[i][j], fake);
return ve[i][j];
}
}
int main(void) {
int n;
cin >> n;
ve.resize(n);
for (int i = 0; i < n; i++) {
ve[i].resize(n);
}
long long int arr[n];
for (int i = 0; i < n; i++)
cin >> arr[i];
long long int ans = recurse(arr, 0, n - 1);
long long int sum = 0;
for (int i = 0; i < n; i++)
sum += arr[i];
long long int fake = sum - ans;
cout << (ans - fake) << endl;
} | insert | 24 | 24 | 24 | 25 | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.