#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;
llo su3=0;
void apply(int l,int r){
for(int j=l;j<=r;j++){
llo ax=j*min(it[j],n);
su3+=ax;
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++;
}
}
}
}
void remove(int l,int r){
for(int j=l;j<=r;j++){
llo ax=j*min(it[j],n);
su3-=ax;
}
}
llo pp;
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);
llo su4=su3;
solve(mid+1,r);
remove(l,mid);
for(int ii=0;ii<dp2.size();ii++){
dp[ii]=dp2[ii];
}
for(int ii=su3+1;ii<=su4;ii++){
dp[ii]=-1;
}
apply(mid+1,r);
su4=su3;
solve(l,mid);
remove(mid+1,r);
for(int ii=0;ii<dp2.size();ii++){
dp[ii]=dp2[ii];
}
for(int ii=su3+1;ii<=su4;ii++){
dp[ii]=-1;
}
}
}
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=0;
if(su<dd){
cout<<"impossible"<<endl;
return 0;
}
llo cur=su-dd;
pp=cur;
it[0]=0;
for(int i=0;i<=n*n*n;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;
}
Compilation message
vault.cpp: In function 'void solve(int, int)':
vault.cpp:119:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
119 | for(int ii=0;ii<dp2.size();ii++){
| ~~^~~~~~~~~~~
vault.cpp:129:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
129 | for(int ii=0;ii<dp2.size();ii++){
| ~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 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 |
96 ms |
2896 KB |
Output is correct |
6 |
Incorrect |
95 ms |
3332 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 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 |
96 ms |
2896 KB |
Output is correct |
6 |
Incorrect |
95 ms |
3332 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
12 ms |
1152 KB |
Output is correct |
3 |
Correct |
5 ms |
852 KB |
Output is correct |
4 |
Correct |
15 ms |
1156 KB |
Output is correct |
5 |
Correct |
20 ms |
1288 KB |
Output is correct |
6 |
Correct |
12 ms |
1000 KB |
Output is correct |
7 |
Correct |
3 ms |
852 KB |
Output is correct |
8 |
Correct |
2 ms |
724 KB |
Output is correct |
9 |
Correct |
5 ms |
852 KB |
Output is correct |
10 |
Correct |
9 ms |
988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
12 ms |
1152 KB |
Output is correct |
3 |
Correct |
5 ms |
852 KB |
Output is correct |
4 |
Correct |
15 ms |
1156 KB |
Output is correct |
5 |
Correct |
20 ms |
1288 KB |
Output is correct |
6 |
Correct |
12 ms |
1000 KB |
Output is correct |
7 |
Correct |
3 ms |
852 KB |
Output is correct |
8 |
Correct |
2 ms |
724 KB |
Output is correct |
9 |
Correct |
5 ms |
852 KB |
Output is correct |
10 |
Correct |
9 ms |
988 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
12 ms |
1184 KB |
Output is correct |
16 |
Correct |
6 ms |
852 KB |
Output is correct |
17 |
Correct |
15 ms |
1220 KB |
Output is correct |
18 |
Correct |
19 ms |
1264 KB |
Output is correct |
19 |
Correct |
12 ms |
1016 KB |
Output is correct |
20 |
Correct |
3 ms |
724 KB |
Output is correct |
21 |
Correct |
2 ms |
724 KB |
Output is correct |
22 |
Correct |
5 ms |
900 KB |
Output is correct |
23 |
Correct |
8 ms |
988 KB |
Output is correct |
24 |
Incorrect |
14 ms |
1136 KB |
Output isn't correct |
25 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
12 ms |
1152 KB |
Output is correct |
3 |
Correct |
5 ms |
852 KB |
Output is correct |
4 |
Correct |
15 ms |
1156 KB |
Output is correct |
5 |
Correct |
20 ms |
1288 KB |
Output is correct |
6 |
Correct |
12 ms |
1000 KB |
Output is correct |
7 |
Correct |
3 ms |
852 KB |
Output is correct |
8 |
Correct |
2 ms |
724 KB |
Output is correct |
9 |
Correct |
5 ms |
852 KB |
Output is correct |
10 |
Correct |
9 ms |
988 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
13 ms |
1136 KB |
Output is correct |
13 |
Correct |
7 ms |
852 KB |
Output is correct |
14 |
Correct |
17 ms |
1212 KB |
Output is correct |
15 |
Correct |
22 ms |
1200 KB |
Output is correct |
16 |
Correct |
15 ms |
1016 KB |
Output is correct |
17 |
Correct |
2 ms |
724 KB |
Output is correct |
18 |
Correct |
2 ms |
736 KB |
Output is correct |
19 |
Correct |
5 ms |
852 KB |
Output is correct |
20 |
Correct |
9 ms |
980 KB |
Output is correct |
21 |
Correct |
10 ms |
1784 KB |
Output is correct |
22 |
Correct |
9 ms |
1620 KB |
Output is correct |
23 |
Correct |
116 ms |
3484 KB |
Output is correct |
24 |
Correct |
71 ms |
2748 KB |
Output is correct |
25 |
Correct |
140 ms |
3832 KB |
Output is correct |
26 |
Correct |
167 ms |
4252 KB |
Output is correct |
27 |
Correct |
133 ms |
3740 KB |
Output is correct |
28 |
Correct |
88 ms |
2772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 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 |
96 ms |
2896 KB |
Output is correct |
6 |
Incorrect |
95 ms |
3332 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
12 ms |
1152 KB |
Output is correct |
3 |
Correct |
5 ms |
852 KB |
Output is correct |
4 |
Correct |
15 ms |
1156 KB |
Output is correct |
5 |
Correct |
20 ms |
1288 KB |
Output is correct |
6 |
Correct |
12 ms |
1000 KB |
Output is correct |
7 |
Correct |
3 ms |
852 KB |
Output is correct |
8 |
Correct |
2 ms |
724 KB |
Output is correct |
9 |
Correct |
5 ms |
852 KB |
Output is correct |
10 |
Correct |
9 ms |
988 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
13 ms |
1136 KB |
Output is correct |
13 |
Correct |
7 ms |
852 KB |
Output is correct |
14 |
Correct |
17 ms |
1212 KB |
Output is correct |
15 |
Correct |
22 ms |
1200 KB |
Output is correct |
16 |
Correct |
15 ms |
1016 KB |
Output is correct |
17 |
Correct |
2 ms |
724 KB |
Output is correct |
18 |
Correct |
2 ms |
736 KB |
Output is correct |
19 |
Correct |
5 ms |
852 KB |
Output is correct |
20 |
Correct |
9 ms |
980 KB |
Output is correct |
21 |
Correct |
10 ms |
1784 KB |
Output is correct |
22 |
Correct |
9 ms |
1620 KB |
Output is correct |
23 |
Correct |
116 ms |
3484 KB |
Output is correct |
24 |
Correct |
71 ms |
2748 KB |
Output is correct |
25 |
Correct |
140 ms |
3832 KB |
Output is correct |
26 |
Correct |
167 ms |
4252 KB |
Output is correct |
27 |
Correct |
133 ms |
3740 KB |
Output is correct |
28 |
Correct |
88 ms |
2772 KB |
Output is correct |
29 |
Correct |
1 ms |
512 KB |
Output is correct |
30 |
Correct |
14 ms |
1160 KB |
Output is correct |
31 |
Correct |
5 ms |
852 KB |
Output is correct |
32 |
Correct |
15 ms |
1216 KB |
Output is correct |
33 |
Correct |
19 ms |
1200 KB |
Output is correct |
34 |
Correct |
11 ms |
1004 KB |
Output is correct |
35 |
Correct |
2 ms |
724 KB |
Output is correct |
36 |
Correct |
2 ms |
724 KB |
Output is correct |
37 |
Correct |
5 ms |
892 KB |
Output is correct |
38 |
Correct |
8 ms |
988 KB |
Output is correct |
39 |
Correct |
9 ms |
1784 KB |
Output is correct |
40 |
Correct |
9 ms |
1676 KB |
Output is correct |
41 |
Correct |
115 ms |
3548 KB |
Output is correct |
42 |
Correct |
73 ms |
2756 KB |
Output is correct |
43 |
Correct |
119 ms |
3724 KB |
Output is correct |
44 |
Correct |
171 ms |
4248 KB |
Output is correct |
45 |
Correct |
133 ms |
3752 KB |
Output is correct |
46 |
Correct |
85 ms |
2876 KB |
Output is correct |
47 |
Correct |
90 ms |
9636 KB |
Output is correct |
48 |
Correct |
87 ms |
9452 KB |
Output is correct |
49 |
Correct |
2155 ms |
26620 KB |
Output is correct |
50 |
Correct |
1071 ms |
17768 KB |
Output is correct |
51 |
Correct |
2417 ms |
29196 KB |
Output is correct |
52 |
Correct |
3379 ms |
34652 KB |
Output is correct |
53 |
Correct |
2200 ms |
26632 KB |
Output is correct |
54 |
Correct |
1495 ms |
20004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 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 |
96 ms |
2896 KB |
Output is correct |
6 |
Incorrect |
95 ms |
3332 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
12 ms |
1152 KB |
Output is correct |
3 |
Correct |
5 ms |
852 KB |
Output is correct |
4 |
Correct |
15 ms |
1156 KB |
Output is correct |
5 |
Correct |
20 ms |
1288 KB |
Output is correct |
6 |
Correct |
12 ms |
1000 KB |
Output is correct |
7 |
Correct |
3 ms |
852 KB |
Output is correct |
8 |
Correct |
2 ms |
724 KB |
Output is correct |
9 |
Correct |
5 ms |
852 KB |
Output is correct |
10 |
Correct |
9 ms |
988 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
13 ms |
1136 KB |
Output is correct |
13 |
Correct |
7 ms |
852 KB |
Output is correct |
14 |
Correct |
17 ms |
1212 KB |
Output is correct |
15 |
Correct |
22 ms |
1200 KB |
Output is correct |
16 |
Correct |
15 ms |
1016 KB |
Output is correct |
17 |
Correct |
2 ms |
724 KB |
Output is correct |
18 |
Correct |
2 ms |
736 KB |
Output is correct |
19 |
Correct |
5 ms |
852 KB |
Output is correct |
20 |
Correct |
9 ms |
980 KB |
Output is correct |
21 |
Correct |
10 ms |
1784 KB |
Output is correct |
22 |
Correct |
9 ms |
1620 KB |
Output is correct |
23 |
Correct |
116 ms |
3484 KB |
Output is correct |
24 |
Correct |
71 ms |
2748 KB |
Output is correct |
25 |
Correct |
140 ms |
3832 KB |
Output is correct |
26 |
Correct |
167 ms |
4252 KB |
Output is correct |
27 |
Correct |
133 ms |
3740 KB |
Output is correct |
28 |
Correct |
88 ms |
2772 KB |
Output is correct |
29 |
Correct |
1 ms |
512 KB |
Output is correct |
30 |
Correct |
14 ms |
1160 KB |
Output is correct |
31 |
Correct |
5 ms |
852 KB |
Output is correct |
32 |
Correct |
15 ms |
1216 KB |
Output is correct |
33 |
Correct |
19 ms |
1200 KB |
Output is correct |
34 |
Correct |
11 ms |
1004 KB |
Output is correct |
35 |
Correct |
2 ms |
724 KB |
Output is correct |
36 |
Correct |
2 ms |
724 KB |
Output is correct |
37 |
Correct |
5 ms |
892 KB |
Output is correct |
38 |
Correct |
8 ms |
988 KB |
Output is correct |
39 |
Correct |
9 ms |
1784 KB |
Output is correct |
40 |
Correct |
9 ms |
1676 KB |
Output is correct |
41 |
Correct |
115 ms |
3548 KB |
Output is correct |
42 |
Correct |
73 ms |
2756 KB |
Output is correct |
43 |
Correct |
119 ms |
3724 KB |
Output is correct |
44 |
Correct |
171 ms |
4248 KB |
Output is correct |
45 |
Correct |
133 ms |
3752 KB |
Output is correct |
46 |
Correct |
85 ms |
2876 KB |
Output is correct |
47 |
Correct |
90 ms |
9636 KB |
Output is correct |
48 |
Correct |
87 ms |
9452 KB |
Output is correct |
49 |
Correct |
2155 ms |
26620 KB |
Output is correct |
50 |
Correct |
1071 ms |
17768 KB |
Output is correct |
51 |
Correct |
2417 ms |
29196 KB |
Output is correct |
52 |
Correct |
3379 ms |
34652 KB |
Output is correct |
53 |
Correct |
2200 ms |
26632 KB |
Output is correct |
54 |
Correct |
1495 ms |
20004 KB |
Output is correct |
55 |
Correct |
1 ms |
480 KB |
Output is correct |
56 |
Correct |
13 ms |
1204 KB |
Output is correct |
57 |
Correct |
5 ms |
896 KB |
Output is correct |
58 |
Correct |
15 ms |
1268 KB |
Output is correct |
59 |
Correct |
19 ms |
1204 KB |
Output is correct |
60 |
Correct |
17 ms |
1060 KB |
Output is correct |
61 |
Correct |
2 ms |
724 KB |
Output is correct |
62 |
Correct |
2 ms |
724 KB |
Output is correct |
63 |
Correct |
5 ms |
796 KB |
Output is correct |
64 |
Correct |
9 ms |
988 KB |
Output is correct |
65 |
Correct |
10 ms |
1780 KB |
Output is correct |
66 |
Correct |
9 ms |
1620 KB |
Output is correct |
67 |
Correct |
113 ms |
3492 KB |
Output is correct |
68 |
Correct |
71 ms |
2748 KB |
Output is correct |
69 |
Correct |
124 ms |
3788 KB |
Output is correct |
70 |
Correct |
167 ms |
4252 KB |
Output is correct |
71 |
Correct |
129 ms |
3756 KB |
Output is correct |
72 |
Correct |
98 ms |
2748 KB |
Output is correct |
73 |
Correct |
107 ms |
9552 KB |
Output is correct |
74 |
Correct |
84 ms |
9452 KB |
Output is correct |
75 |
Correct |
2125 ms |
26656 KB |
Output is correct |
76 |
Correct |
1055 ms |
17884 KB |
Output is correct |
77 |
Correct |
2478 ms |
28916 KB |
Output is correct |
78 |
Correct |
3295 ms |
34564 KB |
Output is correct |
79 |
Correct |
2157 ms |
26440 KB |
Output is correct |
80 |
Correct |
1484 ms |
20120 KB |
Output is correct |
81 |
Runtime error |
1 ms |
852 KB |
Execution killed with signal 11 |
82 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 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 |
96 ms |
2896 KB |
Output is correct |
6 |
Incorrect |
95 ms |
3332 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |