#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 1e5+10, MOD = 1e9+7;
const ll INF = 1e18+10;
vector<pair<int, int>> fish[N];
vector<int> use[N];
ll max_weights(int n, int m, vector<int> x, vector<int> y, vector<int> w) {
for (int i = 0; i < m; i++) {
fish[x[i]].emplace_back(y[i], w[i]);
if (x[i]) use[x[i]-1].push_back(y[i]);
if (x[i] < n-1) use[x[i]+1].push_back(y[i]);
}
for (int i = 0; i < n; i++) {
use[i].push_back(-1);
sort(use[i].begin(), use[i].end());
sort(fish[i].begin(), fish[i].end());
}
vector<ll> prv_l{0}, prv_r{0};
vector<int> prv_h{-1};
vector<pair<int, int>> prv_fish;
for (int c = 0; c < n; c++) {
int k = sz(use[c]);
vector<ll> cur_l(k), cur_r(k);
auto cur_h = use[c];
auto cur_fish = fish[c];
vector<ll> ps_prv(sz(prv_fish));
vector<ll> ps_cur(sz(cur_fish));
for (int i = 0; i < sz(prv_fish); i++) {
ps_prv[i] = prv_fish[i].second;
if (i) ps_prv[i] += ps_prv[i-1];
}
for (int i = 0; i < sz(cur_fish); i++) {
ps_cur[i] = cur_fish[i].second;
if (i) ps_cur[i] += ps_cur[i-1];
}
vector<ll> my_ps_prv(sz(cur_h));
vector<ll> his_ps_prv(sz(prv_h));
for (int j = 0; j < sz(cur_h); j++) {
my_ps_prv[j] = upper_bound(prv_fish.begin(), prv_fish.end(), pair<int, int>{cur_h[j], MOD}) - prv_fish.begin() - 1;
if (my_ps_prv[j] < 0) my_ps_prv[j] = 0;
else my_ps_prv[j] = ps_prv[my_ps_prv[j]];
}
for (int i = 0; i < sz(prv_h); i++) {
his_ps_prv[i] = upper_bound(prv_fish.begin(), prv_fish.end(), pair<int, int>{prv_h[i], MOD}) - prv_fish.begin() - 1;
if (his_ps_prv[i] < 0) his_ps_prv[i] = 0;
else his_ps_prv[i] = ps_prv[his_ps_prv[i]];
}
vector<ll> my_ps_cur(sz(cur_h));
vector<ll> his_ps_cur(sz(prv_h));
for (int j = 0; j < sz(cur_h); j++) {
my_ps_cur[j] = upper_bound(cur_fish.begin(), cur_fish.end(), pair<int, int>{cur_h[j], MOD}) - cur_fish.begin() - 1;
if (my_ps_cur[j] < 0) my_ps_cur[j] = 0;
else my_ps_cur[j] = ps_cur[my_ps_cur[j]];
}
for (int i = 0; i < sz(prv_h); i++) {
his_ps_cur[i] = upper_bound(cur_fish.begin(), cur_fish.end(), pair<int, int>{prv_h[i], MOD}) - cur_fish.begin() - 1;
if (his_ps_cur[i] < 0) his_ps_cur[i] = 0;
else his_ps_cur[i] = ps_cur[his_ps_cur[i]];
}
ll mx_prv_l = *max_element(prv_l.begin(), prv_l.end());
ll mx_prv_r = *max_element(prv_r.begin(), prv_r.end());
ll best1 = -INF;
ll best2 = -INF;
ll best3 = -INF;
ll best4 = -INF;
ll best5 = -INF;
for (int i = 0; i < sz(prv_h); i++) {
best1 = max(best1, prv_r[i] - his_ps_prv[i]);
best2 = max(best2, prv_r[i] - his_ps_prv[i] + his_ps_cur[i]);
best3 = max(best3, prv_r[i] - his_ps_prv[i]);
best4 = max(best4, prv_r[i] + his_ps_cur[i]);
best5 = max(best5, prv_l[i] + his_ps_cur[i]);
}
for (int j = 0; j < sz(cur_h); j++) {
cur_r[j] = max(cur_r[j], mx_prv_l);
cur_r[j] = max(cur_r[j], mx_prv_r);
cur_r[j] = max(cur_r[j], my_ps_prv[j] + best1);
cur_l[j] = max(cur_l[j], best2 + my_ps_prv[j] - my_ps_cur[j]);
cur_l[j] = max(cur_l[j], best3 + my_ps_prv[j]);
cur_l[j] = max(cur_l[j], best4 - my_ps_cur[j]);
cur_l[j] = max(cur_l[j], mx_prv_r);
cur_l[j] = max(cur_l[j], best5 - my_ps_cur[j]);
cur_l[j] = max(cur_l[j], mx_prv_l);
/*
for (int i = 0; i < sz(prv_h); i++) {
ll cost_prv = prv_r[i] + max(my_ps_prv[j] - his_ps_prv[i], 0LL);
cost_prv = max(cost_prv, prv_l[i]);
cost_prv += max(his_ps_cur[i] - my_ps_cur[j], 0LL);
cur_l[j] = max(cur_l[j], cost_prv);
}
*/
}
swap(prv_l, cur_l);
swap(prv_r, cur_r);
swap(prv_h, cur_h);
swap(prv_fish, cur_fish);
}
ll ans = 0;
for (ll x : prv_l) ans = max(ans, x);
for (ll x : prv_r) ans = max(ans, x);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
14780 KB |
Output is correct |
2 |
Correct |
69 ms |
16708 KB |
Output is correct |
3 |
Correct |
21 ms |
8128 KB |
Output is correct |
4 |
Correct |
21 ms |
8132 KB |
Output is correct |
5 |
Correct |
193 ms |
24824 KB |
Output is correct |
6 |
Correct |
177 ms |
22088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
119 ms |
22272 KB |
Output is correct |
3 |
Correct |
151 ms |
29352 KB |
Output is correct |
4 |
Correct |
57 ms |
16172 KB |
Output is correct |
5 |
Correct |
69 ms |
18408 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4888 KB |
Output is correct |
8 |
Correct |
2 ms |
4932 KB |
Output is correct |
9 |
Correct |
2 ms |
4996 KB |
Output is correct |
10 |
Correct |
21 ms |
8128 KB |
Output is correct |
11 |
Correct |
21 ms |
8020 KB |
Output is correct |
12 |
Correct |
67 ms |
16696 KB |
Output is correct |
13 |
Correct |
80 ms |
19012 KB |
Output is correct |
14 |
Correct |
64 ms |
16488 KB |
Output is correct |
15 |
Correct |
77 ms |
17652 KB |
Output is correct |
16 |
Correct |
64 ms |
16484 KB |
Output is correct |
17 |
Correct |
74 ms |
17644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
8124 KB |
Output is correct |
2 |
Correct |
21 ms |
8020 KB |
Output is correct |
3 |
Correct |
45 ms |
10764 KB |
Output is correct |
4 |
Correct |
38 ms |
10108 KB |
Output is correct |
5 |
Correct |
83 ms |
13520 KB |
Output is correct |
6 |
Correct |
73 ms |
13608 KB |
Output is correct |
7 |
Correct |
87 ms |
13504 KB |
Output is correct |
8 |
Correct |
75 ms |
13604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4936 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
5172 KB |
Output is correct |
11 |
Correct |
3 ms |
5020 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
4996 KB |
Output is correct |
14 |
Correct |
3 ms |
5048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4936 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
5172 KB |
Output is correct |
11 |
Correct |
3 ms |
5020 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
4996 KB |
Output is correct |
14 |
Correct |
3 ms |
5048 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
5 ms |
5076 KB |
Output is correct |
17 |
Correct |
30 ms |
6996 KB |
Output is correct |
18 |
Correct |
30 ms |
7596 KB |
Output is correct |
19 |
Correct |
24 ms |
7456 KB |
Output is correct |
20 |
Correct |
24 ms |
7380 KB |
Output is correct |
21 |
Correct |
24 ms |
7380 KB |
Output is correct |
22 |
Correct |
59 ms |
9812 KB |
Output is correct |
23 |
Correct |
10 ms |
5476 KB |
Output is correct |
24 |
Correct |
20 ms |
6420 KB |
Output is correct |
25 |
Correct |
3 ms |
5076 KB |
Output is correct |
26 |
Correct |
7 ms |
5452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4936 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
5172 KB |
Output is correct |
11 |
Correct |
3 ms |
5020 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
4996 KB |
Output is correct |
14 |
Correct |
3 ms |
5048 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
5 ms |
5076 KB |
Output is correct |
17 |
Correct |
30 ms |
6996 KB |
Output is correct |
18 |
Correct |
30 ms |
7596 KB |
Output is correct |
19 |
Correct |
24 ms |
7456 KB |
Output is correct |
20 |
Correct |
24 ms |
7380 KB |
Output is correct |
21 |
Correct |
24 ms |
7380 KB |
Output is correct |
22 |
Correct |
59 ms |
9812 KB |
Output is correct |
23 |
Correct |
10 ms |
5476 KB |
Output is correct |
24 |
Correct |
20 ms |
6420 KB |
Output is correct |
25 |
Correct |
3 ms |
5076 KB |
Output is correct |
26 |
Correct |
7 ms |
5452 KB |
Output is correct |
27 |
Correct |
5 ms |
5204 KB |
Output is correct |
28 |
Correct |
151 ms |
16572 KB |
Output is correct |
29 |
Correct |
198 ms |
19916 KB |
Output is correct |
30 |
Correct |
171 ms |
24368 KB |
Output is correct |
31 |
Correct |
197 ms |
24360 KB |
Output is correct |
32 |
Correct |
211 ms |
26912 KB |
Output is correct |
33 |
Correct |
175 ms |
24364 KB |
Output is correct |
34 |
Correct |
188 ms |
24364 KB |
Output is correct |
35 |
Correct |
64 ms |
13020 KB |
Output is correct |
36 |
Correct |
192 ms |
26160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
8124 KB |
Output is correct |
2 |
Correct |
21 ms |
8020 KB |
Output is correct |
3 |
Correct |
45 ms |
10764 KB |
Output is correct |
4 |
Correct |
38 ms |
10108 KB |
Output is correct |
5 |
Correct |
83 ms |
13520 KB |
Output is correct |
6 |
Correct |
73 ms |
13608 KB |
Output is correct |
7 |
Correct |
87 ms |
13504 KB |
Output is correct |
8 |
Correct |
75 ms |
13604 KB |
Output is correct |
9 |
Correct |
70 ms |
13604 KB |
Output is correct |
10 |
Correct |
69 ms |
12128 KB |
Output is correct |
11 |
Correct |
120 ms |
19012 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
4 ms |
5000 KB |
Output is correct |
16 |
Correct |
5 ms |
4948 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Correct |
23 ms |
8132 KB |
Output is correct |
19 |
Correct |
26 ms |
8020 KB |
Output is correct |
20 |
Correct |
22 ms |
8128 KB |
Output is correct |
21 |
Correct |
23 ms |
8020 KB |
Output is correct |
22 |
Correct |
104 ms |
13024 KB |
Output is correct |
23 |
Correct |
135 ms |
19208 KB |
Output is correct |
24 |
Correct |
133 ms |
19268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
14780 KB |
Output is correct |
2 |
Correct |
69 ms |
16708 KB |
Output is correct |
3 |
Correct |
21 ms |
8128 KB |
Output is correct |
4 |
Correct |
21 ms |
8132 KB |
Output is correct |
5 |
Correct |
193 ms |
24824 KB |
Output is correct |
6 |
Correct |
177 ms |
22088 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
119 ms |
22272 KB |
Output is correct |
9 |
Correct |
151 ms |
29352 KB |
Output is correct |
10 |
Correct |
57 ms |
16172 KB |
Output is correct |
11 |
Correct |
69 ms |
18408 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4888 KB |
Output is correct |
14 |
Correct |
2 ms |
4932 KB |
Output is correct |
15 |
Correct |
2 ms |
4996 KB |
Output is correct |
16 |
Correct |
21 ms |
8128 KB |
Output is correct |
17 |
Correct |
21 ms |
8020 KB |
Output is correct |
18 |
Correct |
67 ms |
16696 KB |
Output is correct |
19 |
Correct |
80 ms |
19012 KB |
Output is correct |
20 |
Correct |
64 ms |
16488 KB |
Output is correct |
21 |
Correct |
77 ms |
17652 KB |
Output is correct |
22 |
Correct |
64 ms |
16484 KB |
Output is correct |
23 |
Correct |
74 ms |
17644 KB |
Output is correct |
24 |
Correct |
21 ms |
8124 KB |
Output is correct |
25 |
Correct |
21 ms |
8020 KB |
Output is correct |
26 |
Correct |
45 ms |
10764 KB |
Output is correct |
27 |
Correct |
38 ms |
10108 KB |
Output is correct |
28 |
Correct |
83 ms |
13520 KB |
Output is correct |
29 |
Correct |
73 ms |
13608 KB |
Output is correct |
30 |
Correct |
87 ms |
13504 KB |
Output is correct |
31 |
Correct |
75 ms |
13604 KB |
Output is correct |
32 |
Correct |
2 ms |
4936 KB |
Output is correct |
33 |
Correct |
2 ms |
4948 KB |
Output is correct |
34 |
Correct |
2 ms |
4948 KB |
Output is correct |
35 |
Correct |
2 ms |
4948 KB |
Output is correct |
36 |
Correct |
2 ms |
4948 KB |
Output is correct |
37 |
Correct |
2 ms |
4948 KB |
Output is correct |
38 |
Correct |
2 ms |
4948 KB |
Output is correct |
39 |
Correct |
2 ms |
4948 KB |
Output is correct |
40 |
Correct |
2 ms |
4948 KB |
Output is correct |
41 |
Correct |
3 ms |
5172 KB |
Output is correct |
42 |
Correct |
3 ms |
5020 KB |
Output is correct |
43 |
Correct |
3 ms |
5076 KB |
Output is correct |
44 |
Correct |
3 ms |
4996 KB |
Output is correct |
45 |
Correct |
3 ms |
5048 KB |
Output is correct |
46 |
Correct |
3 ms |
4948 KB |
Output is correct |
47 |
Correct |
5 ms |
5076 KB |
Output is correct |
48 |
Correct |
30 ms |
6996 KB |
Output is correct |
49 |
Correct |
30 ms |
7596 KB |
Output is correct |
50 |
Correct |
24 ms |
7456 KB |
Output is correct |
51 |
Correct |
24 ms |
7380 KB |
Output is correct |
52 |
Correct |
24 ms |
7380 KB |
Output is correct |
53 |
Correct |
59 ms |
9812 KB |
Output is correct |
54 |
Correct |
10 ms |
5476 KB |
Output is correct |
55 |
Correct |
20 ms |
6420 KB |
Output is correct |
56 |
Correct |
3 ms |
5076 KB |
Output is correct |
57 |
Correct |
7 ms |
5452 KB |
Output is correct |
58 |
Correct |
5 ms |
5204 KB |
Output is correct |
59 |
Correct |
151 ms |
16572 KB |
Output is correct |
60 |
Correct |
198 ms |
19916 KB |
Output is correct |
61 |
Correct |
171 ms |
24368 KB |
Output is correct |
62 |
Correct |
197 ms |
24360 KB |
Output is correct |
63 |
Correct |
211 ms |
26912 KB |
Output is correct |
64 |
Correct |
175 ms |
24364 KB |
Output is correct |
65 |
Correct |
188 ms |
24364 KB |
Output is correct |
66 |
Correct |
64 ms |
13020 KB |
Output is correct |
67 |
Correct |
192 ms |
26160 KB |
Output is correct |
68 |
Correct |
70 ms |
13604 KB |
Output is correct |
69 |
Correct |
69 ms |
12128 KB |
Output is correct |
70 |
Correct |
120 ms |
19012 KB |
Output is correct |
71 |
Correct |
2 ms |
4948 KB |
Output is correct |
72 |
Correct |
2 ms |
4948 KB |
Output is correct |
73 |
Correct |
2 ms |
4948 KB |
Output is correct |
74 |
Correct |
4 ms |
5000 KB |
Output is correct |
75 |
Correct |
5 ms |
4948 KB |
Output is correct |
76 |
Correct |
2 ms |
4948 KB |
Output is correct |
77 |
Correct |
23 ms |
8132 KB |
Output is correct |
78 |
Correct |
26 ms |
8020 KB |
Output is correct |
79 |
Correct |
22 ms |
8128 KB |
Output is correct |
80 |
Correct |
23 ms |
8020 KB |
Output is correct |
81 |
Correct |
104 ms |
13024 KB |
Output is correct |
82 |
Correct |
135 ms |
19208 KB |
Output is correct |
83 |
Correct |
133 ms |
19268 KB |
Output is correct |
84 |
Correct |
227 ms |
29428 KB |
Output is correct |
85 |
Correct |
236 ms |
29628 KB |
Output is correct |
86 |
Correct |
192 ms |
28964 KB |
Output is correct |
87 |
Correct |
202 ms |
29388 KB |
Output is correct |
88 |
Correct |
3 ms |
4948 KB |
Output is correct |
89 |
Correct |
192 ms |
29360 KB |
Output is correct |
90 |
Correct |
182 ms |
27308 KB |
Output is correct |
91 |
Correct |
164 ms |
27188 KB |
Output is correct |