#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
typedef vector<pii> vpii;
typedef vector<ll> vi;
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
void resetPQ(priority_queue<pii, vpii> &q, int cut)
{
priority_queue<pii, vpii> qq;
while (!q.empty())
{
pii tmp = q.top();
q.pop();
if (tmp.second >= cut)
qq.push(tmp);
}
swap(q, qq);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
ll M, L;
cin >> M >> L;
vi A(2 * M + 1);
for (int i = 0; i < sz(A); ++i)
cin >> A[i];
int an = accumulate(A.begin(), A.begin() + M, 0);
int ap = accumulate(A.begin() + M + 1, A.begin() + 2 * M + 1, 0);
int MAXSIZE = 2 * M * max(an, ap);
vi dp(MAXSIZE, -1);
ll center = MAXSIZE / 2;
dp[center] = A[M];
for (int foo = 0; foo < 2; ++foo)
{
reverse(all(A)), reverse(all(dp));
for (ll m = 1; m <= M; ++m)
{
ll maxNum = A[m + M];
for (ll r = 0; r < m; ++r)
{
priority_queue<pii, vpii> q;
for (int i = 0; m * i + r < sz(dp); ++i)
{
int idx = m * i + r;
while (!q.empty() && q.top().second < idx - maxNum * m)
q.pop();
if (sz(q) > 2 * m)
resetPQ(q, idx - maxNum * m);
if (dp[idx] != -1)
q.push({dp[idx] - i, idx});
if (!q.empty())
dp[idx] = i + q.top().first;
}
}
}
}
ll ans = -1;
if (0 <= center + L && center + L < sz(dp))
ans = dp[center + L];
if (ans == -1)
cout << "impossible\n";
else
cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
441 ms |
1364 KB |
Output is correct |
6 |
Correct |
616 ms |
1364 KB |
Output is correct |
7 |
Correct |
113 ms |
724 KB |
Output is correct |
8 |
Correct |
430 ms |
1316 KB |
Output is correct |
9 |
Correct |
2119 ms |
2184 KB |
Output is correct |
10 |
Correct |
3 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
441 ms |
1364 KB |
Output is correct |
6 |
Correct |
616 ms |
1364 KB |
Output is correct |
7 |
Correct |
113 ms |
724 KB |
Output is correct |
8 |
Correct |
430 ms |
1316 KB |
Output is correct |
9 |
Correct |
2119 ms |
2184 KB |
Output is correct |
10 |
Correct |
3 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
450 ms |
1236 KB |
Output is correct |
17 |
Correct |
640 ms |
1484 KB |
Output is correct |
18 |
Correct |
116 ms |
724 KB |
Output is correct |
19 |
Correct |
419 ms |
1312 KB |
Output is correct |
20 |
Correct |
2115 ms |
2172 KB |
Output is correct |
21 |
Correct |
3 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Execution timed out |
5043 ms |
8724 KB |
Time limit exceeded |
24 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
441 ms |
1364 KB |
Output is correct |
6 |
Correct |
616 ms |
1364 KB |
Output is correct |
7 |
Correct |
113 ms |
724 KB |
Output is correct |
8 |
Correct |
430 ms |
1316 KB |
Output is correct |
9 |
Correct |
2119 ms |
2184 KB |
Output is correct |
10 |
Correct |
3 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
441 ms |
1364 KB |
Output is correct |
6 |
Correct |
616 ms |
1364 KB |
Output is correct |
7 |
Correct |
113 ms |
724 KB |
Output is correct |
8 |
Correct |
430 ms |
1316 KB |
Output is correct |
9 |
Correct |
2119 ms |
2184 KB |
Output is correct |
10 |
Correct |
3 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
450 ms |
1236 KB |
Output is correct |
17 |
Correct |
640 ms |
1484 KB |
Output is correct |
18 |
Correct |
116 ms |
724 KB |
Output is correct |
19 |
Correct |
419 ms |
1312 KB |
Output is correct |
20 |
Correct |
2115 ms |
2172 KB |
Output is correct |
21 |
Correct |
3 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Execution timed out |
5043 ms |
8724 KB |
Time limit exceeded |
24 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
441 ms |
1364 KB |
Output is correct |
6 |
Correct |
616 ms |
1364 KB |
Output is correct |
7 |
Correct |
113 ms |
724 KB |
Output is correct |
8 |
Correct |
430 ms |
1316 KB |
Output is correct |
9 |
Correct |
2119 ms |
2184 KB |
Output is correct |
10 |
Correct |
3 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
450 ms |
1236 KB |
Output is correct |
17 |
Correct |
640 ms |
1484 KB |
Output is correct |
18 |
Correct |
116 ms |
724 KB |
Output is correct |
19 |
Correct |
419 ms |
1312 KB |
Output is correct |
20 |
Correct |
2115 ms |
2172 KB |
Output is correct |
21 |
Correct |
3 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Execution timed out |
5043 ms |
8724 KB |
Time limit exceeded |
24 |
Halted |
0 ms |
0 KB |
- |