// wygzgyw
#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
t=0; char ch=getchar(); int f=1;
while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
do { (t*=10)+=ch-'0'; ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;
}
template <typename T> void write(T t) {
if (t<0) { putchar('-'); write(-t); return; }
if (t>9) write(t/10);
putchar('0'+t%10);
}
template <typename T> void writeln(T t) { write(t); puts(""); }
#define MP make_pair
typedef long long ll;
const int maxn=300*300+10;
const ll INF=1e18;
int m;
ll rub[maxn];
ll L,sum,ans;
ll *a,*b,*now=rub;
ll f[maxn*2],g[maxn*2];
int N=300*300+2;
ll l,r;
void solve(vector<int> &V,int flag) {
for (int i=0;i<V.size();i++) {
g[V[i]]=-INF;
for (int j=max(0LL,i+l);j<=i+r&&j<V.size();j++) {
g[V[i]]=max(g[V[i]],f[V[j]]+(flag?i-j:j-i));
}
}
}
int main() {
//freopen("1.txt","r",stdin);
read(m); read(L);
now+=m,a=now,now+=m+1,now+=m,b=now,now+=m+1;
for (int i=-m;i<=m;i++) read(a[i]),sum+=a[i]*i;
if (sum<L) {
reverse(a-m,a+m+1),sum*=-1,L*=-1;
}
sum=0; for (int i=-m;i<0;i++) sum+=a[i]*i,b[i]=a[i];
if (sum>L) { puts("impossible"); return 0; }
for (int i=0;i<=m;i++) {
if (!i) b[i]=a[i]; else b[i]=min(a[i],(L-sum)/i);
sum+=b[i]*i;
//printf(" %lld\n",b[i]);
}
//printf("%lld %lld\n",sum,L);
for (int i=-m;i<=m;i++) ans+=b[i];
//printf("> %lld\n",ans);
//N=10;
for (int i=-N;i<=N;i++) f[i+N]=-INF;
f[N]=0;
for (int i=-m;i<=m;i++) if (i!=0) {
if (i<0) l=0,r=a[i]; else l=-b[i],r=a[i]-b[i];
//printf(" %d %lld %lld\n",i,abs(i)*l,abs(i)*r);
l*=-1,r*=-1,swap(l,r);
vector<int> V1;
for (int j=0;j<abs(i);j++) {
V1.clear();
for (int k=j;k>=-N;k-=abs(i)) V1.push_back(k+N);
reverse(V1.begin(),V1.end());
for (int k=j+abs(i);k<N;k+=abs(i)) V1.push_back(k+N);
solve(V1,(i>0?1:0));
}
for (int j=0;j<N*2;j++) f[j]=g[j];
}
//printf("> %lld %lld\n",L-sum,f[L-sum+N]);
if (f[L-sum+N]+ans>=0) printf("%lld\n",f[L-sum+N]+ans);
else puts("impossible");
return 0;
}
/*
0. Enough array size? Enough array size? Enough array size? Integer overflow?
1. Think TWICE, Code ONCE!
Are there any counterexamples to your algo?
2. Be careful about the BOUNDARIES!
N=1? P=1? Something about 0?
3. Do not make STUPID MISTAKES!
Time complexity? Memory usage? Precision error?
*/
Compilation message
vault.cpp: In function 'void solve(std::vector<int>&, int)':
vault.cpp:27:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for (int i=0;i<V.size();i++) {
| ~^~~~~~~~~
vault.cpp:29:42: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | for (int j=max(0LL,i+l);j<=i+r&&j<V.size();j++) {
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
4784 KB |
Output is correct |
2 |
Correct |
12 ms |
4832 KB |
Output is correct |
3 |
Correct |
7 ms |
4868 KB |
Output is correct |
4 |
Correct |
39 ms |
4808 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
802 ms |
4932 KB |
Output is correct |
7 |
Correct |
359 ms |
4788 KB |
Output is correct |
8 |
Correct |
740 ms |
4764 KB |
Output is correct |
9 |
Correct |
1282 ms |
4956 KB |
Output is correct |
10 |
Correct |
171 ms |
4808 KB |
Output is correct |
11 |
Correct |
152 ms |
4816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
4784 KB |
Output is correct |
2 |
Correct |
12 ms |
4832 KB |
Output is correct |
3 |
Correct |
7 ms |
4868 KB |
Output is correct |
4 |
Correct |
39 ms |
4808 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
802 ms |
4932 KB |
Output is correct |
7 |
Correct |
359 ms |
4788 KB |
Output is correct |
8 |
Correct |
740 ms |
4764 KB |
Output is correct |
9 |
Correct |
1282 ms |
4956 KB |
Output is correct |
10 |
Correct |
171 ms |
4808 KB |
Output is correct |
11 |
Correct |
152 ms |
4816 KB |
Output is correct |
12 |
Correct |
10 ms |
4784 KB |
Output is correct |
13 |
Correct |
13 ms |
4812 KB |
Output is correct |
14 |
Correct |
6 ms |
4868 KB |
Output is correct |
15 |
Correct |
47 ms |
4860 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
749 ms |
4836 KB |
Output is correct |
18 |
Correct |
393 ms |
4808 KB |
Output is correct |
19 |
Correct |
685 ms |
4764 KB |
Output is correct |
20 |
Correct |
1337 ms |
4872 KB |
Output is correct |
21 |
Correct |
162 ms |
4808 KB |
Output is correct |
22 |
Correct |
139 ms |
4820 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
2522 ms |
4920 KB |
Output is correct |
25 |
Correct |
1075 ms |
4828 KB |
Output is correct |
26 |
Correct |
4658 ms |
4828 KB |
Output is correct |
27 |
Correct |
4642 ms |
4772 KB |
Output is correct |
28 |
Correct |
284 ms |
4792 KB |
Output is correct |
29 |
Correct |
300 ms |
4800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
4812 KB |
Output is correct |
2 |
Execution timed out |
5036 ms |
4880 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
4812 KB |
Output is correct |
2 |
Execution timed out |
5036 ms |
4880 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
4812 KB |
Output is correct |
2 |
Execution timed out |
5036 ms |
4880 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
4784 KB |
Output is correct |
2 |
Correct |
12 ms |
4832 KB |
Output is correct |
3 |
Correct |
7 ms |
4868 KB |
Output is correct |
4 |
Correct |
39 ms |
4808 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
802 ms |
4932 KB |
Output is correct |
7 |
Correct |
359 ms |
4788 KB |
Output is correct |
8 |
Correct |
740 ms |
4764 KB |
Output is correct |
9 |
Correct |
1282 ms |
4956 KB |
Output is correct |
10 |
Correct |
171 ms |
4808 KB |
Output is correct |
11 |
Correct |
152 ms |
4816 KB |
Output is correct |
12 |
Correct |
37 ms |
4812 KB |
Output is correct |
13 |
Execution timed out |
5036 ms |
4880 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
4812 KB |
Output is correct |
2 |
Execution timed out |
5036 ms |
4880 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
4784 KB |
Output is correct |
2 |
Correct |
12 ms |
4832 KB |
Output is correct |
3 |
Correct |
7 ms |
4868 KB |
Output is correct |
4 |
Correct |
39 ms |
4808 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
802 ms |
4932 KB |
Output is correct |
7 |
Correct |
359 ms |
4788 KB |
Output is correct |
8 |
Correct |
740 ms |
4764 KB |
Output is correct |
9 |
Correct |
1282 ms |
4956 KB |
Output is correct |
10 |
Correct |
171 ms |
4808 KB |
Output is correct |
11 |
Correct |
152 ms |
4816 KB |
Output is correct |
12 |
Correct |
10 ms |
4784 KB |
Output is correct |
13 |
Correct |
13 ms |
4812 KB |
Output is correct |
14 |
Correct |
6 ms |
4868 KB |
Output is correct |
15 |
Correct |
47 ms |
4860 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
749 ms |
4836 KB |
Output is correct |
18 |
Correct |
393 ms |
4808 KB |
Output is correct |
19 |
Correct |
685 ms |
4764 KB |
Output is correct |
20 |
Correct |
1337 ms |
4872 KB |
Output is correct |
21 |
Correct |
162 ms |
4808 KB |
Output is correct |
22 |
Correct |
139 ms |
4820 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
2522 ms |
4920 KB |
Output is correct |
25 |
Correct |
1075 ms |
4828 KB |
Output is correct |
26 |
Correct |
4658 ms |
4828 KB |
Output is correct |
27 |
Correct |
4642 ms |
4772 KB |
Output is correct |
28 |
Correct |
284 ms |
4792 KB |
Output is correct |
29 |
Correct |
300 ms |
4800 KB |
Output is correct |
30 |
Correct |
37 ms |
4812 KB |
Output is correct |
31 |
Execution timed out |
5036 ms |
4880 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
4812 KB |
Output is correct |
2 |
Execution timed out |
5036 ms |
4880 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
4784 KB |
Output is correct |
2 |
Correct |
12 ms |
4832 KB |
Output is correct |
3 |
Correct |
7 ms |
4868 KB |
Output is correct |
4 |
Correct |
39 ms |
4808 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
802 ms |
4932 KB |
Output is correct |
7 |
Correct |
359 ms |
4788 KB |
Output is correct |
8 |
Correct |
740 ms |
4764 KB |
Output is correct |
9 |
Correct |
1282 ms |
4956 KB |
Output is correct |
10 |
Correct |
171 ms |
4808 KB |
Output is correct |
11 |
Correct |
152 ms |
4816 KB |
Output is correct |
12 |
Correct |
10 ms |
4784 KB |
Output is correct |
13 |
Correct |
13 ms |
4812 KB |
Output is correct |
14 |
Correct |
6 ms |
4868 KB |
Output is correct |
15 |
Correct |
47 ms |
4860 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
749 ms |
4836 KB |
Output is correct |
18 |
Correct |
393 ms |
4808 KB |
Output is correct |
19 |
Correct |
685 ms |
4764 KB |
Output is correct |
20 |
Correct |
1337 ms |
4872 KB |
Output is correct |
21 |
Correct |
162 ms |
4808 KB |
Output is correct |
22 |
Correct |
139 ms |
4820 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
2522 ms |
4920 KB |
Output is correct |
25 |
Correct |
1075 ms |
4828 KB |
Output is correct |
26 |
Correct |
4658 ms |
4828 KB |
Output is correct |
27 |
Correct |
4642 ms |
4772 KB |
Output is correct |
28 |
Correct |
284 ms |
4792 KB |
Output is correct |
29 |
Correct |
300 ms |
4800 KB |
Output is correct |
30 |
Correct |
37 ms |
4812 KB |
Output is correct |
31 |
Execution timed out |
5036 ms |
4880 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |