#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define rrep2(i, a, b) for (int i = (int)(b) - 1; i >= (int)(a); --i)
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
using namespace std;
using ll = long long;
template<class T, class U> bool chmin(T& a, const U& b) { return a > b ? a = b, true : false; }
template<class T, class U> bool chmax(T& a, const U& b) { return a < b ? a = b, true : false; }
using namespace std;
constexpr ll inf = 1e18;
int main() {
int M; cin >> M;
ll L; cin >> L;
vector<ll> A(2 * M + 1);
rep (i, 2 * M + 1) cin >> A[i];
if (L < 0) {
reverse(all(A));
L = -L;
}
ll sml = 0, smr = 0;
rep (i, 2 * M + 1) {
if (i < M) sml += A[i] * (i - M);
if (i > M) smr += A[i] * (i - M);
}
if (L < sml || smr < L) {
puts("impossible");
return 0;
}
vector<pair<int, ll>> B;
rep (i, 2 * M + 1) {
if (i == M) continue;
if (A[i] == 0) continue;
// if (A[i] > M) continue;
B.emplace_back(i - M, A[i]);
}
vector<ll> dp(smr - sml + 1, -inf);
dp[0 - sml] = 0;
for (auto [x, y] : B) {
int ax = abs(x);
vector<ll> memo;
vector<ll> res;
deque<int> st;
rep (i, ax) {
// mod x ごとにスライド最大値の形になる
memo.clear();
for (int j = i; j <= smr - sml; j += ax) {
memo.push_back(dp[j]);
}
res.assign(memo.size(), -inf);
st.clear();
if (x > 0) {
rep (i, memo.size()) memo[i] -= i;
rep (i, res.size()) {
while (!st.empty() && st.front() < i - y) st.pop_front();
while (!st.empty() && memo[st.back()] < memo[i]) st.pop_back();
st.push_back(i);
res[i] = memo[st.front()] + i;
}
}
else {
rep (i, memo.size()) memo[i] += i;
rrep (i, res.size()) {
while (!st.empty() && st.front() > i + y) st.pop_front();
while (!st.empty() && memo[st.back()] < memo[i]) st.pop_back();
st.push_back(i);
res[i] = memo[st.front()] - i;
}
}
rep (j, res.size()) dp[j * ax + i] = res[j];
}
}
if (dp[L - sml] >= 0) {
cout << dp[L - sml] + A[M] << endl;
}
else {
puts("impossible");
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
48 ms |
2372 KB |
Output is correct |
7 |
Correct |
10 ms |
856 KB |
Output is correct |
8 |
Correct |
50 ms |
1972 KB |
Output is correct |
9 |
Correct |
89 ms |
3428 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
48 ms |
2372 KB |
Output is correct |
7 |
Correct |
10 ms |
856 KB |
Output is correct |
8 |
Correct |
50 ms |
1972 KB |
Output is correct |
9 |
Correct |
89 ms |
3428 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
50 ms |
2352 KB |
Output is correct |
18 |
Correct |
9 ms |
860 KB |
Output is correct |
19 |
Correct |
44 ms |
1948 KB |
Output is correct |
20 |
Correct |
90 ms |
3316 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
788 ms |
12356 KB |
Output is correct |
25 |
Correct |
116 ms |
2392 KB |
Output is correct |
26 |
Correct |
1491 ms |
24184 KB |
Output is correct |
27 |
Correct |
1677 ms |
23900 KB |
Output is correct |
28 |
Correct |
0 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
48 ms |
2372 KB |
Output is correct |
7 |
Correct |
10 ms |
856 KB |
Output is correct |
8 |
Correct |
50 ms |
1972 KB |
Output is correct |
9 |
Correct |
89 ms |
3428 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 6 |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
48 ms |
2372 KB |
Output is correct |
7 |
Correct |
10 ms |
856 KB |
Output is correct |
8 |
Correct |
50 ms |
1972 KB |
Output is correct |
9 |
Correct |
89 ms |
3428 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
50 ms |
2352 KB |
Output is correct |
18 |
Correct |
9 ms |
860 KB |
Output is correct |
19 |
Correct |
44 ms |
1948 KB |
Output is correct |
20 |
Correct |
90 ms |
3316 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
788 ms |
12356 KB |
Output is correct |
25 |
Correct |
116 ms |
2392 KB |
Output is correct |
26 |
Correct |
1491 ms |
24184 KB |
Output is correct |
27 |
Correct |
1677 ms |
23900 KB |
Output is correct |
28 |
Correct |
0 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 6 |
32 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
48 ms |
2372 KB |
Output is correct |
7 |
Correct |
10 ms |
856 KB |
Output is correct |
8 |
Correct |
50 ms |
1972 KB |
Output is correct |
9 |
Correct |
89 ms |
3428 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
50 ms |
2352 KB |
Output is correct |
18 |
Correct |
9 ms |
860 KB |
Output is correct |
19 |
Correct |
44 ms |
1948 KB |
Output is correct |
20 |
Correct |
90 ms |
3316 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
788 ms |
12356 KB |
Output is correct |
25 |
Correct |
116 ms |
2392 KB |
Output is correct |
26 |
Correct |
1491 ms |
24184 KB |
Output is correct |
27 |
Correct |
1677 ms |
23900 KB |
Output is correct |
28 |
Correct |
0 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 6 |
32 |
Halted |
0 ms |
0 KB |
- |