#include <bits/stdc++.h>
using namespace std;
template <typename T> class negarr {
private:
vector<T> data;
int n;
public:
negarr(int n) : n(n) { data.resize(2 * n + 1, 0); }
void reset() { fill(data.begin(), data.end(), -1000000000); }
inline T &operator[](int i) { return data[i + n]; }
};
class pstate {
public:
int x, y;
pstate(int x, int y) : x(x), y(y) {}
friend bool operator<(const pstate &a, const pstate &b) {
return a.x + a.y < b.x + b.y;
}
};
class nstate {
public:
int x, y;
nstate(int x, int y) : x(x), y(y) {}
friend bool operator<(const nstate &a, const nstate &b) {
return a.y - a.x < b.y - b.x;
}
};
int main() {
int m;
long long l;
cin >> m >> l;
const int BOUND = m * (m + 1) / 2 * 100;
negarr<int> a(m), knapsack(BOUND);
if (l > BOUND || l < -BOUND) {
printf("impossible\n");
return 0;
}
knapsack.reset();
knapsack[0] = 0;
for (int i = -m; i <= m; i++) {
cin >> a[i];
if (i == 0) {
for (int id = -BOUND; id <= BOUND; id++) {
knapsack[id] += a[i];
}
} else if (abs(i) > 15) {
negarr<int> tmp(BOUND);
tmp.reset();
if (i > 0) {
vector<vector<nstate>> pq(i);
for (int id = -BOUND; id <= BOUND; id++) {
int mod = id % i;
if (mod < 0)
mod += i;
pq[mod].reserve(2 * BOUND / i + 5);
}
for (int id = -BOUND; id <= BOUND; id++) {
int mod = id % i;
if (mod < 0)
mod += i;
int xx = (id + BOUND) / i;
while (!pq[mod].empty() && xx - pq[mod].front().x > a[i]) {
pop_heap(pq[mod].begin(), pq[mod].end());
pq[mod].pop_back();
}
while (!pq[mod].empty() &&
pq[mod].front() < nstate(xx, knapsack[id])) {
pop_heap(pq[mod].begin(), pq[mod].end());
pq[mod].pop_back();
}
pq[mod].push_back({xx, knapsack[id]});
push_heap(pq[mod].begin(), pq[mod].end());
if (!pq[mod].empty()) {
nstate t = pq[mod].front();
tmp[id] = max(tmp[id], t.y - t.x + xx);
}
}
} else if (i < 0) {
vector<vector<pstate>> pq(-i);
for (int id = -BOUND; id <= BOUND; id++) {
int mod = id % (-i);
if (mod < 0)
mod += (-i);
pq[mod].reserve(2 * BOUND / (-i) + 5);
}
for (int id = BOUND; id >= -BOUND; id--) {
int mod = id % (-i);
if (mod < 0)
mod += (-i);
int xx = (id + BOUND) / (-i);
while (!pq[mod].empty() && pq[mod].front().x - xx > a[i]) {
pop_heap(pq[mod].begin(), pq[mod].end());
pq[mod].pop_back();
}
while (!pq[mod].empty() &&
pq[mod].front() < pstate(xx, knapsack[id])) {
pop_heap(pq[mod].begin(), pq[mod].end());
pq[mod].pop_back();
}
pq[mod].push_back({xx, knapsack[id]});
push_heap(pq[mod].begin(), pq[mod].end());
if (!pq[mod].empty()) {
pstate t = pq[mod].front();
knapsack[id] = max(knapsack[id], t.y + t.x - xx);
}
}
}
for (int id = -BOUND; id <= BOUND; id++) {
knapsack[id] = max(knapsack[id], tmp[id]);
}
} else {
for (int rep = 0; rep < a[i]; rep++) {
negarr<int> tmp(BOUND);
tmp.reset();
for (int id = -BOUND; id <= BOUND; id++) {
if (-BOUND <= id + i && id + i <= BOUND)
tmp[id + i] = max(tmp[id + i], knapsack[id] + 1);
}
for (int id = -BOUND; id <= BOUND; id++) {
knapsack[id] = max(knapsack[id], tmp[id]);
}
}
}
}
if (knapsack[l] >= 0)
printf("%d\n", knapsack[l]);
else
printf("impossible\n");
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
7 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
1236 KB |
Output is correct |
6 |
Correct |
2793 ms |
3244 KB |
Output is correct |
7 |
Correct |
1388 ms |
3264 KB |
Output is correct |
8 |
Correct |
2755 ms |
3336 KB |
Output is correct |
9 |
Correct |
4232 ms |
3444 KB |
Output is correct |
10 |
Correct |
679 ms |
3276 KB |
Output is correct |
11 |
Correct |
691 ms |
3380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
7 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
1236 KB |
Output is correct |
6 |
Correct |
2793 ms |
3244 KB |
Output is correct |
7 |
Correct |
1388 ms |
3264 KB |
Output is correct |
8 |
Correct |
2755 ms |
3336 KB |
Output is correct |
9 |
Correct |
4232 ms |
3444 KB |
Output is correct |
10 |
Correct |
679 ms |
3276 KB |
Output is correct |
11 |
Correct |
691 ms |
3380 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 |
5 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
1236 KB |
Output is correct |
17 |
Correct |
2842 ms |
3268 KB |
Output is correct |
18 |
Correct |
1395 ms |
3260 KB |
Output is correct |
19 |
Correct |
2752 ms |
3288 KB |
Output is correct |
20 |
Correct |
4203 ms |
3244 KB |
Output is correct |
21 |
Correct |
677 ms |
3200 KB |
Output is correct |
22 |
Correct |
691 ms |
3220 KB |
Output is correct |
23 |
Correct |
2 ms |
4180 KB |
Output is correct |
24 |
Execution timed out |
5075 ms |
8764 KB |
Time limit exceeded |
25 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
384 KB |
Output is correct |
2 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
384 KB |
Output is correct |
2 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
384 KB |
Output is correct |
2 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
7 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
1236 KB |
Output is correct |
6 |
Correct |
2793 ms |
3244 KB |
Output is correct |
7 |
Correct |
1388 ms |
3264 KB |
Output is correct |
8 |
Correct |
2755 ms |
3336 KB |
Output is correct |
9 |
Correct |
4232 ms |
3444 KB |
Output is correct |
10 |
Correct |
679 ms |
3276 KB |
Output is correct |
11 |
Correct |
691 ms |
3380 KB |
Output is correct |
12 |
Correct |
6 ms |
384 KB |
Output is correct |
13 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
384 KB |
Output is correct |
2 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
7 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
1236 KB |
Output is correct |
6 |
Correct |
2793 ms |
3244 KB |
Output is correct |
7 |
Correct |
1388 ms |
3264 KB |
Output is correct |
8 |
Correct |
2755 ms |
3336 KB |
Output is correct |
9 |
Correct |
4232 ms |
3444 KB |
Output is correct |
10 |
Correct |
679 ms |
3276 KB |
Output is correct |
11 |
Correct |
691 ms |
3380 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 |
5 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
1236 KB |
Output is correct |
17 |
Correct |
2842 ms |
3268 KB |
Output is correct |
18 |
Correct |
1395 ms |
3260 KB |
Output is correct |
19 |
Correct |
2752 ms |
3288 KB |
Output is correct |
20 |
Correct |
4203 ms |
3244 KB |
Output is correct |
21 |
Correct |
677 ms |
3200 KB |
Output is correct |
22 |
Correct |
691 ms |
3220 KB |
Output is correct |
23 |
Correct |
2 ms |
4180 KB |
Output is correct |
24 |
Execution timed out |
5075 ms |
8764 KB |
Time limit exceeded |
25 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
384 KB |
Output is correct |
2 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
7 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
1236 KB |
Output is correct |
6 |
Correct |
2793 ms |
3244 KB |
Output is correct |
7 |
Correct |
1388 ms |
3264 KB |
Output is correct |
8 |
Correct |
2755 ms |
3336 KB |
Output is correct |
9 |
Correct |
4232 ms |
3444 KB |
Output is correct |
10 |
Correct |
679 ms |
3276 KB |
Output is correct |
11 |
Correct |
691 ms |
3380 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 |
5 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
1236 KB |
Output is correct |
17 |
Correct |
2842 ms |
3268 KB |
Output is correct |
18 |
Correct |
1395 ms |
3260 KB |
Output is correct |
19 |
Correct |
2752 ms |
3288 KB |
Output is correct |
20 |
Correct |
4203 ms |
3244 KB |
Output is correct |
21 |
Correct |
677 ms |
3200 KB |
Output is correct |
22 |
Correct |
691 ms |
3220 KB |
Output is correct |
23 |
Correct |
2 ms |
4180 KB |
Output is correct |
24 |
Execution timed out |
5075 ms |
8764 KB |
Time limit exceeded |
25 |
Halted |
0 ms |
0 KB |
- |