#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
long long max_weights(int n, int m, std::vector<int> fx, std::vector<int> fy,
std::vector<int> fw)
{
n += 2;
vector < map < int, ll > > a (n);
for (int i = 0; i < m; ++i) a[fx[i] + 1][fy[i]] += fw[i];
ll mxdp = 0;
vector < vector < pair < int, ll > > > p (3), q (2), dp (3);
map < int, int > is;
for (auto &x : a[1]) ++is[x.first];
for (int i = 2; i < n; ++i)
{
for (auto &x : a[i]) ++is[x.first];
if (i - 3 >= 1)
for (auto &x : a[i - 3])
{
--is[x.first];
if (!is[x.first]) is.erase (is.find (x.first));
}
ll ls = 0, rs = 0, ms = 0;
ll mxp = -1e18, mxq = -1e18;
ll id = -1;
stack < int > st;
auto itp = p[2].begin ();
auto itq = q[1].begin ();
for (auto &[x, tmp] : is)
{
st.push (x);
ls += a[i - 2].count (x) ? a[i - 2][x] : 0ll;
ms += a[i - 1].count (x) ? a[i - 1][x] : 0ll;
rs += a[i].count (x) ? a[i][x] : 0ll;
if (a[i].count (x) + a[i - 2].count (x) == 0) continue;
dp[0].push_back ({ x, 0 });
p[0].push_back ({ x, 0 });
q[0].push_back ({ x, 0 });
++id;
dp[0][id].second = ls + rs + mxdp;
while (itp != p[2].end () && itp->first <= x)
mxp = max (mxp, itp->second), ++itp;
while (itq != q[1].end () && itq->first <= x)
mxq = max (mxq, itq->second), ++itq;
dp[0][id].second = max (dp[0][id].second, ls + rs + mxp);
dp[0][id].second = max (dp[0][id].second, ls + rs + mxq);
}
mxp = -1e18, mxq = -1e18;
auto ritp = dp[2].rbegin (), ritq = dp[1].rbegin ();
while (st.size ())
{
int x = st.top ();
st.pop ();
if (a[i].count (x) + a[i - 2].count (x) == 0) goto cont;
while (ritp != dp[2].rend () && ritp->first >= x)
mxp = max (mxp, ritp->second), ++ritp;
while (ritq != dp[1].rend () && ritq->first >= x)
mxq = max (mxq, ritq->second), ++ritq;
dp[0][id].second = max (dp[0][id].second, rs + mxp);
q[0][id].second = dp[0][id].second - ms - rs;
dp[0][id].second = max (dp[0][id].second, rs + mxq - ms);
p[0][id].second = dp[0][id].second - rs;
--id;
cont:
ls -= a[i - 2].count (x) ? a[i - 2][x] : 0ll;
ms -= a[i - 1].count (x) ? a[i - 1][x] : 0ll;
rs -= a[i].count (x) ? a[i][x] : 0ll;
}
swap (p[2], p[1]);
swap (p[1], p[0]);
p[0].clear ();
swap (q[1], q[0]);
q[0].clear ();
swap (dp[2], dp[1]);
swap (dp[1], dp[0]);
for (auto &x : dp[0]) mxdp = max (mxdp, x.second);
dp[0].clear ();
}
for (int i = 1; i <= 2; ++i) for (auto &x : dp[i]) mxdp = max (mxdp, x.second);
return mxdp;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
157 ms |
21400 KB |
Output is correct |
2 |
Correct |
194 ms |
25176 KB |
Output is correct |
3 |
Correct |
5 ms |
5112 KB |
Output is correct |
4 |
Correct |
7 ms |
4956 KB |
Output is correct |
5 |
Correct |
865 ms |
53740 KB |
Output is correct |
6 |
Correct |
481 ms |
37832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
604 KB |
Output is correct |
2 |
Correct |
363 ms |
38608 KB |
Output is correct |
3 |
Correct |
480 ms |
44852 KB |
Output is correct |
4 |
Correct |
143 ms |
21204 KB |
Output is correct |
5 |
Correct |
167 ms |
25232 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
5 ms |
4956 KB |
Output is correct |
11 |
Correct |
7 ms |
4956 KB |
Output is correct |
12 |
Correct |
163 ms |
25552 KB |
Output is correct |
13 |
Correct |
232 ms |
29904 KB |
Output is correct |
14 |
Correct |
155 ms |
21996 KB |
Output is correct |
15 |
Correct |
165 ms |
23516 KB |
Output is correct |
16 |
Correct |
138 ms |
22448 KB |
Output is correct |
17 |
Correct |
180 ms |
24692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4952 KB |
Output is correct |
2 |
Correct |
5 ms |
4956 KB |
Output is correct |
3 |
Correct |
27 ms |
10076 KB |
Output is correct |
4 |
Correct |
22 ms |
8676 KB |
Output is correct |
5 |
Correct |
54 ms |
15364 KB |
Output is correct |
6 |
Correct |
50 ms |
14672 KB |
Output is correct |
7 |
Correct |
47 ms |
15184 KB |
Output is correct |
8 |
Correct |
63 ms |
15296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
448 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
448 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
444 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
688 KB |
Output is correct |
17 |
Correct |
61 ms |
5252 KB |
Output is correct |
18 |
Correct |
58 ms |
5164 KB |
Output is correct |
19 |
Correct |
55 ms |
4944 KB |
Output is correct |
20 |
Correct |
60 ms |
4952 KB |
Output is correct |
21 |
Correct |
53 ms |
4956 KB |
Output is correct |
22 |
Correct |
119 ms |
9820 KB |
Output is correct |
23 |
Correct |
15 ms |
1372 KB |
Output is correct |
24 |
Correct |
52 ms |
3568 KB |
Output is correct |
25 |
Correct |
2 ms |
348 KB |
Output is correct |
26 |
Correct |
13 ms |
1224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
448 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
444 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
688 KB |
Output is correct |
17 |
Correct |
61 ms |
5252 KB |
Output is correct |
18 |
Correct |
58 ms |
5164 KB |
Output is correct |
19 |
Correct |
55 ms |
4944 KB |
Output is correct |
20 |
Correct |
60 ms |
4952 KB |
Output is correct |
21 |
Correct |
53 ms |
4956 KB |
Output is correct |
22 |
Correct |
119 ms |
9820 KB |
Output is correct |
23 |
Correct |
15 ms |
1372 KB |
Output is correct |
24 |
Correct |
52 ms |
3568 KB |
Output is correct |
25 |
Correct |
2 ms |
348 KB |
Output is correct |
26 |
Correct |
13 ms |
1224 KB |
Output is correct |
27 |
Correct |
2 ms |
860 KB |
Output is correct |
28 |
Correct |
364 ms |
22980 KB |
Output is correct |
29 |
Correct |
574 ms |
32600 KB |
Output is correct |
30 |
Correct |
718 ms |
32068 KB |
Output is correct |
31 |
Correct |
691 ms |
32208 KB |
Output is correct |
32 |
Correct |
574 ms |
31516 KB |
Output is correct |
33 |
Correct |
658 ms |
32068 KB |
Output is correct |
34 |
Correct |
701 ms |
32068 KB |
Output is correct |
35 |
Correct |
176 ms |
12524 KB |
Output is correct |
36 |
Correct |
686 ms |
32148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4952 KB |
Output is correct |
2 |
Correct |
5 ms |
4956 KB |
Output is correct |
3 |
Correct |
27 ms |
10076 KB |
Output is correct |
4 |
Correct |
22 ms |
8676 KB |
Output is correct |
5 |
Correct |
54 ms |
15364 KB |
Output is correct |
6 |
Correct |
50 ms |
14672 KB |
Output is correct |
7 |
Correct |
47 ms |
15184 KB |
Output is correct |
8 |
Correct |
63 ms |
15296 KB |
Output is correct |
9 |
Correct |
52 ms |
14928 KB |
Output is correct |
10 |
Correct |
53 ms |
12880 KB |
Output is correct |
11 |
Correct |
132 ms |
25800 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
5 ms |
4956 KB |
Output is correct |
19 |
Correct |
5 ms |
4956 KB |
Output is correct |
20 |
Correct |
5 ms |
4956 KB |
Output is correct |
21 |
Correct |
5 ms |
4956 KB |
Output is correct |
22 |
Correct |
100 ms |
15832 KB |
Output is correct |
23 |
Correct |
203 ms |
26052 KB |
Output is correct |
24 |
Correct |
145 ms |
26272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
157 ms |
21400 KB |
Output is correct |
2 |
Correct |
194 ms |
25176 KB |
Output is correct |
3 |
Correct |
5 ms |
5112 KB |
Output is correct |
4 |
Correct |
7 ms |
4956 KB |
Output is correct |
5 |
Correct |
865 ms |
53740 KB |
Output is correct |
6 |
Correct |
481 ms |
37832 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
363 ms |
38608 KB |
Output is correct |
9 |
Correct |
480 ms |
44852 KB |
Output is correct |
10 |
Correct |
143 ms |
21204 KB |
Output is correct |
11 |
Correct |
167 ms |
25232 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
5 ms |
4956 KB |
Output is correct |
17 |
Correct |
7 ms |
4956 KB |
Output is correct |
18 |
Correct |
163 ms |
25552 KB |
Output is correct |
19 |
Correct |
232 ms |
29904 KB |
Output is correct |
20 |
Correct |
155 ms |
21996 KB |
Output is correct |
21 |
Correct |
165 ms |
23516 KB |
Output is correct |
22 |
Correct |
138 ms |
22448 KB |
Output is correct |
23 |
Correct |
180 ms |
24692 KB |
Output is correct |
24 |
Correct |
5 ms |
4952 KB |
Output is correct |
25 |
Correct |
5 ms |
4956 KB |
Output is correct |
26 |
Correct |
27 ms |
10076 KB |
Output is correct |
27 |
Correct |
22 ms |
8676 KB |
Output is correct |
28 |
Correct |
54 ms |
15364 KB |
Output is correct |
29 |
Correct |
50 ms |
14672 KB |
Output is correct |
30 |
Correct |
47 ms |
15184 KB |
Output is correct |
31 |
Correct |
63 ms |
15296 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
344 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
2 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
448 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
444 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
3 ms |
688 KB |
Output is correct |
48 |
Correct |
61 ms |
5252 KB |
Output is correct |
49 |
Correct |
58 ms |
5164 KB |
Output is correct |
50 |
Correct |
55 ms |
4944 KB |
Output is correct |
51 |
Correct |
60 ms |
4952 KB |
Output is correct |
52 |
Correct |
53 ms |
4956 KB |
Output is correct |
53 |
Correct |
119 ms |
9820 KB |
Output is correct |
54 |
Correct |
15 ms |
1372 KB |
Output is correct |
55 |
Correct |
52 ms |
3568 KB |
Output is correct |
56 |
Correct |
2 ms |
348 KB |
Output is correct |
57 |
Correct |
13 ms |
1224 KB |
Output is correct |
58 |
Correct |
2 ms |
860 KB |
Output is correct |
59 |
Correct |
364 ms |
22980 KB |
Output is correct |
60 |
Correct |
574 ms |
32600 KB |
Output is correct |
61 |
Correct |
718 ms |
32068 KB |
Output is correct |
62 |
Correct |
691 ms |
32208 KB |
Output is correct |
63 |
Correct |
574 ms |
31516 KB |
Output is correct |
64 |
Correct |
658 ms |
32068 KB |
Output is correct |
65 |
Correct |
701 ms |
32068 KB |
Output is correct |
66 |
Correct |
176 ms |
12524 KB |
Output is correct |
67 |
Correct |
686 ms |
32148 KB |
Output is correct |
68 |
Correct |
52 ms |
14928 KB |
Output is correct |
69 |
Correct |
53 ms |
12880 KB |
Output is correct |
70 |
Correct |
132 ms |
25800 KB |
Output is correct |
71 |
Correct |
0 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
0 ms |
348 KB |
Output is correct |
74 |
Correct |
0 ms |
348 KB |
Output is correct |
75 |
Correct |
0 ms |
348 KB |
Output is correct |
76 |
Correct |
0 ms |
344 KB |
Output is correct |
77 |
Correct |
5 ms |
4956 KB |
Output is correct |
78 |
Correct |
5 ms |
4956 KB |
Output is correct |
79 |
Correct |
5 ms |
4956 KB |
Output is correct |
80 |
Correct |
5 ms |
4956 KB |
Output is correct |
81 |
Correct |
100 ms |
15832 KB |
Output is correct |
82 |
Correct |
203 ms |
26052 KB |
Output is correct |
83 |
Correct |
145 ms |
26272 KB |
Output is correct |
84 |
Correct |
899 ms |
43128 KB |
Output is correct |
85 |
Correct |
839 ms |
44172 KB |
Output is correct |
86 |
Correct |
459 ms |
36524 KB |
Output is correct |
87 |
Correct |
397 ms |
37204 KB |
Output is correct |
88 |
Correct |
0 ms |
348 KB |
Output is correct |
89 |
Correct |
396 ms |
37304 KB |
Output is correct |
90 |
Correct |
422 ms |
36296 KB |
Output is correct |
91 |
Correct |
347 ms |
36056 KB |
Output is correct |