#include "fish.h"
#include <algorithm>
#include <array>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
using std::array;
using std::pair;
using std::string;
using std::tuple;
using std::vector;
using std::begin;
using std::end;
using ll = long long;
constexpr int inf = (1 << 30) - 1;
constexpr ll infll = (1ll << 62) - 1;
template <class T> bool setmax(T& x, const T& y) { return x < y && (x = y, true); }
ll max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
vector<vector<array<int, 2>>> fish(N);
for (int i = 0; i < M; ++i) {
fish[X[i]].push_back({Y[i], W[i]});
}
for (auto& v : fish) {
std::sort(begin(v), end(v));
v.push_back({N, 0});
}
vector<ll> dp0(fish[0].size()), dp1(fish[0].size());
for (int row = 1; row < N; ++row) {
const int n = fish[row - 1].size();
const int m = fish[row].size();
vector<ll> ndp0(m), ndp1(m, *std::max_element(begin(dp0), end(dp0)));
{
vector<ll> acc(m);
for (int i = 0; i < m - 1; ++i) {
acc[i + 1] = acc[i] + fish[row][i][1];
}
int j = n;
ll max = -infll;
for (int i = m - 2; i >= 0; --i) {
while (j > 0 && fish[row - 1][j - 1][0] > fish[row][i][0]) {
j -= 1;
setmax(max, dp0[j] + acc[i + 1]);
}
setmax(ndp0[i], max - acc[i]);
}
}
{
vector<ll> acc(n);
for (int i = 0; i < n - 1; ++i) {
acc[i + 1] = acc[i] + fish[row - 1][i][1];
}
int j = 0;
ll max = -infll;
for (int i = 0; i < m; ++i) {
while (j < n && fish[row - 1][j][0] < fish[row][i][0]) {
setmax(max, dp1[j] - acc[j]);
j += 1;
}
setmax(ndp1[i], max + acc[j]);
}
}
dp0 = std::move(ndp0);
dp1 = std::move(ndp1);
for (int i = 0; i < m; ++i) {
setmax(dp0[i], dp1[i]);
}
}
return *std::max_element(begin(dp0), end(dp0));
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
10960 KB |
Output is correct |
2 |
Correct |
45 ms |
12968 KB |
Output is correct |
3 |
Correct |
11 ms |
5716 KB |
Output is correct |
4 |
Correct |
12 ms |
5716 KB |
Output is correct |
5 |
Correct |
127 ms |
23684 KB |
Output is correct |
6 |
Correct |
105 ms |
22548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
65 ms |
16232 KB |
Output is correct |
3 |
Correct |
78 ms |
19464 KB |
Output is correct |
4 |
Correct |
37 ms |
10944 KB |
Output is correct |
5 |
Correct |
44 ms |
12968 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
12 ms |
5716 KB |
Output is correct |
11 |
Correct |
12 ms |
5716 KB |
Output is correct |
12 |
Correct |
40 ms |
11100 KB |
Output is correct |
13 |
Correct |
45 ms |
12992 KB |
Output is correct |
14 |
Correct |
37 ms |
10796 KB |
Output is correct |
15 |
Correct |
41 ms |
11968 KB |
Output is correct |
16 |
Correct |
37 ms |
10756 KB |
Output is correct |
17 |
Correct |
42 ms |
12000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
5716 KB |
Output is correct |
2 |
Correct |
13 ms |
5716 KB |
Output is correct |
3 |
Correct |
27 ms |
7376 KB |
Output is correct |
4 |
Correct |
23 ms |
7160 KB |
Output is correct |
5 |
Correct |
41 ms |
9684 KB |
Output is correct |
6 |
Correct |
37 ms |
8984 KB |
Output is correct |
7 |
Correct |
40 ms |
9672 KB |
Output is correct |
8 |
Correct |
40 ms |
9660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
15 ms |
2740 KB |
Output is correct |
18 |
Correct |
14 ms |
2908 KB |
Output is correct |
19 |
Correct |
14 ms |
2772 KB |
Output is correct |
20 |
Correct |
13 ms |
2748 KB |
Output is correct |
21 |
Correct |
13 ms |
2772 KB |
Output is correct |
22 |
Correct |
26 ms |
5332 KB |
Output is correct |
23 |
Correct |
3 ms |
696 KB |
Output is correct |
24 |
Correct |
10 ms |
1876 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
3 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
15 ms |
2740 KB |
Output is correct |
18 |
Correct |
14 ms |
2908 KB |
Output is correct |
19 |
Correct |
14 ms |
2772 KB |
Output is correct |
20 |
Correct |
13 ms |
2748 KB |
Output is correct |
21 |
Correct |
13 ms |
2772 KB |
Output is correct |
22 |
Correct |
26 ms |
5332 KB |
Output is correct |
23 |
Correct |
3 ms |
696 KB |
Output is correct |
24 |
Correct |
10 ms |
1876 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
3 ms |
724 KB |
Output is correct |
27 |
Correct |
2 ms |
468 KB |
Output is correct |
28 |
Correct |
68 ms |
12284 KB |
Output is correct |
29 |
Correct |
93 ms |
16856 KB |
Output is correct |
30 |
Correct |
101 ms |
16344 KB |
Output is correct |
31 |
Correct |
92 ms |
16352 KB |
Output is correct |
32 |
Correct |
91 ms |
17372 KB |
Output is correct |
33 |
Correct |
91 ms |
16420 KB |
Output is correct |
34 |
Correct |
92 ms |
16348 KB |
Output is correct |
35 |
Correct |
38 ms |
6716 KB |
Output is correct |
36 |
Correct |
92 ms |
17304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
5716 KB |
Output is correct |
2 |
Correct |
13 ms |
5716 KB |
Output is correct |
3 |
Correct |
27 ms |
7376 KB |
Output is correct |
4 |
Correct |
23 ms |
7160 KB |
Output is correct |
5 |
Correct |
41 ms |
9684 KB |
Output is correct |
6 |
Correct |
37 ms |
8984 KB |
Output is correct |
7 |
Correct |
40 ms |
9672 KB |
Output is correct |
8 |
Correct |
40 ms |
9660 KB |
Output is correct |
9 |
Correct |
40 ms |
9492 KB |
Output is correct |
10 |
Correct |
38 ms |
8368 KB |
Output is correct |
11 |
Correct |
116 ms |
16340 KB |
Output is correct |
12 |
Correct |
0 ms |
296 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
300 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
12 ms |
5680 KB |
Output is correct |
19 |
Correct |
13 ms |
5736 KB |
Output is correct |
20 |
Correct |
12 ms |
5716 KB |
Output is correct |
21 |
Correct |
12 ms |
5684 KB |
Output is correct |
22 |
Correct |
46 ms |
10768 KB |
Output is correct |
23 |
Correct |
88 ms |
16440 KB |
Output is correct |
24 |
Correct |
80 ms |
16972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
10960 KB |
Output is correct |
2 |
Correct |
45 ms |
12968 KB |
Output is correct |
3 |
Correct |
11 ms |
5716 KB |
Output is correct |
4 |
Correct |
12 ms |
5716 KB |
Output is correct |
5 |
Correct |
127 ms |
23684 KB |
Output is correct |
6 |
Correct |
105 ms |
22548 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
65 ms |
16232 KB |
Output is correct |
9 |
Correct |
78 ms |
19464 KB |
Output is correct |
10 |
Correct |
37 ms |
10944 KB |
Output is correct |
11 |
Correct |
44 ms |
12968 KB |
Output is correct |
12 |
Correct |
1 ms |
340 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 |
12 ms |
5716 KB |
Output is correct |
17 |
Correct |
12 ms |
5716 KB |
Output is correct |
18 |
Correct |
40 ms |
11100 KB |
Output is correct |
19 |
Correct |
45 ms |
12992 KB |
Output is correct |
20 |
Correct |
37 ms |
10796 KB |
Output is correct |
21 |
Correct |
41 ms |
11968 KB |
Output is correct |
22 |
Correct |
37 ms |
10756 KB |
Output is correct |
23 |
Correct |
42 ms |
12000 KB |
Output is correct |
24 |
Correct |
12 ms |
5716 KB |
Output is correct |
25 |
Correct |
13 ms |
5716 KB |
Output is correct |
26 |
Correct |
27 ms |
7376 KB |
Output is correct |
27 |
Correct |
23 ms |
7160 KB |
Output is correct |
28 |
Correct |
41 ms |
9684 KB |
Output is correct |
29 |
Correct |
37 ms |
8984 KB |
Output is correct |
30 |
Correct |
40 ms |
9672 KB |
Output is correct |
31 |
Correct |
40 ms |
9660 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
15 ms |
2740 KB |
Output is correct |
49 |
Correct |
14 ms |
2908 KB |
Output is correct |
50 |
Correct |
14 ms |
2772 KB |
Output is correct |
51 |
Correct |
13 ms |
2748 KB |
Output is correct |
52 |
Correct |
13 ms |
2772 KB |
Output is correct |
53 |
Correct |
26 ms |
5332 KB |
Output is correct |
54 |
Correct |
3 ms |
696 KB |
Output is correct |
55 |
Correct |
10 ms |
1876 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
3 ms |
724 KB |
Output is correct |
58 |
Correct |
2 ms |
468 KB |
Output is correct |
59 |
Correct |
68 ms |
12284 KB |
Output is correct |
60 |
Correct |
93 ms |
16856 KB |
Output is correct |
61 |
Correct |
101 ms |
16344 KB |
Output is correct |
62 |
Correct |
92 ms |
16352 KB |
Output is correct |
63 |
Correct |
91 ms |
17372 KB |
Output is correct |
64 |
Correct |
91 ms |
16420 KB |
Output is correct |
65 |
Correct |
92 ms |
16348 KB |
Output is correct |
66 |
Correct |
38 ms |
6716 KB |
Output is correct |
67 |
Correct |
92 ms |
17304 KB |
Output is correct |
68 |
Correct |
40 ms |
9492 KB |
Output is correct |
69 |
Correct |
38 ms |
8368 KB |
Output is correct |
70 |
Correct |
116 ms |
16340 KB |
Output is correct |
71 |
Correct |
0 ms |
296 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
0 ms |
212 KB |
Output is correct |
74 |
Correct |
1 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
300 KB |
Output is correct |
76 |
Correct |
0 ms |
212 KB |
Output is correct |
77 |
Correct |
12 ms |
5680 KB |
Output is correct |
78 |
Correct |
13 ms |
5736 KB |
Output is correct |
79 |
Correct |
12 ms |
5716 KB |
Output is correct |
80 |
Correct |
12 ms |
5684 KB |
Output is correct |
81 |
Correct |
46 ms |
10768 KB |
Output is correct |
82 |
Correct |
88 ms |
16440 KB |
Output is correct |
83 |
Correct |
80 ms |
16972 KB |
Output is correct |
84 |
Correct |
110 ms |
21768 KB |
Output is correct |
85 |
Correct |
119 ms |
21780 KB |
Output is correct |
86 |
Correct |
117 ms |
21452 KB |
Output is correct |
87 |
Correct |
114 ms |
22076 KB |
Output is correct |
88 |
Correct |
0 ms |
212 KB |
Output is correct |
89 |
Correct |
137 ms |
22116 KB |
Output is correct |
90 |
Correct |
132 ms |
21132 KB |
Output is correct |
91 |
Correct |
125 ms |
21284 KB |
Output is correct |