#include <bits/stdc++.h>
using namespace std;
template <typename T> class negarr {
private:
T data[505000 * 2 + 1];
public:
negarr() { memset(data, 0, sizeof(data)); }
void reset() { fill(data, data + 505000 * 2 + 1, -1000000000); }
T &operator[](int i) { return data[i + 505000]; }
};
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;
}
};
negarr<int> a, knapsack;
int main() {
int m;
long long l;
cin >> m >> l;
if (l > 505000 || l < -505000) {
printf("impossible\n");
return 0;
}
knapsack.reset();
knapsack[0] = 0;
for (int i = -m; i <= m; i++) {
cin >> a[i];
negarr<int> tmp;
tmp.reset();
if (i == 0) {
for (int id = -505000; id <= 505000; id++) {
knapsack[id] += a[i];
}
} else {
if (i > 0) {
priority_queue<nstate> pq[i];
for (int id = -505000; id <= 505000; id++) {
int mod = id % i;
if (mod < 0)
mod += i;
int xx = (id + 505000) / i;
pq[mod].push({xx, knapsack[id]});
while (!pq[mod].empty() && xx - pq[mod].top().x > a[i])
pq[mod].pop();
if (!pq[mod].empty()) {
nstate t = pq[mod].top();
tmp[id] = max(tmp[id], t.y - t.x + xx);
}
}
} else if (i < 0) {
priority_queue<pstate> pq[-i];
for (int id = 505000; id >= -505000; id--) {
int mod = id % (-i);
if (mod < 0)
mod += (-i);
int xx = (id + 505000) / (-i);
pq[mod].push({xx, knapsack[id]});
while (!pq[mod].empty() && pq[mod].top().x - xx > a[i])
pq[mod].pop();
if (!pq[mod].empty()) {
pstate t = pq[mod].top();
tmp[id] = max(tmp[id], t.y + t.x - xx);
}
}
}
for (int id = -505000; id <= 505000; id++) {
knapsack[id] = max(knapsack[id], tmp[id]);
}
}
}
if (knapsack[l] >= 0)
printf("%d\n", knapsack[l]);
else
printf("impossible\n");
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
134 ms |
12136 KB |
Output is correct |
2 |
Correct |
169 ms |
12132 KB |
Output is correct |
3 |
Correct |
67 ms |
12116 KB |
Output is correct |
4 |
Correct |
616 ms |
12132 KB |
Output is correct |
5 |
Correct |
5 ms |
12116 KB |
Output is correct |
6 |
Correct |
4813 ms |
12544 KB |
Output is correct |
7 |
Correct |
3766 ms |
12356 KB |
Output is correct |
8 |
Execution timed out |
5066 ms |
12604 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
134 ms |
12136 KB |
Output is correct |
2 |
Correct |
169 ms |
12132 KB |
Output is correct |
3 |
Correct |
67 ms |
12116 KB |
Output is correct |
4 |
Correct |
616 ms |
12132 KB |
Output is correct |
5 |
Correct |
5 ms |
12116 KB |
Output is correct |
6 |
Correct |
4813 ms |
12544 KB |
Output is correct |
7 |
Correct |
3766 ms |
12356 KB |
Output is correct |
8 |
Execution timed out |
5066 ms |
12604 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
703 ms |
12132 KB |
Output is correct |
2 |
Incorrect |
6 ms |
12116 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
703 ms |
12132 KB |
Output is correct |
2 |
Incorrect |
6 ms |
12116 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
703 ms |
12132 KB |
Output is correct |
2 |
Incorrect |
6 ms |
12116 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
134 ms |
12136 KB |
Output is correct |
2 |
Correct |
169 ms |
12132 KB |
Output is correct |
3 |
Correct |
67 ms |
12116 KB |
Output is correct |
4 |
Correct |
616 ms |
12132 KB |
Output is correct |
5 |
Correct |
5 ms |
12116 KB |
Output is correct |
6 |
Correct |
4813 ms |
12544 KB |
Output is correct |
7 |
Correct |
3766 ms |
12356 KB |
Output is correct |
8 |
Execution timed out |
5066 ms |
12604 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
703 ms |
12132 KB |
Output is correct |
2 |
Incorrect |
6 ms |
12116 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
134 ms |
12136 KB |
Output is correct |
2 |
Correct |
169 ms |
12132 KB |
Output is correct |
3 |
Correct |
67 ms |
12116 KB |
Output is correct |
4 |
Correct |
616 ms |
12132 KB |
Output is correct |
5 |
Correct |
5 ms |
12116 KB |
Output is correct |
6 |
Correct |
4813 ms |
12544 KB |
Output is correct |
7 |
Correct |
3766 ms |
12356 KB |
Output is correct |
8 |
Execution timed out |
5066 ms |
12604 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
703 ms |
12132 KB |
Output is correct |
2 |
Incorrect |
6 ms |
12116 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
134 ms |
12136 KB |
Output is correct |
2 |
Correct |
169 ms |
12132 KB |
Output is correct |
3 |
Correct |
67 ms |
12116 KB |
Output is correct |
4 |
Correct |
616 ms |
12132 KB |
Output is correct |
5 |
Correct |
5 ms |
12116 KB |
Output is correct |
6 |
Correct |
4813 ms |
12544 KB |
Output is correct |
7 |
Correct |
3766 ms |
12356 KB |
Output is correct |
8 |
Execution timed out |
5066 ms |
12604 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |