#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
typedef pair<int,int> pi;
typedef vector <int> vi;
typedef vector <pi> vpi;
typedef pair<pi, ll> pii;
typedef set <ll> si;
typedef long double ld;
#define f first
#define s second
#define mp make_pair
#define FOR(i,s,e) for(int i=s;i<=int(e);++i)
#define DEC(i,s,e) for(int i=s;i>=int(e);--i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i,x) for (auto i: x)
#define mem(x,i) memset(x,i,sizeof x)
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define maxn 611
#define INF (ll)(1e18+10)
#define MOD 998244353
typedef pair <vi, int> pvi;
typedef pair <int,pi> ipi;
typedef vector <pii> vpii;
typedef pair <pi,pi> pipi;
int dp[maxn][maxn][maxn];
int A[maxn], sm = 0;
int M,L;
int32_t main(){
fast;
cin >> M >> L;
FOR(i,0,2*M) cin >> A[i];
vector <pii> v;
int ans = 0;
FOR(i,M,2*M) if (sm <= L){
int x = INF;
if (i != M){
x = (L - sm) / (i - M);
}
x = min(x, A[i]);
sm += x * (i-M);
A[i] -= x;
v.pb(pii(pi(-(i-M), x), -1));
ans += x;
assert(sm <= L+M);
if (A[i] > 0 && sm < L){
sm += i - M; A[i]--; ans++;
v.pb(pii(pi(-(i-M),1),-1));
assert(sm <= L + M && sm >= L);
break;
}
}
//cout << sm << ' ' << ans << '\n';
//FOR(i,0,2*M) cout << A[i] << ' ';
//cout << '\n';
//assert(sm <= L + M);
if (sm < L){
cout << "impossible"; return 0;
}
FOR(i,0,2*M) v.pb(pii(pi(i-M, A[i]), 1));
vector <pii> pos, neg;
aFOR(i, v) if (i.f.s > 0){
if (i.f.f >= 0) pos.pb(i);
else neg.pb(i);
}
FOR(i,0,2*M) dp[i][0][0] = -INF;
dp[M][0][0] = 0;
FOR(j,0,pos.size()) FOR(k,0,neg.size()) if (j > 0 || k > 0){
FOR(i,0,2*M){
dp[i][j][k] = -INF;
if (j > 0){
FOR(x,0,min(pos[j-1].f.s, 2*M)) if (0 <= i + x * pos[j-1].f.f && i + x * pos[j-1].f.f <= 2*M){
dp[i][j][k] = max(dp[i][j][k], dp[i+x*pos[j-1].f.f][j-1][k] + x * pos[j-1].s);
}
}
if (k > 0){
FOR(x,0,min(neg[k-1].f.s, 2*M)) if (0 <= i + x * neg[k-1].f.f && i + x * neg[k-1].f.f <= 2*M){
dp[i][j][k] = max(dp[i][j][k], dp[i+x*neg[k-1].f.f][j][k-1] + x * neg[k-1].s);
}
}
}
}
ans += dp[sm - L + M][pos.size()][neg.size()];
if (ans < 0) cout << "impossible";
else cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
1172 KB |
Output is correct |
5 |
Runtime error |
80 ms |
45772 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
1172 KB |
Output is correct |
5 |
Runtime error |
80 ms |
45772 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1236 KB |
Output is correct |
2 |
Correct |
2 ms |
1876 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
4 ms |
5588 KB |
Output is correct |
5 |
Correct |
4 ms |
2132 KB |
Output is correct |
6 |
Correct |
4 ms |
7892 KB |
Output is correct |
7 |
Incorrect |
1 ms |
1492 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1236 KB |
Output is correct |
2 |
Correct |
2 ms |
1876 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
4 ms |
5588 KB |
Output is correct |
5 |
Correct |
4 ms |
2132 KB |
Output is correct |
6 |
Correct |
4 ms |
7892 KB |
Output is correct |
7 |
Incorrect |
1 ms |
1492 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1236 KB |
Output is correct |
2 |
Correct |
2 ms |
1876 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
4 ms |
5588 KB |
Output is correct |
5 |
Correct |
4 ms |
2132 KB |
Output is correct |
6 |
Correct |
4 ms |
7892 KB |
Output is correct |
7 |
Incorrect |
1 ms |
1492 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
1172 KB |
Output is correct |
5 |
Runtime error |
80 ms |
45772 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1236 KB |
Output is correct |
2 |
Correct |
2 ms |
1876 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
4 ms |
5588 KB |
Output is correct |
5 |
Correct |
4 ms |
2132 KB |
Output is correct |
6 |
Correct |
4 ms |
7892 KB |
Output is correct |
7 |
Incorrect |
1 ms |
1492 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
1172 KB |
Output is correct |
5 |
Runtime error |
80 ms |
45772 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1236 KB |
Output is correct |
2 |
Correct |
2 ms |
1876 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
4 ms |
5588 KB |
Output is correct |
5 |
Correct |
4 ms |
2132 KB |
Output is correct |
6 |
Correct |
4 ms |
7892 KB |
Output is correct |
7 |
Incorrect |
1 ms |
1492 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
1172 KB |
Output is correct |
5 |
Runtime error |
80 ms |
45772 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |