#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 311
#define INF (ll)(1e18+10)
#define MOD 998266353
typedef pair <vi, int> pvi;
typedef pair <int,pi> ipi;
typedef vector <pii> vpii;
typedef pair <pi,pi> pipi;
int dp[2][maxn*maxn*6];
int A[maxn*2];
int M,L;
int32_t main(){
fast;
cin >> M >> L;
FOR(i,0,2*M) cin >> A[i];
vector <pii> v;
int ans = 0,sm=0;
int total = 0;
FOR(i,0,2*M) total += A[i] * (i - M);
if (total >= L){
FOR(i,0,M){
int x = A[i];
ans += x;
A[i] -= x;
sm += (i - M) * x;
v.pb(pii(pi(-(i-M), x), -1));
}
FOR(i,M+1,2*M){
int x = min((L - sm) / (i - M), A[i]);
ans += x;
A[i] -= x;
sm += (i-M) * x;
v.pb(pii(pi(-(i-M), x), -1));
}
}else{
FOR(i,M,2*M){
int x = A[i];
A[i] -= x;
ans += x;
sm += (i-M) * x;
v.pb(pii(pi(-(i-M), x), -1));
}
DEC(i,M-1,0){
int x = min((sm - L) / (-(i - M)), A[i]);
A[i] -= x;
ans += x;
sm += (i-M) * x;
v.pb(pii(pi(-(i-M), x), -1));
}
}
FOR(i,0,2*M) v.pb(pii(pi(i-M, A[i]), 1));
vpi v2;
aFOR(i,v){
int x = min(2 * M, i.f.s);
for (int k = 0; (1 << k) <= x; k++){
v2.pb(pi((1<<k) * i.f.f, (1<<k)*i.s));
x -= (1 << k);
}
if (x > 0) v2.pb(pi(x * i.f.f, x*i.s));
}
FOR(i,0,6*M*M) dp[0][i] = -INF;
dp[0][M] = 0;
aFOR(i,v2){
FOR(j,0,6*M*M) if (j - i.f >= 0 && j - i.f <= 6*M*M) dp[1][j] = max(dp[0][j], dp[0][j-i.f] + i.s);
FOR(j,0,6*M*M) dp[0][j] = dp[1][j];
}
if (L - sm + M < 0 || L - sm + M > 6 * M * M){
cout << "impossible"; return 0;
}
ans += dp[0][L-sm+M];
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 |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
11 ms |
588 KB |
Output is correct |
6 |
Correct |
9 ms |
596 KB |
Output is correct |
7 |
Incorrect |
5 ms |
468 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 |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
11 ms |
588 KB |
Output is correct |
6 |
Correct |
9 ms |
596 KB |
Output is correct |
7 |
Incorrect |
5 ms |
468 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 |
2 ms |
340 KB |
Output is correct |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
11 ms |
588 KB |
Output is correct |
6 |
Correct |
9 ms |
596 KB |
Output is correct |
7 |
Incorrect |
5 ms |
468 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 |
2 ms |
340 KB |
Output is correct |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
11 ms |
588 KB |
Output is correct |
6 |
Correct |
9 ms |
596 KB |
Output is correct |
7 |
Incorrect |
5 ms |
468 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 |
2 ms |
340 KB |
Output is correct |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
11 ms |
588 KB |
Output is correct |
6 |
Correct |
9 ms |
596 KB |
Output is correct |
7 |
Incorrect |
5 ms |
468 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |