#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using vpii = vector<pii>;
using vvpii = vector<vpii>;
using vi = vector<int>;
using vvi = vector<vi>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
#define sz(x) int(x.size())
void selfmax(ll& a, ll b)
{
a = max(a, b);
}
const int maxN = 100'000;
vpll fish[1 + maxN];
vll fishpref[1 + maxN];
vll tot(1 + maxN, 0);
ll max_weights(int N, int M, vi X, vi Y, vi W)
{
vvll sm(2, vll(N, 0));
for(int j = 0; j < M; j++)
if(X[j] < 2)
sm[X[j]][Y[j]] += W[j];
if(N == 2)
{
return max(sm[0][0] + sm[0][1], sm[1][0] + sm[1][1]);
}
int xmx = 0;
for(int j = 0; j < M; j++)
{
X[j]++;
xmx = max(xmx, X[j] + 2);
Y[j]++;
}
xmx = min(xmx, N);
// cerr << "xmx = " << xmx << '\n';
vpll fish[1+N];
ll* fishpref[1+N];
vpii fishbyY[1+N];
for(int j = 0; j < M; j++)
{
// fish[X[j]].push_back({Y[j], W[j]});
fishbyY[Y[j]].push_back({X[j], W[j]});
tot[X[j]] += W[j];
}
for(int y = 0; y <= N; y++)
for(pii z : fishbyY[y])
fish[z.first].push_back({y, z.second});
for(int r = 1; r <= xmx; r++)
{
fish[r].push_back({N+1, 0});
// sort(fish[r].begin(), fish[r].end());
if(fish[r][0].first != 1)
{
fish[r].insert(fish[r].begin(), {1, 0});
}
fishpref[r] = new ll[sz(fish[r])];
for(int i = 0; i < sz(fish[r]); i++)
{
fishpref[r][i] = (i == 0 ? 0 : fishpref[r][i-1]) + fish[r][i].second;
}
}
fish[0] = vpll{{0, 0}};
fishpref[0] = new ll[1];
fishpref[0][0] = 0;
// vvll inc(1+N), dec(1+N);
ll* inc[1+N];
ll* dec[1+N];
ll* incpref[1+N];
ll* decpref[1+N]; //pref inc dec mx
inc[0] = new ll[1];
inc[0][0] = 0;
dec[0] = new ll[1];
dec[0][0] = 0;
ll res = 0;
// cerr << "done\n";
for(int r = 1; r <= xmx; r++)
{
incpref[r-1] = new ll[sz(fish[r-1])];
decpref[r-1] = new ll[sz(fish[r-1])];
incpref[r-1][0] = decpref[r-1][0] = 0;
for(int i = 1; i < sz(fish[r-1]); i++)
{
incpref[r-1][i] = max(incpref[r-1][i-1], inc[r-1][i]);
decpref[r-1][i] = max(decpref[r-1][i-1], dec[r-1][i]);
}
// inc[r] = dec[r] = vll(sz(fish[r]), 0);
inc[r] = new ll[sz(fish[r])];
dec[r] = new ll[sz(fish[r])];
for(int i = 0; i < sz(fish[r]); i++)
inc[r][i] = dec[r][i] = 0;
vll Csuff;
if(r >= 2)
{
Csuff = vll(sz(fish[r-2]));
int pi = sz(fish[r-1])-1;
ll pwt = tot[r-1];
for(int j = sz(fish[r-2])-1; j >= 0; j--)
{
while(pi >= 0 && fish[r-1][pi].first > fish[r-2][j].first - 1)
{
pwt -= fish[r-1][pi].second;
pi--;
}
// Csuff[j] = max(inc[r-2][j], dec[r-2][j]) + htwt(r-1, fish[r-2][j].first - 1);
Csuff[j] = max(inc[r-2][j], dec[r-2][j]) + pwt;
if(j+1 < sz(fish[r-2]))
selfmax(Csuff[j], Csuff[j+1]);
}
}
ll Dmx = 0;
ll Did = -1;
int ppi = -1;
int qi = -1;
ll qwt = 0;
for(int i = 0; i < sz(fish[r]); i++)
{
ll basic = 0;
ll Bcatch = 0;
// int Bk = -1;
ll oldqwt = qwt;
while(qi+1 < sz(fish[r-1]) && fish[r-1][qi+1].first <= fish[r][i].first - 1)
{
qi++;
qwt += fish[r-1][qi].second;
}
ll prevpwt = qwt;
ll constD = -(tot[r-1] - prevpwt) + tot[r-1];
if(r >= 2)
{
//A
selfmax(basic, max(inc[r-2][0], dec[r-2][0]) + prevpwt);
while(ppi+1 < sz(fish[r-2]) && fish[r-2][ppi+1].first <= fish[r][i].first)
ppi++;
int ploci = ppi;
// while(Bk+1 < sz(fish[r-1]) && fish[r-1][Bk+1].first <= fish[r][i].first - 1)
// {
// Bk++;
// Bcatch += fish[r-1][Bk].second;
// }
Bcatch = qwt;
// assert(Bcatch == qwt);
// assert(Bk == qi);
selfmax(basic, Bcatch + max(incpref[r-2][ploci], decpref[r-2][ploci]));
if(ploci+1 < sz(fish[r-2]))
{
// cerr << ploci << " : " << sz(fish[r-2]) << '\n';
selfmax(basic, Csuff[ploci+1]);
}
}
selfmax(inc[r][i], basic);
selfmax(dec[r][i], basic);
//type D transition
while(Did+1 < sz(fish[r-1]) && fish[r-1][Did+1].first <= fish[r][i].first)
{
Did++;
Dmx = max(Dmx, inc[r-1][Did] - (Did >= 1 ? fishpref[r-1][Did-1] : 0));
}
selfmax(inc[r][i], Dmx + constD);
selfmax(dec[r][i], Dmx + constD);
}
int j = sz(fish[r-1]);
ll bestval = -1'000'000'000'000'000'000LL;
int ri = sz(fish[r]);
ll rwt = 0;
for(int i = sz(fish[r])-1; i >= 0; i--)
{
while(j-1 >= 0 && fish[r-1][j-1].first > fish[r][i].first)
{
j--;
while(ri-1 >= 0 && fish[r][ri-1].first >= fish[r-1][j].first)
{
ri--;
rwt += fish[r][ri].second;
}
bestval = max(bestval, dec[r-1][j] + tot[r] - rwt);
}
selfmax(dec[r][i], bestval - (i >= 1 ? fishpref[r][i-1] : 0));
}
for(int i = 0; i < sz(fish[r]); i++)
selfmax(res, max(inc[r][i], dec[r][i]));
}
return res;
}
Compilation message
fish.cpp: In function 'll max_weights(int, int, vi, vi, vi)':
fish.cpp:166:7: warning: unused variable 'oldqwt' [-Wunused-variable]
166 | ll oldqwt = qwt;
| ^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
24420 KB |
Output is correct |
2 |
Correct |
62 ms |
27636 KB |
Output is correct |
3 |
Correct |
9 ms |
15940 KB |
Output is correct |
4 |
Correct |
10 ms |
15856 KB |
Output is correct |
5 |
Correct |
212 ms |
64068 KB |
Output is correct |
6 |
Correct |
297 ms |
65660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5716 KB |
Output is correct |
2 |
Correct |
91 ms |
30956 KB |
Output is correct |
3 |
Correct |
108 ms |
35444 KB |
Output is correct |
4 |
Correct |
50 ms |
24328 KB |
Output is correct |
5 |
Correct |
61 ms |
27680 KB |
Output is correct |
6 |
Correct |
3 ms |
5716 KB |
Output is correct |
7 |
Correct |
4 ms |
5716 KB |
Output is correct |
8 |
Correct |
3 ms |
5764 KB |
Output is correct |
9 |
Correct |
3 ms |
5728 KB |
Output is correct |
10 |
Correct |
11 ms |
15940 KB |
Output is correct |
11 |
Correct |
9 ms |
15940 KB |
Output is correct |
12 |
Correct |
50 ms |
24368 KB |
Output is correct |
13 |
Correct |
63 ms |
27704 KB |
Output is correct |
14 |
Correct |
48 ms |
24084 KB |
Output is correct |
15 |
Correct |
54 ms |
24848 KB |
Output is correct |
16 |
Correct |
53 ms |
24076 KB |
Output is correct |
17 |
Correct |
61 ms |
26276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
15940 KB |
Output is correct |
2 |
Correct |
47 ms |
36228 KB |
Output is correct |
3 |
Correct |
62 ms |
34656 KB |
Output is correct |
4 |
Correct |
59 ms |
35376 KB |
Output is correct |
5 |
Correct |
90 ms |
39356 KB |
Output is correct |
6 |
Correct |
84 ms |
39424 KB |
Output is correct |
7 |
Correct |
88 ms |
39460 KB |
Output is correct |
8 |
Correct |
99 ms |
39356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5716 KB |
Output is correct |
2 |
Correct |
4 ms |
5716 KB |
Output is correct |
3 |
Correct |
3 ms |
5716 KB |
Output is correct |
4 |
Correct |
3 ms |
5716 KB |
Output is correct |
5 |
Correct |
4 ms |
5716 KB |
Output is correct |
6 |
Correct |
3 ms |
5716 KB |
Output is correct |
7 |
Correct |
3 ms |
5716 KB |
Output is correct |
8 |
Correct |
3 ms |
5716 KB |
Output is correct |
9 |
Correct |
4 ms |
5844 KB |
Output is correct |
10 |
Correct |
5 ms |
6100 KB |
Output is correct |
11 |
Correct |
4 ms |
5880 KB |
Output is correct |
12 |
Correct |
4 ms |
5972 KB |
Output is correct |
13 |
Correct |
4 ms |
5792 KB |
Output is correct |
14 |
Correct |
4 ms |
5844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5716 KB |
Output is correct |
2 |
Correct |
4 ms |
5716 KB |
Output is correct |
3 |
Correct |
3 ms |
5716 KB |
Output is correct |
4 |
Correct |
3 ms |
5716 KB |
Output is correct |
5 |
Correct |
4 ms |
5716 KB |
Output is correct |
6 |
Correct |
3 ms |
5716 KB |
Output is correct |
7 |
Correct |
3 ms |
5716 KB |
Output is correct |
8 |
Correct |
3 ms |
5716 KB |
Output is correct |
9 |
Correct |
4 ms |
5844 KB |
Output is correct |
10 |
Correct |
5 ms |
6100 KB |
Output is correct |
11 |
Correct |
4 ms |
5880 KB |
Output is correct |
12 |
Correct |
4 ms |
5972 KB |
Output is correct |
13 |
Correct |
4 ms |
5792 KB |
Output is correct |
14 |
Correct |
4 ms |
5844 KB |
Output is correct |
15 |
Correct |
3 ms |
5844 KB |
Output is correct |
16 |
Correct |
5 ms |
5972 KB |
Output is correct |
17 |
Correct |
21 ms |
10164 KB |
Output is correct |
18 |
Correct |
26 ms |
10704 KB |
Output is correct |
19 |
Correct |
21 ms |
10580 KB |
Output is correct |
20 |
Correct |
20 ms |
10580 KB |
Output is correct |
21 |
Correct |
19 ms |
10544 KB |
Output is correct |
22 |
Correct |
37 ms |
15300 KB |
Output is correct |
23 |
Correct |
7 ms |
6740 KB |
Output is correct |
24 |
Correct |
16 ms |
8704 KB |
Output is correct |
25 |
Correct |
3 ms |
5972 KB |
Output is correct |
26 |
Correct |
7 ms |
6656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5716 KB |
Output is correct |
2 |
Correct |
4 ms |
5716 KB |
Output is correct |
3 |
Correct |
3 ms |
5716 KB |
Output is correct |
4 |
Correct |
3 ms |
5716 KB |
Output is correct |
5 |
Correct |
4 ms |
5716 KB |
Output is correct |
6 |
Correct |
3 ms |
5716 KB |
Output is correct |
7 |
Correct |
3 ms |
5716 KB |
Output is correct |
8 |
Correct |
3 ms |
5716 KB |
Output is correct |
9 |
Correct |
4 ms |
5844 KB |
Output is correct |
10 |
Correct |
5 ms |
6100 KB |
Output is correct |
11 |
Correct |
4 ms |
5880 KB |
Output is correct |
12 |
Correct |
4 ms |
5972 KB |
Output is correct |
13 |
Correct |
4 ms |
5792 KB |
Output is correct |
14 |
Correct |
4 ms |
5844 KB |
Output is correct |
15 |
Correct |
3 ms |
5844 KB |
Output is correct |
16 |
Correct |
5 ms |
5972 KB |
Output is correct |
17 |
Correct |
21 ms |
10164 KB |
Output is correct |
18 |
Correct |
26 ms |
10704 KB |
Output is correct |
19 |
Correct |
21 ms |
10580 KB |
Output is correct |
20 |
Correct |
20 ms |
10580 KB |
Output is correct |
21 |
Correct |
19 ms |
10544 KB |
Output is correct |
22 |
Correct |
37 ms |
15300 KB |
Output is correct |
23 |
Correct |
7 ms |
6740 KB |
Output is correct |
24 |
Correct |
16 ms |
8704 KB |
Output is correct |
25 |
Correct |
3 ms |
5972 KB |
Output is correct |
26 |
Correct |
7 ms |
6656 KB |
Output is correct |
27 |
Correct |
6 ms |
6868 KB |
Output is correct |
28 |
Correct |
84 ms |
28240 KB |
Output is correct |
29 |
Correct |
138 ms |
34892 KB |
Output is correct |
30 |
Correct |
132 ms |
34640 KB |
Output is correct |
31 |
Correct |
134 ms |
34676 KB |
Output is correct |
32 |
Correct |
118 ms |
37464 KB |
Output is correct |
33 |
Correct |
167 ms |
34668 KB |
Output is correct |
34 |
Correct |
130 ms |
34636 KB |
Output is correct |
35 |
Correct |
53 ms |
17632 KB |
Output is correct |
36 |
Correct |
134 ms |
35680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
15940 KB |
Output is correct |
2 |
Correct |
47 ms |
36228 KB |
Output is correct |
3 |
Correct |
62 ms |
34656 KB |
Output is correct |
4 |
Correct |
59 ms |
35376 KB |
Output is correct |
5 |
Correct |
90 ms |
39356 KB |
Output is correct |
6 |
Correct |
84 ms |
39424 KB |
Output is correct |
7 |
Correct |
88 ms |
39460 KB |
Output is correct |
8 |
Correct |
99 ms |
39356 KB |
Output is correct |
9 |
Correct |
97 ms |
44836 KB |
Output is correct |
10 |
Correct |
79 ms |
26412 KB |
Output is correct |
11 |
Correct |
177 ms |
46824 KB |
Output is correct |
12 |
Correct |
3 ms |
5716 KB |
Output is correct |
13 |
Correct |
3 ms |
5716 KB |
Output is correct |
14 |
Correct |
4 ms |
5716 KB |
Output is correct |
15 |
Correct |
3 ms |
5716 KB |
Output is correct |
16 |
Correct |
3 ms |
5716 KB |
Output is correct |
17 |
Correct |
3 ms |
5716 KB |
Output is correct |
18 |
Correct |
9 ms |
15908 KB |
Output is correct |
19 |
Correct |
9 ms |
15940 KB |
Output is correct |
20 |
Correct |
46 ms |
36228 KB |
Output is correct |
21 |
Correct |
46 ms |
36284 KB |
Output is correct |
22 |
Correct |
118 ms |
45452 KB |
Output is correct |
23 |
Correct |
174 ms |
53304 KB |
Output is correct |
24 |
Correct |
152 ms |
54584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
24420 KB |
Output is correct |
2 |
Correct |
62 ms |
27636 KB |
Output is correct |
3 |
Correct |
9 ms |
15940 KB |
Output is correct |
4 |
Correct |
10 ms |
15856 KB |
Output is correct |
5 |
Correct |
212 ms |
64068 KB |
Output is correct |
6 |
Correct |
297 ms |
65660 KB |
Output is correct |
7 |
Correct |
3 ms |
5716 KB |
Output is correct |
8 |
Correct |
91 ms |
30956 KB |
Output is correct |
9 |
Correct |
108 ms |
35444 KB |
Output is correct |
10 |
Correct |
50 ms |
24328 KB |
Output is correct |
11 |
Correct |
61 ms |
27680 KB |
Output is correct |
12 |
Correct |
3 ms |
5716 KB |
Output is correct |
13 |
Correct |
4 ms |
5716 KB |
Output is correct |
14 |
Correct |
3 ms |
5764 KB |
Output is correct |
15 |
Correct |
3 ms |
5728 KB |
Output is correct |
16 |
Correct |
11 ms |
15940 KB |
Output is correct |
17 |
Correct |
9 ms |
15940 KB |
Output is correct |
18 |
Correct |
50 ms |
24368 KB |
Output is correct |
19 |
Correct |
63 ms |
27704 KB |
Output is correct |
20 |
Correct |
48 ms |
24084 KB |
Output is correct |
21 |
Correct |
54 ms |
24848 KB |
Output is correct |
22 |
Correct |
53 ms |
24076 KB |
Output is correct |
23 |
Correct |
61 ms |
26276 KB |
Output is correct |
24 |
Correct |
12 ms |
15940 KB |
Output is correct |
25 |
Correct |
47 ms |
36228 KB |
Output is correct |
26 |
Correct |
62 ms |
34656 KB |
Output is correct |
27 |
Correct |
59 ms |
35376 KB |
Output is correct |
28 |
Correct |
90 ms |
39356 KB |
Output is correct |
29 |
Correct |
84 ms |
39424 KB |
Output is correct |
30 |
Correct |
88 ms |
39460 KB |
Output is correct |
31 |
Correct |
99 ms |
39356 KB |
Output is correct |
32 |
Correct |
3 ms |
5716 KB |
Output is correct |
33 |
Correct |
4 ms |
5716 KB |
Output is correct |
34 |
Correct |
3 ms |
5716 KB |
Output is correct |
35 |
Correct |
3 ms |
5716 KB |
Output is correct |
36 |
Correct |
4 ms |
5716 KB |
Output is correct |
37 |
Correct |
3 ms |
5716 KB |
Output is correct |
38 |
Correct |
3 ms |
5716 KB |
Output is correct |
39 |
Correct |
3 ms |
5716 KB |
Output is correct |
40 |
Correct |
4 ms |
5844 KB |
Output is correct |
41 |
Correct |
5 ms |
6100 KB |
Output is correct |
42 |
Correct |
4 ms |
5880 KB |
Output is correct |
43 |
Correct |
4 ms |
5972 KB |
Output is correct |
44 |
Correct |
4 ms |
5792 KB |
Output is correct |
45 |
Correct |
4 ms |
5844 KB |
Output is correct |
46 |
Correct |
3 ms |
5844 KB |
Output is correct |
47 |
Correct |
5 ms |
5972 KB |
Output is correct |
48 |
Correct |
21 ms |
10164 KB |
Output is correct |
49 |
Correct |
26 ms |
10704 KB |
Output is correct |
50 |
Correct |
21 ms |
10580 KB |
Output is correct |
51 |
Correct |
20 ms |
10580 KB |
Output is correct |
52 |
Correct |
19 ms |
10544 KB |
Output is correct |
53 |
Correct |
37 ms |
15300 KB |
Output is correct |
54 |
Correct |
7 ms |
6740 KB |
Output is correct |
55 |
Correct |
16 ms |
8704 KB |
Output is correct |
56 |
Correct |
3 ms |
5972 KB |
Output is correct |
57 |
Correct |
7 ms |
6656 KB |
Output is correct |
58 |
Correct |
6 ms |
6868 KB |
Output is correct |
59 |
Correct |
84 ms |
28240 KB |
Output is correct |
60 |
Correct |
138 ms |
34892 KB |
Output is correct |
61 |
Correct |
132 ms |
34640 KB |
Output is correct |
62 |
Correct |
134 ms |
34676 KB |
Output is correct |
63 |
Correct |
118 ms |
37464 KB |
Output is correct |
64 |
Correct |
167 ms |
34668 KB |
Output is correct |
65 |
Correct |
130 ms |
34636 KB |
Output is correct |
66 |
Correct |
53 ms |
17632 KB |
Output is correct |
67 |
Correct |
134 ms |
35680 KB |
Output is correct |
68 |
Correct |
97 ms |
44836 KB |
Output is correct |
69 |
Correct |
79 ms |
26412 KB |
Output is correct |
70 |
Correct |
177 ms |
46824 KB |
Output is correct |
71 |
Correct |
3 ms |
5716 KB |
Output is correct |
72 |
Correct |
3 ms |
5716 KB |
Output is correct |
73 |
Correct |
4 ms |
5716 KB |
Output is correct |
74 |
Correct |
3 ms |
5716 KB |
Output is correct |
75 |
Correct |
3 ms |
5716 KB |
Output is correct |
76 |
Correct |
3 ms |
5716 KB |
Output is correct |
77 |
Correct |
9 ms |
15908 KB |
Output is correct |
78 |
Correct |
9 ms |
15940 KB |
Output is correct |
79 |
Correct |
46 ms |
36228 KB |
Output is correct |
80 |
Correct |
46 ms |
36284 KB |
Output is correct |
81 |
Correct |
118 ms |
45452 KB |
Output is correct |
82 |
Correct |
174 ms |
53304 KB |
Output is correct |
83 |
Correct |
152 ms |
54584 KB |
Output is correct |
84 |
Correct |
175 ms |
47520 KB |
Output is correct |
85 |
Correct |
169 ms |
49664 KB |
Output is correct |
86 |
Correct |
316 ms |
65632 KB |
Output is correct |
87 |
Correct |
274 ms |
67808 KB |
Output is correct |
88 |
Correct |
3 ms |
5716 KB |
Output is correct |
89 |
Correct |
293 ms |
68660 KB |
Output is correct |
90 |
Correct |
210 ms |
54444 KB |
Output is correct |
91 |
Correct |
154 ms |
50404 KB |
Output is correct |