#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 dp[1000001];
deque<pair<llo,llo>> zz[301];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n>>dd;
llo cot=0;
for(llo i=0;i<n;i++){
llo aa;
cin>>aa;
cot+=aa;
}
llo su=0;
for(llo i=0;i<=n;i++){
cin>>it[i];
su+=it[i]*i;
cot+=it[i];
}
if(su<dd){
cout<<"impossible"<<endl;
return 0;
}
llo cur=su-dd;
it[0]=0;
llo ans=-1;
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=n*n*n;
/*for(llo j=1;j<i;j++){
su3+=j*min(n,it[j]);
}
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;
for(llo j=1;j<=n;j++){
if(j==i){
continue;
}
for(int jj=0;jj<j;jj++){
zz[jj].clear();
}
for(int ii=0;ii<=su3;ii++){
int y=ii%j;
if(dp[ii]>=0){
pair<int,int> cur={dp[ii]-(ii/j),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-j*min(it[j],n)){
zz[y].pop_front();
}
else{
break;
}
}
if(zz[y].size()){
llo xx=zz[y].front().a+(ii/j) ;
if(dp[ii]==-1){
dp[ii]=xx;
}
dp[ii]=min(dp[ii],xx);
}
}
/* for(llo ii=su3;ii>=0;ii--){
llo co=0;
for(llo jj=ii;jj>=0 and jj>=ii-j*n;jj-=j){
if(dp[jj]>=0){
llo xx=dp[jj]+co;
if(co>it[j]){
continue;
}
if(dp[ii]==-1){
dp[ii]=xx;
}
dp[ii]=min(dp[ii],xx);
}
co++;
}
}*/
}
/* for(llo j=i+1;j<=n;j++){
for(llo ii=su3;ii>=0;ii--){
llo co=0;
for(llo jj=ii;jj>=0 and jj>=ii-j*min(it[j],n);jj-=j){
if(dp[jj]>=0){
llo xx=dp[jj]+co;
if(dp[ii]==-1){
dp[ii]=xx;
}
dp[ii]=min(dp[ii],xx);
}
co++;
}
}
}*/
llo cur2=cur-su2;
for(llo j=0;j<=su3;j++){
if(cur2>=j and 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;
}
/*
if(y==11){
cout<<dp[j]<<endl;
cout<<cur2<<",,,,"<<su2<<endl;
cout<<i<<"::"<<j<<endl;
}*/
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 |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
2386 ms |
1620 KB |
Output is correct |
6 |
Incorrect |
2446 ms |
1548 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
2386 ms |
1620 KB |
Output is correct |
6 |
Incorrect |
2446 ms |
1548 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 |
35 ms |
724 KB |
Output is correct |
3 |
Correct |
6 ms |
752 KB |
Output is correct |
4 |
Correct |
127 ms |
752 KB |
Output is correct |
5 |
Correct |
30 ms |
740 KB |
Output is correct |
6 |
Correct |
189 ms |
724 KB |
Output is correct |
7 |
Correct |
145 ms |
732 KB |
Output is correct |
8 |
Correct |
146 ms |
852 KB |
Output is correct |
9 |
Correct |
125 ms |
736 KB |
Output is correct |
10 |
Correct |
80 ms |
732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
35 ms |
724 KB |
Output is correct |
3 |
Correct |
6 ms |
752 KB |
Output is correct |
4 |
Correct |
127 ms |
752 KB |
Output is correct |
5 |
Correct |
30 ms |
740 KB |
Output is correct |
6 |
Correct |
189 ms |
724 KB |
Output is correct |
7 |
Correct |
145 ms |
732 KB |
Output is correct |
8 |
Correct |
146 ms |
852 KB |
Output is correct |
9 |
Correct |
125 ms |
736 KB |
Output is correct |
10 |
Correct |
80 ms |
732 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
34 ms |
724 KB |
Output is correct |
16 |
Correct |
6 ms |
724 KB |
Output is correct |
17 |
Correct |
124 ms |
740 KB |
Output is correct |
18 |
Correct |
30 ms |
724 KB |
Output is correct |
19 |
Correct |
204 ms |
724 KB |
Output is correct |
20 |
Correct |
151 ms |
724 KB |
Output is correct |
21 |
Correct |
143 ms |
728 KB |
Output is correct |
22 |
Correct |
121 ms |
724 KB |
Output is correct |
23 |
Correct |
81 ms |
736 KB |
Output is correct |
24 |
Incorrect |
119 ms |
740 KB |
Output isn't correct |
25 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
35 ms |
724 KB |
Output is correct |
3 |
Correct |
6 ms |
752 KB |
Output is correct |
4 |
Correct |
127 ms |
752 KB |
Output is correct |
5 |
Correct |
30 ms |
740 KB |
Output is correct |
6 |
Correct |
189 ms |
724 KB |
Output is correct |
7 |
Correct |
145 ms |
732 KB |
Output is correct |
8 |
Correct |
146 ms |
852 KB |
Output is correct |
9 |
Correct |
125 ms |
736 KB |
Output is correct |
10 |
Correct |
80 ms |
732 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
35 ms |
732 KB |
Output is correct |
13 |
Correct |
6 ms |
756 KB |
Output is correct |
14 |
Correct |
121 ms |
724 KB |
Output is correct |
15 |
Correct |
29 ms |
724 KB |
Output is correct |
16 |
Correct |
186 ms |
852 KB |
Output is correct |
17 |
Correct |
143 ms |
724 KB |
Output is correct |
18 |
Correct |
145 ms |
724 KB |
Output is correct |
19 |
Correct |
121 ms |
736 KB |
Output is correct |
20 |
Correct |
79 ms |
732 KB |
Output is correct |
21 |
Correct |
1912 ms |
1512 KB |
Output is correct |
22 |
Correct |
1917 ms |
1500 KB |
Output is correct |
23 |
Correct |
101 ms |
1524 KB |
Output is correct |
24 |
Correct |
48 ms |
1520 KB |
Output is correct |
25 |
Correct |
203 ms |
1632 KB |
Output is correct |
26 |
Correct |
1402 ms |
1532 KB |
Output is correct |
27 |
Correct |
2561 ms |
1644 KB |
Output is correct |
28 |
Correct |
1002 ms |
1524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
2386 ms |
1620 KB |
Output is correct |
6 |
Incorrect |
2446 ms |
1548 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 |
35 ms |
724 KB |
Output is correct |
3 |
Correct |
6 ms |
752 KB |
Output is correct |
4 |
Correct |
127 ms |
752 KB |
Output is correct |
5 |
Correct |
30 ms |
740 KB |
Output is correct |
6 |
Correct |
189 ms |
724 KB |
Output is correct |
7 |
Correct |
145 ms |
732 KB |
Output is correct |
8 |
Correct |
146 ms |
852 KB |
Output is correct |
9 |
Correct |
125 ms |
736 KB |
Output is correct |
10 |
Correct |
80 ms |
732 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
35 ms |
732 KB |
Output is correct |
13 |
Correct |
6 ms |
756 KB |
Output is correct |
14 |
Correct |
121 ms |
724 KB |
Output is correct |
15 |
Correct |
29 ms |
724 KB |
Output is correct |
16 |
Correct |
186 ms |
852 KB |
Output is correct |
17 |
Correct |
143 ms |
724 KB |
Output is correct |
18 |
Correct |
145 ms |
724 KB |
Output is correct |
19 |
Correct |
121 ms |
736 KB |
Output is correct |
20 |
Correct |
79 ms |
732 KB |
Output is correct |
21 |
Correct |
1912 ms |
1512 KB |
Output is correct |
22 |
Correct |
1917 ms |
1500 KB |
Output is correct |
23 |
Correct |
101 ms |
1524 KB |
Output is correct |
24 |
Correct |
48 ms |
1520 KB |
Output is correct |
25 |
Correct |
203 ms |
1632 KB |
Output is correct |
26 |
Correct |
1402 ms |
1532 KB |
Output is correct |
27 |
Correct |
2561 ms |
1644 KB |
Output is correct |
28 |
Correct |
1002 ms |
1524 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
33 ms |
724 KB |
Output is correct |
31 |
Correct |
6 ms |
740 KB |
Output is correct |
32 |
Correct |
123 ms |
752 KB |
Output is correct |
33 |
Correct |
29 ms |
724 KB |
Output is correct |
34 |
Correct |
189 ms |
756 KB |
Output is correct |
35 |
Correct |
147 ms |
724 KB |
Output is correct |
36 |
Correct |
147 ms |
740 KB |
Output is correct |
37 |
Correct |
123 ms |
724 KB |
Output is correct |
38 |
Correct |
83 ms |
724 KB |
Output is correct |
39 |
Correct |
1917 ms |
1512 KB |
Output is correct |
40 |
Correct |
1901 ms |
1512 KB |
Output is correct |
41 |
Correct |
100 ms |
1532 KB |
Output is correct |
42 |
Correct |
45 ms |
1512 KB |
Output is correct |
43 |
Correct |
204 ms |
1520 KB |
Output is correct |
44 |
Correct |
1403 ms |
1536 KB |
Output is correct |
45 |
Correct |
2552 ms |
1784 KB |
Output is correct |
46 |
Correct |
998 ms |
1612 KB |
Output is correct |
47 |
Execution timed out |
5057 ms |
8276 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
2386 ms |
1620 KB |
Output is correct |
6 |
Incorrect |
2446 ms |
1548 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 |
35 ms |
724 KB |
Output is correct |
3 |
Correct |
6 ms |
752 KB |
Output is correct |
4 |
Correct |
127 ms |
752 KB |
Output is correct |
5 |
Correct |
30 ms |
740 KB |
Output is correct |
6 |
Correct |
189 ms |
724 KB |
Output is correct |
7 |
Correct |
145 ms |
732 KB |
Output is correct |
8 |
Correct |
146 ms |
852 KB |
Output is correct |
9 |
Correct |
125 ms |
736 KB |
Output is correct |
10 |
Correct |
80 ms |
732 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
35 ms |
732 KB |
Output is correct |
13 |
Correct |
6 ms |
756 KB |
Output is correct |
14 |
Correct |
121 ms |
724 KB |
Output is correct |
15 |
Correct |
29 ms |
724 KB |
Output is correct |
16 |
Correct |
186 ms |
852 KB |
Output is correct |
17 |
Correct |
143 ms |
724 KB |
Output is correct |
18 |
Correct |
145 ms |
724 KB |
Output is correct |
19 |
Correct |
121 ms |
736 KB |
Output is correct |
20 |
Correct |
79 ms |
732 KB |
Output is correct |
21 |
Correct |
1912 ms |
1512 KB |
Output is correct |
22 |
Correct |
1917 ms |
1500 KB |
Output is correct |
23 |
Correct |
101 ms |
1524 KB |
Output is correct |
24 |
Correct |
48 ms |
1520 KB |
Output is correct |
25 |
Correct |
203 ms |
1632 KB |
Output is correct |
26 |
Correct |
1402 ms |
1532 KB |
Output is correct |
27 |
Correct |
2561 ms |
1644 KB |
Output is correct |
28 |
Correct |
1002 ms |
1524 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
33 ms |
724 KB |
Output is correct |
31 |
Correct |
6 ms |
740 KB |
Output is correct |
32 |
Correct |
123 ms |
752 KB |
Output is correct |
33 |
Correct |
29 ms |
724 KB |
Output is correct |
34 |
Correct |
189 ms |
756 KB |
Output is correct |
35 |
Correct |
147 ms |
724 KB |
Output is correct |
36 |
Correct |
147 ms |
740 KB |
Output is correct |
37 |
Correct |
123 ms |
724 KB |
Output is correct |
38 |
Correct |
83 ms |
724 KB |
Output is correct |
39 |
Correct |
1917 ms |
1512 KB |
Output is correct |
40 |
Correct |
1901 ms |
1512 KB |
Output is correct |
41 |
Correct |
100 ms |
1532 KB |
Output is correct |
42 |
Correct |
45 ms |
1512 KB |
Output is correct |
43 |
Correct |
204 ms |
1520 KB |
Output is correct |
44 |
Correct |
1403 ms |
1536 KB |
Output is correct |
45 |
Correct |
2552 ms |
1784 KB |
Output is correct |
46 |
Correct |
998 ms |
1612 KB |
Output is correct |
47 |
Execution timed out |
5057 ms |
8276 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
2386 ms |
1620 KB |
Output is correct |
6 |
Incorrect |
2446 ms |
1548 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |