#include <bits/stdc++.h>
#define ll long long
const ll inf = 1e18+10LL;
const int MAX = 505005;
using namespace std;
int M;
int dp[2*MAX];
ll L;
// -----------
int l, r;
deque<pair<int,int> > dq;
void initialize(){
l = 1, r = 0;
while(!dq.empty())
dq.pop_back();
}
void insere(int val){
while(!dq.empty() && dq.back().first <= val)
dq.pop_back();
dq.push_back(make_pair(val, ++r));
}
void popa(){
if(dq.front().second == l++)
dq.pop_front();
}
int qry(){
return dq.front().first;
}
// -----------
void makeNegative(int qtd, int val){
val = -val;
for(int i = 0; i < val; i++){
int R = i-val;
int L = i;
initialize();
for(int j = i; j < 2*MAX; j += val){
while(R+val < 2*MAX && ((R-j)/val) < qtd ){
R += val;
insere(dp[R]+R/val);
}
while(L < j){
L += val;
popa();
}
dp[j] = qry()-j/val;
}
}
}
void makePositive(int qtd, int val){
for(int i = 2*MAX-1; i >= 2*MAX-val; i--){
initialize();
int L = i+val, R = i;
for(int j = i; j >= 0; j -= val){
while(L-val >= 0 && (j-L)/val < qtd){
L -= val;
insere(dp[L]-L/val);
}
while(R > j){
R -= val;
popa();
}
dp[j] = qry() + j/val;
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> M >> L;
if( abs(L) >= MAX-5 ){
cout << "impossible\n";
}
for(int i = 0; i < MAX*2; i++){
dp[i] = -2*MAX;
}
dp[MAX] = 0;
for(int i = -M, qtd; i <= M; i++){
cin >> qtd;
if(qtd == 0) continue;
if(i < 0) makeNegative(qtd,i);
else if(i > 0) makePositive(qtd,i);
else{
for(int i = 0; i < 2*MAX; i++)
dp[i] += qtd;
}
}
if(dp[L+MAX] < 0)
cout << "impossible\n";
else cout << dp[L+MAX] << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
105 ms |
4256 KB |
Output is correct |
2 |
Correct |
75 ms |
4252 KB |
Output is correct |
3 |
Correct |
29 ms |
4276 KB |
Output is correct |
4 |
Correct |
201 ms |
4252 KB |
Output is correct |
5 |
Runtime error |
2415 ms |
8352 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
105 ms |
4256 KB |
Output is correct |
2 |
Correct |
75 ms |
4252 KB |
Output is correct |
3 |
Correct |
29 ms |
4276 KB |
Output is correct |
4 |
Correct |
201 ms |
4252 KB |
Output is correct |
5 |
Runtime error |
2415 ms |
8352 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
210 ms |
4252 KB |
Output is correct |
2 |
Runtime error |
685 ms |
11464 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
210 ms |
4252 KB |
Output is correct |
2 |
Runtime error |
685 ms |
11464 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
210 ms |
4252 KB |
Output is correct |
2 |
Runtime error |
685 ms |
11464 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
105 ms |
4256 KB |
Output is correct |
2 |
Correct |
75 ms |
4252 KB |
Output is correct |
3 |
Correct |
29 ms |
4276 KB |
Output is correct |
4 |
Correct |
201 ms |
4252 KB |
Output is correct |
5 |
Runtime error |
2415 ms |
8352 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
210 ms |
4252 KB |
Output is correct |
2 |
Runtime error |
685 ms |
11464 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
105 ms |
4256 KB |
Output is correct |
2 |
Correct |
75 ms |
4252 KB |
Output is correct |
3 |
Correct |
29 ms |
4276 KB |
Output is correct |
4 |
Correct |
201 ms |
4252 KB |
Output is correct |
5 |
Runtime error |
2415 ms |
8352 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
210 ms |
4252 KB |
Output is correct |
2 |
Runtime error |
685 ms |
11464 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
105 ms |
4256 KB |
Output is correct |
2 |
Correct |
75 ms |
4252 KB |
Output is correct |
3 |
Correct |
29 ms |
4276 KB |
Output is correct |
4 |
Correct |
201 ms |
4252 KB |
Output is correct |
5 |
Runtime error |
2415 ms |
8352 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |