#include <bits/stdc++.h>
#pragma GCC target ("avx2")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include<bits/stdc++.h>
#include<math.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> pl;
typedef vector<ll> vl;
#define FD(i, r, l) for(ll i = r; i > (l); --i)
#define K first
#define V second
#define G(x) ll x; cin >> x;
#define GD(x) ld x; cin >> x;
#define GS(s) string s; cin >> s;
#define EX(x) { cout << x << '\n'; exit(0); }
#define A(a) (a).begin(), (a).end()
#define F(i, l, r) for (ll i = l; i < (r); ++i)
enum {
UP = 0,
DOWN = 1
};
ll max_weights(int N, int M, std::vector<int> X, std::vector<int> Y,
std::vector<int> W) {
vector<vector<pair<int, int>>> fishes(N);
F(i, 0, M) fishes[X[i]].push_back(make_pair(Y[i], i));
ll largest = 0;
vector<pl> up, down; // stores [ypos, dpvalue] of prev state
vector<vl> dp(M, vl(2, -1e18)); // dp[fish index][type]
F(x, 0, N) {
// computes dp[down] for layer
if (x > 0) { // w/o this check, we allow placing piers to index -1 which is no bueno
sort(A(fishes[x]), greater());
ll max_down = 0, prev = -1e18;
for (auto [y, i]: fishes[x]) {
while (down.size() > 0 && down.back().K > y) {
max_down = max(max_down, down.back().V);
down.pop_back();
}
ll &ret = dp[i][DOWN];
ret = largest + W[i];
ret = max(ret, max_down + W[i]);
ret = max(ret, prev + W[i]);
prev = max(prev, dp[i][DOWN]);
}
}
if (x >= 1) for (auto [y, i] : fishes[x - 1]) {
largest = max(largest, dp[i][DOWN]);
}
// computes dp[up] for layer; ig implciitly we can already think about largest as the previous dpdown???
{
sort(A(fishes[x]));
ll max_up = 0, prev = -1e18;
for (auto [y, i]: fishes[x]) {
while (up.size() > 0 && up.back().K < y) {
max_up = max(max_up, up.back().V);
up.pop_back();
}
ll &ret = dp[i][UP];
ret = largest + W[i];
ret = max(ret, max_up + W[i]);
ret = max(ret, prev + W[i]);
prev = max(prev, dp[i][UP]);
}
}
if (x >= 1) for (auto [y, i] : fishes[x - 1]) {
largest = max(largest, dp[i][UP]);
}
down.clear();
for (auto [y, i]: fishes[x]) down.emplace_back(y, dp[i][DOWN]);
up.clear();
for (auto [y, i]: fishes[x]) up.emplace_back(y, dp[i][UP]);
reverse(A(up));
}
ll answer = 0;
F(i, 0, M) {
answer = max(answer, dp[i][DOWN]);
if (X[i] + 1 < N) answer = max(answer, dp[i][UP]);
}
return answer;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
13720 KB |
Output is correct |
2 |
Correct |
50 ms |
15940 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2652 KB |
Output is correct |
5 |
Correct |
152 ms |
33092 KB |
Output is correct |
6 |
Correct |
149 ms |
30288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
75 ms |
20800 KB |
Output is correct |
3 |
Correct |
100 ms |
24572 KB |
Output is correct |
4 |
Correct |
36 ms |
13640 KB |
Output is correct |
5 |
Correct |
45 ms |
15940 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
2652 KB |
Output is correct |
11 |
Correct |
2 ms |
2904 KB |
Output is correct |
12 |
Correct |
37 ms |
13904 KB |
Output is correct |
13 |
Correct |
46 ms |
15892 KB |
Output is correct |
14 |
Correct |
35 ms |
11484 KB |
Output is correct |
15 |
Correct |
40 ms |
12748 KB |
Output is correct |
16 |
Correct |
48 ms |
11628 KB |
Output is correct |
17 |
Correct |
43 ms |
12704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2648 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
27 ms |
8284 KB |
Output is correct |
4 |
Correct |
17 ms |
6744 KB |
Output is correct |
5 |
Correct |
46 ms |
13640 KB |
Output is correct |
6 |
Correct |
43 ms |
13912 KB |
Output is correct |
7 |
Correct |
46 ms |
13648 KB |
Output is correct |
8 |
Correct |
48 ms |
13648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
18 ms |
4188 KB |
Output is correct |
18 |
Correct |
17 ms |
4444 KB |
Output is correct |
19 |
Correct |
16 ms |
4444 KB |
Output is correct |
20 |
Correct |
16 ms |
4556 KB |
Output is correct |
21 |
Correct |
16 ms |
4444 KB |
Output is correct |
22 |
Correct |
32 ms |
8664 KB |
Output is correct |
23 |
Correct |
4 ms |
1116 KB |
Output is correct |
24 |
Correct |
12 ms |
2908 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
4 ms |
1112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
18 ms |
4188 KB |
Output is correct |
18 |
Correct |
17 ms |
4444 KB |
Output is correct |
19 |
Correct |
16 ms |
4444 KB |
Output is correct |
20 |
Correct |
16 ms |
4556 KB |
Output is correct |
21 |
Correct |
16 ms |
4444 KB |
Output is correct |
22 |
Correct |
32 ms |
8664 KB |
Output is correct |
23 |
Correct |
4 ms |
1116 KB |
Output is correct |
24 |
Correct |
12 ms |
2908 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
4 ms |
1112 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
92 ms |
20116 KB |
Output is correct |
29 |
Correct |
123 ms |
27476 KB |
Output is correct |
30 |
Correct |
125 ms |
26976 KB |
Output is correct |
31 |
Correct |
137 ms |
26988 KB |
Output is correct |
32 |
Correct |
119 ms |
28264 KB |
Output is correct |
33 |
Correct |
130 ms |
26964 KB |
Output is correct |
34 |
Correct |
127 ms |
26984 KB |
Output is correct |
35 |
Correct |
45 ms |
10836 KB |
Output is correct |
36 |
Correct |
130 ms |
27728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2648 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
27 ms |
8284 KB |
Output is correct |
4 |
Correct |
17 ms |
6744 KB |
Output is correct |
5 |
Correct |
46 ms |
13640 KB |
Output is correct |
6 |
Correct |
43 ms |
13912 KB |
Output is correct |
7 |
Correct |
46 ms |
13648 KB |
Output is correct |
8 |
Correct |
48 ms |
13648 KB |
Output is correct |
9 |
Correct |
50 ms |
13736 KB |
Output is correct |
10 |
Correct |
41 ms |
11084 KB |
Output is correct |
11 |
Correct |
113 ms |
21332 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
424 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
2 ms |
2652 KB |
Output is correct |
19 |
Correct |
2 ms |
2648 KB |
Output is correct |
20 |
Correct |
2 ms |
2648 KB |
Output is correct |
21 |
Correct |
2 ms |
2652 KB |
Output is correct |
22 |
Correct |
44 ms |
12628 KB |
Output is correct |
23 |
Correct |
99 ms |
21332 KB |
Output is correct |
24 |
Correct |
98 ms |
21468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
13720 KB |
Output is correct |
2 |
Correct |
50 ms |
15940 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2652 KB |
Output is correct |
5 |
Correct |
152 ms |
33092 KB |
Output is correct |
6 |
Correct |
149 ms |
30288 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
75 ms |
20800 KB |
Output is correct |
9 |
Correct |
100 ms |
24572 KB |
Output is correct |
10 |
Correct |
36 ms |
13640 KB |
Output is correct |
11 |
Correct |
45 ms |
15940 KB |
Output is correct |
12 |
Correct |
1 ms |
600 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
2652 KB |
Output is correct |
17 |
Correct |
2 ms |
2904 KB |
Output is correct |
18 |
Correct |
37 ms |
13904 KB |
Output is correct |
19 |
Correct |
46 ms |
15892 KB |
Output is correct |
20 |
Correct |
35 ms |
11484 KB |
Output is correct |
21 |
Correct |
40 ms |
12748 KB |
Output is correct |
22 |
Correct |
48 ms |
11628 KB |
Output is correct |
23 |
Correct |
43 ms |
12704 KB |
Output is correct |
24 |
Correct |
2 ms |
2648 KB |
Output is correct |
25 |
Correct |
2 ms |
2652 KB |
Output is correct |
26 |
Correct |
27 ms |
8284 KB |
Output is correct |
27 |
Correct |
17 ms |
6744 KB |
Output is correct |
28 |
Correct |
46 ms |
13640 KB |
Output is correct |
29 |
Correct |
43 ms |
13912 KB |
Output is correct |
30 |
Correct |
46 ms |
13648 KB |
Output is correct |
31 |
Correct |
48 ms |
13648 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
344 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
604 KB |
Output is correct |
48 |
Correct |
18 ms |
4188 KB |
Output is correct |
49 |
Correct |
17 ms |
4444 KB |
Output is correct |
50 |
Correct |
16 ms |
4444 KB |
Output is correct |
51 |
Correct |
16 ms |
4556 KB |
Output is correct |
52 |
Correct |
16 ms |
4444 KB |
Output is correct |
53 |
Correct |
32 ms |
8664 KB |
Output is correct |
54 |
Correct |
4 ms |
1116 KB |
Output is correct |
55 |
Correct |
12 ms |
2908 KB |
Output is correct |
56 |
Correct |
1 ms |
344 KB |
Output is correct |
57 |
Correct |
4 ms |
1112 KB |
Output is correct |
58 |
Correct |
1 ms |
604 KB |
Output is correct |
59 |
Correct |
92 ms |
20116 KB |
Output is correct |
60 |
Correct |
123 ms |
27476 KB |
Output is correct |
61 |
Correct |
125 ms |
26976 KB |
Output is correct |
62 |
Correct |
137 ms |
26988 KB |
Output is correct |
63 |
Correct |
119 ms |
28264 KB |
Output is correct |
64 |
Correct |
130 ms |
26964 KB |
Output is correct |
65 |
Correct |
127 ms |
26984 KB |
Output is correct |
66 |
Correct |
45 ms |
10836 KB |
Output is correct |
67 |
Correct |
130 ms |
27728 KB |
Output is correct |
68 |
Correct |
50 ms |
13736 KB |
Output is correct |
69 |
Correct |
41 ms |
11084 KB |
Output is correct |
70 |
Correct |
113 ms |
21332 KB |
Output is correct |
71 |
Correct |
1 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
1 ms |
348 KB |
Output is correct |
74 |
Correct |
0 ms |
348 KB |
Output is correct |
75 |
Correct |
0 ms |
424 KB |
Output is correct |
76 |
Correct |
0 ms |
348 KB |
Output is correct |
77 |
Correct |
2 ms |
2652 KB |
Output is correct |
78 |
Correct |
2 ms |
2648 KB |
Output is correct |
79 |
Correct |
2 ms |
2648 KB |
Output is correct |
80 |
Correct |
2 ms |
2652 KB |
Output is correct |
81 |
Correct |
44 ms |
12628 KB |
Output is correct |
82 |
Correct |
99 ms |
21332 KB |
Output is correct |
83 |
Correct |
98 ms |
21468 KB |
Output is correct |
84 |
Correct |
143 ms |
29760 KB |
Output is correct |
85 |
Correct |
138 ms |
29804 KB |
Output is correct |
86 |
Correct |
176 ms |
30224 KB |
Output is correct |
87 |
Correct |
170 ms |
30584 KB |
Output is correct |
88 |
Correct |
1 ms |
344 KB |
Output is correct |
89 |
Correct |
177 ms |
30804 KB |
Output is correct |
90 |
Correct |
152 ms |
30036 KB |
Output is correct |
91 |
Correct |
153 ms |
29940 KB |
Output is correct |