#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define a first
#define b second
#define pb push_back
#define endl '\n'
llo n,dd;
llo it[350];
llo cc[601];
llo dp[2000001];
deque<pair<llo,llo>> zz[301];
llo ans=-1;
void apply(int l,int r){
for(int j=l;j<=r;j++){
llo ax=j*min(it[j],n);
llo y=0;
llo z=0;
llo su3=n*n*n;
for(int i=0;i<j;i++){
zz[i].clear();
}
for(llo ii=0;ii<=su3;ii++){
// llo y=ii%j;
if(dp[ii]>=0){
pair<llo,llo> cur={dp[ii]-z,ii};
while(zz[y].size()){
if(zz[y].back().a>=cur.a){
zz[y].pop_back();
}
else{
break;
}
}
zz[y].pb(cur);
}
while(zz[y].size()){
if(zz[y].front().b<ii-ax){
zz[y].pop_front();
}
else{
break;
}
}
if(zz[y].size()){
llo xx=zz[y].front().a+z;
if(dp[ii]==-1){
dp[ii]=xx;
}
dp[ii]=min(dp[ii],xx);
}
y++;
if(y==j){
y=0;
z++;
}
}
}
}
llo pp;
llo su3=0;
void solve(int l,int r){
if(l==r){
llo ac=0;
llo ac2=0;
for(int j=r+1;j<=n;j++){
ac+=((it[j]-min(it[j],n)))*j;
ac2+=(it[j]-min(it[j],n));
}
if(ac>pp){
return;
}
llo cur2=pp-ac;
int i=l;
//cout<<cur2<<",,"<<ac<<","<<pp<<endl;
for(llo j=cur2%i;j<=su3 and j<=cur2;j+=i){
if(dp[j]>=0){
//if((cur2-j)%i==0){
llo x=(cur2-j)/i;
if(x<=it[i]){
llo y=dp[j]+x;
y+=ac2;
if(ans==-1){
ans=y;
}
ans=min(ans,y);
}
//}
}
}
}
else{
int mid=(l+r)/2;
vector<llo> dp2;
for(int ii=0;ii<=su3;ii++){
dp2.pb(dp[ii]);
}
apply(l,mid);
solve(mid+1,r);
for(int ii=0;ii<=su3;ii++){
dp[ii]=dp2[ii];
}
apply(mid+1,r);
solve(l,mid);
for(int ii=0;ii<=su3;ii++){
dp[ii]=dp2[ii];
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n>>dd;
llo cot=0;
llo ma=0;
llo acc=0;
for(llo i=0;i<n;i++){
llo aa;
cin>>aa;
cot+=aa;
cc[i]=aa;
acc+=aa;
}
llo su=0;
for(llo i=0;i<=n;i++){
cin>>it[i];
su+=it[i]*i;
cot+=it[i];
cc[n+i]=i;
}
for(llo i=0;i<=2*n;i++){
ma=max(ma,cc[i]);
}
su3=n*n*n;
if(su<dd){
cout<<"impossible"<<endl;
return 0;
}
llo cur=su-dd;
pp=cur;
it[0]=0;
for(int i=0;i<=su3;i++){
dp[i]=-1;
}
dp[0]=0;
solve(1,n);
/*for(llo i=1;i<=n;i++){
llo su2=0;
llo ac=0;
for(llo j=i+1;j<=n;j++){
llo xx=it[j]-n;
if(xx<0){
xx=0;
}
ac+=xx;
su2+=xx*j;
}
if(su2>cur){
continue;
}
llo su3=0;//n*n*n;
for(llo j=1;j<i;j++){
su3+=j*min(it[j],i);
}
for(llo j=i+1;j<=n;j++){
su3+=j*min(it[j],n);
}
for(llo j=0;j<=su3;j++){
dp[j]=-1;
}
dp[0]=0;
su3=0;
for(llo j=1;j<=n;j++){
if(j==i){
continue;
}
if(j<i){
su3+=j*min(it[j],i);
}
else{
su3+=j*min(it[j],n);
}
for(llo jj=0;jj<j;jj++){
zz[jj].clear();
}
llo ax=j*min(it[j],n);
llo y=0;
llo z=0;
for(llo ii=0;ii<=su3;ii++){
// llo y=ii%j;
if(dp[ii]>=0){
pair<llo,llo> cur={dp[ii]-z,ii};
while(zz[y].size()){
if(zz[y].back().a>=cur.a){
zz[y].pop_back();
}
else{
break;
}
}
zz[y].pb(cur);
}
while(zz[y].size()){
if(zz[y].front().b<ii-ax){
zz[y].pop_front();
}
else{
break;
}
}
if(zz[y].size()){
llo xx=zz[y].front().a+z;
if(dp[ii]==-1){
dp[ii]=xx;
}
dp[ii]=min(dp[ii],xx);
}
y++;
if(y==j){
y=0;
z++;
}
}
}
llo cur2=cur-su2;
for(llo j=cur2%i;j<=su3 and j<=cur2;j+=i){
if(dp[j]>=0){
//if((cur2-j)%i==0){
llo x=(cur2-j)/i;
if(x<=it[i]){
llo y=dp[j]+x;
y+=ac;
if(ans==-1){
ans=y;
}
ans=min(ans,y);
}
//}
}
}
}*/
if(ans==-1){
cout<<"impossible"<<endl;
}
else{
//cout<<ans<<endl;
cout<<cot-ans<<endl;
}
//else find minimum number of elements with sum su-dd
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
215 ms |
9048 KB |
Output is correct |
6 |
Incorrect |
224 ms |
8908 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
215 ms |
9048 KB |
Output is correct |
6 |
Incorrect |
224 ms |
8908 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
27 ms |
2108 KB |
Output is correct |
3 |
Correct |
21 ms |
2096 KB |
Output is correct |
4 |
Correct |
27 ms |
2088 KB |
Output is correct |
5 |
Correct |
32 ms |
2192 KB |
Output is correct |
6 |
Correct |
26 ms |
2100 KB |
Output is correct |
7 |
Correct |
24 ms |
2124 KB |
Output is correct |
8 |
Correct |
20 ms |
2096 KB |
Output is correct |
9 |
Correct |
23 ms |
2088 KB |
Output is correct |
10 |
Correct |
25 ms |
2108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
27 ms |
2108 KB |
Output is correct |
3 |
Correct |
21 ms |
2096 KB |
Output is correct |
4 |
Correct |
27 ms |
2088 KB |
Output is correct |
5 |
Correct |
32 ms |
2192 KB |
Output is correct |
6 |
Correct |
26 ms |
2100 KB |
Output is correct |
7 |
Correct |
24 ms |
2124 KB |
Output is correct |
8 |
Correct |
20 ms |
2096 KB |
Output is correct |
9 |
Correct |
23 ms |
2088 KB |
Output is correct |
10 |
Correct |
25 ms |
2108 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
26 ms |
2100 KB |
Output is correct |
16 |
Correct |
22 ms |
2104 KB |
Output is correct |
17 |
Correct |
31 ms |
2088 KB |
Output is correct |
18 |
Correct |
34 ms |
2096 KB |
Output is correct |
19 |
Correct |
33 ms |
2092 KB |
Output is correct |
20 |
Correct |
19 ms |
2112 KB |
Output is correct |
21 |
Correct |
19 ms |
2084 KB |
Output is correct |
22 |
Correct |
20 ms |
2088 KB |
Output is correct |
23 |
Correct |
23 ms |
2092 KB |
Output is correct |
24 |
Incorrect |
27 ms |
2088 KB |
Output isn't correct |
25 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
27 ms |
2108 KB |
Output is correct |
3 |
Correct |
21 ms |
2096 KB |
Output is correct |
4 |
Correct |
27 ms |
2088 KB |
Output is correct |
5 |
Correct |
32 ms |
2192 KB |
Output is correct |
6 |
Correct |
26 ms |
2100 KB |
Output is correct |
7 |
Correct |
24 ms |
2124 KB |
Output is correct |
8 |
Correct |
20 ms |
2096 KB |
Output is correct |
9 |
Correct |
23 ms |
2088 KB |
Output is correct |
10 |
Correct |
25 ms |
2108 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
26 ms |
2100 KB |
Output is correct |
13 |
Correct |
22 ms |
2120 KB |
Output is correct |
14 |
Correct |
28 ms |
2088 KB |
Output is correct |
15 |
Correct |
34 ms |
2224 KB |
Output is correct |
16 |
Correct |
34 ms |
2064 KB |
Output is correct |
17 |
Correct |
22 ms |
2092 KB |
Output is correct |
18 |
Correct |
19 ms |
2096 KB |
Output is correct |
19 |
Correct |
21 ms |
2100 KB |
Output is correct |
20 |
Correct |
23 ms |
2096 KB |
Output is correct |
21 |
Correct |
171 ms |
9424 KB |
Output is correct |
22 |
Correct |
188 ms |
8476 KB |
Output is correct |
23 |
Correct |
227 ms |
8448 KB |
Output is correct |
24 |
Correct |
202 ms |
8480 KB |
Output is correct |
25 |
Correct |
245 ms |
8884 KB |
Output is correct |
26 |
Correct |
267 ms |
8476 KB |
Output is correct |
27 |
Correct |
260 ms |
8948 KB |
Output is correct |
28 |
Correct |
207 ms |
9088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
215 ms |
9048 KB |
Output is correct |
6 |
Incorrect |
224 ms |
8908 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
27 ms |
2108 KB |
Output is correct |
3 |
Correct |
21 ms |
2096 KB |
Output is correct |
4 |
Correct |
27 ms |
2088 KB |
Output is correct |
5 |
Correct |
32 ms |
2192 KB |
Output is correct |
6 |
Correct |
26 ms |
2100 KB |
Output is correct |
7 |
Correct |
24 ms |
2124 KB |
Output is correct |
8 |
Correct |
20 ms |
2096 KB |
Output is correct |
9 |
Correct |
23 ms |
2088 KB |
Output is correct |
10 |
Correct |
25 ms |
2108 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
26 ms |
2100 KB |
Output is correct |
13 |
Correct |
22 ms |
2120 KB |
Output is correct |
14 |
Correct |
28 ms |
2088 KB |
Output is correct |
15 |
Correct |
34 ms |
2224 KB |
Output is correct |
16 |
Correct |
34 ms |
2064 KB |
Output is correct |
17 |
Correct |
22 ms |
2092 KB |
Output is correct |
18 |
Correct |
19 ms |
2096 KB |
Output is correct |
19 |
Correct |
21 ms |
2100 KB |
Output is correct |
20 |
Correct |
23 ms |
2096 KB |
Output is correct |
21 |
Correct |
171 ms |
9424 KB |
Output is correct |
22 |
Correct |
188 ms |
8476 KB |
Output is correct |
23 |
Correct |
227 ms |
8448 KB |
Output is correct |
24 |
Correct |
202 ms |
8480 KB |
Output is correct |
25 |
Correct |
245 ms |
8884 KB |
Output is correct |
26 |
Correct |
267 ms |
8476 KB |
Output is correct |
27 |
Correct |
260 ms |
8948 KB |
Output is correct |
28 |
Correct |
207 ms |
9088 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
36 ms |
2100 KB |
Output is correct |
31 |
Correct |
23 ms |
2100 KB |
Output is correct |
32 |
Correct |
30 ms |
2216 KB |
Output is correct |
33 |
Correct |
39 ms |
2184 KB |
Output is correct |
34 |
Correct |
25 ms |
2088 KB |
Output is correct |
35 |
Correct |
20 ms |
2108 KB |
Output is correct |
36 |
Correct |
19 ms |
2096 KB |
Output is correct |
37 |
Correct |
21 ms |
2088 KB |
Output is correct |
38 |
Correct |
23 ms |
2084 KB |
Output is correct |
39 |
Correct |
156 ms |
9428 KB |
Output is correct |
40 |
Correct |
166 ms |
8444 KB |
Output is correct |
41 |
Correct |
257 ms |
8476 KB |
Output is correct |
42 |
Correct |
197 ms |
8480 KB |
Output is correct |
43 |
Correct |
224 ms |
8904 KB |
Output is correct |
44 |
Correct |
310 ms |
8600 KB |
Output is correct |
45 |
Correct |
238 ms |
8932 KB |
Output is correct |
46 |
Correct |
207 ms |
8936 KB |
Output is correct |
47 |
Correct |
2956 ms |
79220 KB |
Output is correct |
48 |
Correct |
2958 ms |
71496 KB |
Output is correct |
49 |
Correct |
4434 ms |
71860 KB |
Output is correct |
50 |
Correct |
3616 ms |
75960 KB |
Output is correct |
51 |
Correct |
4591 ms |
71768 KB |
Output is correct |
52 |
Execution timed out |
5027 ms |
71732 KB |
Time limit exceeded |
53 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
215 ms |
9048 KB |
Output is correct |
6 |
Incorrect |
224 ms |
8908 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
27 ms |
2108 KB |
Output is correct |
3 |
Correct |
21 ms |
2096 KB |
Output is correct |
4 |
Correct |
27 ms |
2088 KB |
Output is correct |
5 |
Correct |
32 ms |
2192 KB |
Output is correct |
6 |
Correct |
26 ms |
2100 KB |
Output is correct |
7 |
Correct |
24 ms |
2124 KB |
Output is correct |
8 |
Correct |
20 ms |
2096 KB |
Output is correct |
9 |
Correct |
23 ms |
2088 KB |
Output is correct |
10 |
Correct |
25 ms |
2108 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
26 ms |
2100 KB |
Output is correct |
13 |
Correct |
22 ms |
2120 KB |
Output is correct |
14 |
Correct |
28 ms |
2088 KB |
Output is correct |
15 |
Correct |
34 ms |
2224 KB |
Output is correct |
16 |
Correct |
34 ms |
2064 KB |
Output is correct |
17 |
Correct |
22 ms |
2092 KB |
Output is correct |
18 |
Correct |
19 ms |
2096 KB |
Output is correct |
19 |
Correct |
21 ms |
2100 KB |
Output is correct |
20 |
Correct |
23 ms |
2096 KB |
Output is correct |
21 |
Correct |
171 ms |
9424 KB |
Output is correct |
22 |
Correct |
188 ms |
8476 KB |
Output is correct |
23 |
Correct |
227 ms |
8448 KB |
Output is correct |
24 |
Correct |
202 ms |
8480 KB |
Output is correct |
25 |
Correct |
245 ms |
8884 KB |
Output is correct |
26 |
Correct |
267 ms |
8476 KB |
Output is correct |
27 |
Correct |
260 ms |
8948 KB |
Output is correct |
28 |
Correct |
207 ms |
9088 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
36 ms |
2100 KB |
Output is correct |
31 |
Correct |
23 ms |
2100 KB |
Output is correct |
32 |
Correct |
30 ms |
2216 KB |
Output is correct |
33 |
Correct |
39 ms |
2184 KB |
Output is correct |
34 |
Correct |
25 ms |
2088 KB |
Output is correct |
35 |
Correct |
20 ms |
2108 KB |
Output is correct |
36 |
Correct |
19 ms |
2096 KB |
Output is correct |
37 |
Correct |
21 ms |
2088 KB |
Output is correct |
38 |
Correct |
23 ms |
2084 KB |
Output is correct |
39 |
Correct |
156 ms |
9428 KB |
Output is correct |
40 |
Correct |
166 ms |
8444 KB |
Output is correct |
41 |
Correct |
257 ms |
8476 KB |
Output is correct |
42 |
Correct |
197 ms |
8480 KB |
Output is correct |
43 |
Correct |
224 ms |
8904 KB |
Output is correct |
44 |
Correct |
310 ms |
8600 KB |
Output is correct |
45 |
Correct |
238 ms |
8932 KB |
Output is correct |
46 |
Correct |
207 ms |
8936 KB |
Output is correct |
47 |
Correct |
2956 ms |
79220 KB |
Output is correct |
48 |
Correct |
2958 ms |
71496 KB |
Output is correct |
49 |
Correct |
4434 ms |
71860 KB |
Output is correct |
50 |
Correct |
3616 ms |
75960 KB |
Output is correct |
51 |
Correct |
4591 ms |
71768 KB |
Output is correct |
52 |
Execution timed out |
5027 ms |
71732 KB |
Time limit exceeded |
53 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
215 ms |
9048 KB |
Output is correct |
6 |
Incorrect |
224 ms |
8908 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |