#pragma GCC optimize ("Ofast")
#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vi vector<int>
#define ii pair<int, int>
#define vii vector<ii>
#define tuple2 tuple<int, int>
#define tuple3 tuple<int, int, int>
const int N = 1e5;
int n, m;
vi c[N];
map<int, int> w[N];
map<int, ll> pre[N];
map<int, ll> dp[3][N];
ll preS(int col, int x) {
auto it = pre[col].upper_bound(x);
if (it == pre[col].begin()) return 0;
return (--it)->second;
}
ll mx(int curr, int h1, int state) {
if (curr == -1) return 0;
auto it = dp[state][curr].find(h1);
if (it != dp[state][curr].end()) return it->second;
if (state != 2) dp[state][curr][h1] = mx(curr-1, h1, 2);
else dp[state][curr][h1] = mx(curr-1, 0, 1);
it = dp[state][curr].find(h1);
if (!state) {
if (curr+1 < n) {
for (int h : c[curr+1]) {
h++;
if (h >= h1) break;
ll cnt = 0;
// ver si pillo los de la izquierda
if (curr-1 >= 0) {
cnt += preS(curr-1, h-1);
}
// ver si he eliminado alguno de los que he pillado
cnt -= preS(curr, h-1);
it->second = max(it->second, cnt + mx(curr-1, h, 0));
}
}
if (curr-1 >= 0) {
for (int h : c[curr-1]) {
h++;
if (h >= h1) break;
ll cnt = 0;
// ver si pillo los de la izquierda
if (curr-1 >= 0) {
cnt += preS(curr-1, h-1);
}
// ver si he eliminado alguno de los que he pillado
cnt -= preS(curr, h-1);
it->second = max(it->second, cnt + mx(curr-1, h, 0));
}
}
}
else if (state == 1) {
if (curr+1 < n) {
for (int h : c[curr+1]) {
h++;
ll cnt = 0;
// ver si pillo los de la izquierda
if (curr-1 >= 0) {
cnt += preS(curr-1, h-1);
}
// ver si pillo los de la derecha
if (h > h1 && curr+1 < n) {
cnt += preS(curr+1, h-1) - preS(curr+1, h1-1);
}
// ver si he eliminado alguno de los que he pillado
cnt -= preS(curr, min(h, h1)-1);
if (h <= h1) it->second = max(it->second, cnt + mx(curr-1, h, 0));
else it->second = max(it->second, cnt + mx(curr-1, h, 1));
}
}
if (curr-1 >= 0) {
for (int h : c[curr-1]) {
h++;
ll cnt = 0;
// ver si pillo los de la izquierda
if (curr-1 >= 0) {
cnt += preS(curr-1, h-1);
}
// ver si pillo los de la derecha
if (h > h1 && curr+1 < n) {
cnt += preS(curr+1, h-1) - preS(curr+1, h1-1);
}
// ver si he eliminado alguno de los que he pillado
cnt -= preS(curr, min(h, h1)-1);
if (h <= h1) it->second = max(it->second, cnt + mx(curr-1, h, 0));
else it->second = max(it->second, cnt + mx(curr-1, h, 1));
}
}
}
else {
if (curr+1 < n) {
for (int h : c[curr+1]) {
h++;
ll cnt = 0;
// ver si pillo los de la izquierda
if (curr-1 >= 0) {
cnt += preS(curr-1, h-1);
}
// ver si pillo los de la derecha
if (h > h1 && curr+1 < n) {
cnt += preS(curr+1, h-1) - preS(curr+1, h1-1);
}
it->second = max(it->second, cnt + mx(curr-1, h, 1));
}
}
if (curr-1 >= 0) {
for (int h : c[curr-1]) {
h++;
ll cnt = 0;
// ver si pillo los de la izquierda
if (curr-1 >= 0) {
cnt += preS(curr-1, h-1);
}
// ver si pillo los de la derecha
if (h > h1 && curr+1 < n) {
cnt += preS(curr+1, h-1) - preS(curr+1, h1-1);
}
it->second = max(it->second, cnt + mx(curr-1, h, 1));
}
}
}
return it->second;
}
long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y,
std::vector<int> W) {
n = N;
m = M;
for (int i = 0; i < m; i++) {
c[X[i]].push_back(Y[i]);
w[X[i]][Y[i]] = W[i];
}
for (int i = 0; i < n; i++) {
sort(c[i].begin(), c[i].end());
ll sum = 0;
for (int &h : c[i]) {
sum += w[i][h];
pre[i][h] = sum;
}
pre[i][-1] = 0;
}
return mx(n-1, 0, 1);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
148 ms |
67656 KB |
Output is correct |
2 |
Correct |
186 ms |
74152 KB |
Output is correct |
3 |
Correct |
37 ms |
54280 KB |
Output is correct |
4 |
Correct |
42 ms |
54184 KB |
Output is correct |
5 |
Execution timed out |
1089 ms |
104404 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
26088 KB |
Output is correct |
2 |
Execution timed out |
1094 ms |
68140 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
54192 KB |
Output is correct |
2 |
Correct |
38 ms |
54264 KB |
Output is correct |
3 |
Correct |
122 ms |
76696 KB |
Output is correct |
4 |
Correct |
99 ms |
72956 KB |
Output is correct |
5 |
Correct |
209 ms |
95704 KB |
Output is correct |
6 |
Correct |
202 ms |
95796 KB |
Output is correct |
7 |
Correct |
199 ms |
95788 KB |
Output is correct |
8 |
Correct |
205 ms |
95728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
26068 KB |
Output is correct |
2 |
Correct |
17 ms |
26068 KB |
Output is correct |
3 |
Correct |
14 ms |
26068 KB |
Output is correct |
4 |
Correct |
14 ms |
26124 KB |
Output is correct |
5 |
Correct |
15 ms |
26116 KB |
Output is correct |
6 |
Correct |
14 ms |
26036 KB |
Output is correct |
7 |
Correct |
15 ms |
26052 KB |
Output is correct |
8 |
Correct |
14 ms |
26136 KB |
Output is correct |
9 |
Correct |
21 ms |
26344 KB |
Output is correct |
10 |
Correct |
30 ms |
26892 KB |
Output is correct |
11 |
Correct |
20 ms |
26452 KB |
Output is correct |
12 |
Correct |
18 ms |
26708 KB |
Output is correct |
13 |
Correct |
15 ms |
26160 KB |
Output is correct |
14 |
Correct |
17 ms |
26588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
26068 KB |
Output is correct |
2 |
Correct |
17 ms |
26068 KB |
Output is correct |
3 |
Correct |
14 ms |
26068 KB |
Output is correct |
4 |
Correct |
14 ms |
26124 KB |
Output is correct |
5 |
Correct |
15 ms |
26116 KB |
Output is correct |
6 |
Correct |
14 ms |
26036 KB |
Output is correct |
7 |
Correct |
15 ms |
26052 KB |
Output is correct |
8 |
Correct |
14 ms |
26136 KB |
Output is correct |
9 |
Correct |
21 ms |
26344 KB |
Output is correct |
10 |
Correct |
30 ms |
26892 KB |
Output is correct |
11 |
Correct |
20 ms |
26452 KB |
Output is correct |
12 |
Correct |
18 ms |
26708 KB |
Output is correct |
13 |
Correct |
15 ms |
26160 KB |
Output is correct |
14 |
Correct |
17 ms |
26588 KB |
Output is correct |
15 |
Correct |
14 ms |
26352 KB |
Output is correct |
16 |
Correct |
91 ms |
26916 KB |
Output is correct |
17 |
Execution timed out |
1089 ms |
33860 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
26068 KB |
Output is correct |
2 |
Correct |
17 ms |
26068 KB |
Output is correct |
3 |
Correct |
14 ms |
26068 KB |
Output is correct |
4 |
Correct |
14 ms |
26124 KB |
Output is correct |
5 |
Correct |
15 ms |
26116 KB |
Output is correct |
6 |
Correct |
14 ms |
26036 KB |
Output is correct |
7 |
Correct |
15 ms |
26052 KB |
Output is correct |
8 |
Correct |
14 ms |
26136 KB |
Output is correct |
9 |
Correct |
21 ms |
26344 KB |
Output is correct |
10 |
Correct |
30 ms |
26892 KB |
Output is correct |
11 |
Correct |
20 ms |
26452 KB |
Output is correct |
12 |
Correct |
18 ms |
26708 KB |
Output is correct |
13 |
Correct |
15 ms |
26160 KB |
Output is correct |
14 |
Correct |
17 ms |
26588 KB |
Output is correct |
15 |
Correct |
14 ms |
26352 KB |
Output is correct |
16 |
Correct |
91 ms |
26916 KB |
Output is correct |
17 |
Execution timed out |
1089 ms |
33860 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
54192 KB |
Output is correct |
2 |
Correct |
38 ms |
54264 KB |
Output is correct |
3 |
Correct |
122 ms |
76696 KB |
Output is correct |
4 |
Correct |
99 ms |
72956 KB |
Output is correct |
5 |
Correct |
209 ms |
95704 KB |
Output is correct |
6 |
Correct |
202 ms |
95796 KB |
Output is correct |
7 |
Correct |
199 ms |
95788 KB |
Output is correct |
8 |
Correct |
205 ms |
95728 KB |
Output is correct |
9 |
Correct |
270 ms |
114500 KB |
Output is correct |
10 |
Correct |
196 ms |
76884 KB |
Output is correct |
11 |
Correct |
402 ms |
127848 KB |
Output is correct |
12 |
Correct |
18 ms |
26016 KB |
Output is correct |
13 |
Correct |
14 ms |
26068 KB |
Output is correct |
14 |
Correct |
17 ms |
26108 KB |
Output is correct |
15 |
Correct |
18 ms |
26120 KB |
Output is correct |
16 |
Correct |
17 ms |
26064 KB |
Output is correct |
17 |
Correct |
15 ms |
26068 KB |
Output is correct |
18 |
Correct |
36 ms |
54256 KB |
Output is correct |
19 |
Correct |
53 ms |
54216 KB |
Output is correct |
20 |
Correct |
39 ms |
54292 KB |
Output is correct |
21 |
Correct |
39 ms |
54220 KB |
Output is correct |
22 |
Correct |
314 ms |
108208 KB |
Output is correct |
23 |
Correct |
548 ms |
152084 KB |
Output is correct |
24 |
Correct |
553 ms |
157548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
148 ms |
67656 KB |
Output is correct |
2 |
Correct |
186 ms |
74152 KB |
Output is correct |
3 |
Correct |
37 ms |
54280 KB |
Output is correct |
4 |
Correct |
42 ms |
54184 KB |
Output is correct |
5 |
Execution timed out |
1089 ms |
104404 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |