#include "bits/stdc++.h"
#include "fish.h"
//#define int64_t long long
#define all(v) v.begin() , v.end()
#define sz(a) (int64_t)a.size()
using namespace std;
const int64_t INF = 1e15;
long long max_weights(int N, int M, vector<int> X, vector<int> Y,vector<int> W){
int64_t n,m,ans=0;
n=N,m=M;
vector<array<int64_t,3>> ar(m);
for(int64_t i=0;i<m;i++){
ar[i][0]=X[i]+1;
ar[i][1]=Y[i]+1;
ar[i][2]=W[i];
}
vector<int64_t> consider[n+5];
vector<array<int64_t,2>> pre[n+5];
vector<array<int64_t,2>> fish[n+5];
for(int64_t i=0;i<m;i++){
if(ar[i][0]>1) consider[ar[i][0]-1].push_back(ar[i][1]);
if(ar[i][0]+1<=n) consider[ar[i][0]+1].push_back(ar[i][1]);
}
for(int64_t i=0;i<=n;i++){
consider[i].push_back(0);
sort(all(consider[i]));
consider[i].erase(unique(all(consider[i])),consider[i].end());
}
int64_t res[n+5];
memset(res,0,sizeof(res));
for(int64_t i=0;i<m;i++) fish[ar[i][0]].push_back({ar[i][1],ar[i][2]});
for(int64_t i=1;i<=n;i++){
fish[i].push_back({0,0});
sort(all(fish[i]));
int64_t cur=0;
for(auto x:fish[i]){
cur+=x[1];
pre[i].push_back({x[0],cur});
}
}
vector<int64_t> dp[n+5][2];
for(int64_t i=0;i<=n;i++){
dp[i][0].assign(sz(consider[i]),0);
dp[i][1].assign(sz(consider[i]),0);
}
auto get_sum=[&](int64_t i,int64_t j)->int64_t{
if(i<1 || i>n) return 0;
array<int64_t,2> bs={j+1,-1};
int64_t it=lower_bound(all(pre[i]),bs)-pre[i].begin();
if(it>0) return pre[i][it-1][1];
return 0;
};
for(int64_t i=1;i<=n;i++){
//start
for(int64_t j=0;j<sz(consider[i]);j++){
dp[i][0][j]=max(dp[i][0][j],(i>=3 ? res[i-3] : 0)+get_sum(i-1,consider[i][j]));
dp[i][1][j]=max(dp[i][1][j],(i>=3 ? res[i-3] : 0)
+get_sum(i-1,consider[i][j])+get_sum(i+1,consider[i][j]));
}
// calc increasing dp
vector<array<int64_t,2>> v;
int64_t cur=0;
for(int64_t j=0;j<sz(dp[i-1][0]);j++){
cur=max(cur,dp[i-1][0][j]-get_sum(i-1,consider[i-1][j]));
v.push_back({consider[i-1][j],cur});
}
auto get_val=[&](int64_t u,int64_t t)->int64_t{
array<int64_t,2> bs={u+1,-1};
int64_t it=lower_bound(all(v),bs)-v.begin();
if(it-t>=0 && it-t<sz(v)) return v[it-t][1];
return 0;
};
for(int64_t j=0;j<sz(consider[i]);j++){
int64_t u=consider[i][j];
int64_t val=get_sum(i-1,u)+get_val(u,1);
dp[i][0][j]=max(dp[i][0][j],val);
}
// calc decrasing dp
v.clear();
cur=0;
for(int64_t j=sz(dp[i-1][1])-1;j>=0;j--){
cur=max(cur,dp[i-1][1][j]);
v.push_back({consider[i-1][j],cur});
}
reverse(all(v));
for(int64_t j=0;j<sz(dp[i][1]);j++){
int64_t u=consider[i][j];
dp[i][1][j]=max(dp[i][1][j],get_val(u,0)-get_sum(i,u)+get_sum(i+1,u));
}
if(i>=2){ // new block
vector<array<int64_t,2>> arka[2];
cur=0;
for(int64_t j=0;j<sz(consider[i-2]);j++){
int64_t u=consider[i-2][j];
cur=max(cur,dp[i-2][0][j]);
cur=max(cur,dp[i-2][1][j]-get_sum(i-1,u));
arka[0].push_back({u,cur});
}
cur=0;
for(int64_t j=sz(consider[i-2])-1;j>=0;j--){
int64_t u=consider[i-2][j];
cur=max(cur,dp[i-2][0][j]+get_sum(i-1,u));
cur=max(cur,dp[i-2][1][j]);
arka[1].push_back({u,cur});
}
reverse(all(arka[1]));
auto get_kucuk=[&](int64_t u)->int64_t{
array<int64_t,2> bs={u+1,-1};
int64_t it=lower_bound(all(arka[0]),bs)-arka[0].begin();
if(it>0) return arka[0][it-1][1];
return 0;
};
auto get_buyuk=[&](int64_t u)->int64_t{
array<int64_t,2> bs={u+1,-1};
int64_t it=lower_bound(all(arka[1]),bs)-arka[1].begin();
if(it<sz(arka[1])) return arka[1][it][1];
return 0;
};
for(int64_t j=0;j<sz(consider[i]);j++){
int64_t u=consider[i][j];
dp[i][0][j]=max(dp[i][0][j],get_buyuk(u));
dp[i][0][j]=max(dp[i][0][j],get_kucuk(u)+get_sum(i-1,u));
}
for(int64_t j=0;j<sz(consider[i]);j++){
int64_t u=consider[i][j];
dp[i][1][j]=max(dp[i][1][j],get_buyuk(u)+get_sum(i+1,u));
dp[i][1][j]=max(dp[i][1][j],get_kucuk(u)+get_sum(i+1,u)+get_sum(i-1,u));
}
}
// inc to dec
v.clear();
cur=0;
for(int64_t j=sz(consider[i-1])-1;j>=0;j--){
int64_t u=consider[i-1][j];
cur=max(cur,dp[i-1][0][j]+get_sum(i,u));
v.push_back({u,cur});
}
reverse(all(v));
for(int64_t j=0;j<sz(consider[i]);j++){
int64_t u=consider[i][j];
dp[i][1][j]=max(dp[i][1][j],get_val(u,0)-get_sum(i,u)+get_sum(i+1,u));
}
for(int64_t j=0;j<sz(consider[i]);j++){
ans=max(ans,dp[i][0][j]+get_sum(i+1,consider[i][j]));
ans=max(ans,dp[i][1][j]);
}
res[i]=ans;
}
return max(res[n],ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
104 ms |
39896 KB |
Output is correct |
2 |
Correct |
122 ms |
45640 KB |
Output is correct |
3 |
Correct |
44 ms |
28392 KB |
Output is correct |
4 |
Correct |
44 ms |
28500 KB |
Output is correct |
5 |
Correct |
554 ms |
77512 KB |
Output is correct |
6 |
Correct |
377 ms |
77144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
295 ms |
52820 KB |
Output is correct |
3 |
Correct |
317 ms |
61440 KB |
Output is correct |
4 |
Correct |
105 ms |
39808 KB |
Output is correct |
5 |
Correct |
121 ms |
45620 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 |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
436 KB |
Output is correct |
10 |
Correct |
52 ms |
28600 KB |
Output is correct |
11 |
Correct |
46 ms |
28508 KB |
Output is correct |
12 |
Correct |
157 ms |
41820 KB |
Output is correct |
13 |
Correct |
181 ms |
48060 KB |
Output is correct |
14 |
Correct |
136 ms |
39396 KB |
Output is correct |
15 |
Correct |
162 ms |
43604 KB |
Output is correct |
16 |
Correct |
138 ms |
39168 KB |
Output is correct |
17 |
Correct |
150 ms |
43540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
28504 KB |
Output is correct |
2 |
Correct |
45 ms |
28404 KB |
Output is correct |
3 |
Correct |
77 ms |
31508 KB |
Output is correct |
4 |
Correct |
73 ms |
32596 KB |
Output is correct |
5 |
Correct |
111 ms |
40604 KB |
Output is correct |
6 |
Correct |
110 ms |
39944 KB |
Output is correct |
7 |
Correct |
109 ms |
40368 KB |
Output is correct |
8 |
Correct |
108 ms |
40532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
352 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 |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
3 ms |
860 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
352 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 |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
3 ms |
860 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
604 KB |
Output is correct |
17 |
Correct |
74 ms |
6576 KB |
Output is correct |
18 |
Correct |
63 ms |
8284 KB |
Output is correct |
19 |
Correct |
49 ms |
7768 KB |
Output is correct |
20 |
Correct |
43 ms |
7764 KB |
Output is correct |
21 |
Correct |
38 ms |
7772 KB |
Output is correct |
22 |
Correct |
99 ms |
15080 KB |
Output is correct |
23 |
Correct |
14 ms |
1880 KB |
Output is correct |
24 |
Correct |
55 ms |
4988 KB |
Output is correct |
25 |
Correct |
2 ms |
604 KB |
Output is correct |
26 |
Correct |
12 ms |
1856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
352 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 |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
3 ms |
860 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
604 KB |
Output is correct |
17 |
Correct |
74 ms |
6576 KB |
Output is correct |
18 |
Correct |
63 ms |
8284 KB |
Output is correct |
19 |
Correct |
49 ms |
7768 KB |
Output is correct |
20 |
Correct |
43 ms |
7764 KB |
Output is correct |
21 |
Correct |
38 ms |
7772 KB |
Output is correct |
22 |
Correct |
99 ms |
15080 KB |
Output is correct |
23 |
Correct |
14 ms |
1880 KB |
Output is correct |
24 |
Correct |
55 ms |
4988 KB |
Output is correct |
25 |
Correct |
2 ms |
604 KB |
Output is correct |
26 |
Correct |
12 ms |
1856 KB |
Output is correct |
27 |
Correct |
5 ms |
1372 KB |
Output is correct |
28 |
Correct |
352 ms |
34064 KB |
Output is correct |
29 |
Correct |
517 ms |
46668 KB |
Output is correct |
30 |
Correct |
608 ms |
48468 KB |
Output is correct |
31 |
Correct |
616 ms |
48720 KB |
Output is correct |
32 |
Correct |
407 ms |
47440 KB |
Output is correct |
33 |
Correct |
611 ms |
48788 KB |
Output is correct |
34 |
Correct |
632 ms |
48724 KB |
Output is correct |
35 |
Correct |
188 ms |
19968 KB |
Output is correct |
36 |
Correct |
659 ms |
52048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
28504 KB |
Output is correct |
2 |
Correct |
45 ms |
28404 KB |
Output is correct |
3 |
Correct |
77 ms |
31508 KB |
Output is correct |
4 |
Correct |
73 ms |
32596 KB |
Output is correct |
5 |
Correct |
111 ms |
40604 KB |
Output is correct |
6 |
Correct |
110 ms |
39944 KB |
Output is correct |
7 |
Correct |
109 ms |
40368 KB |
Output is correct |
8 |
Correct |
108 ms |
40532 KB |
Output is correct |
9 |
Correct |
139 ms |
40300 KB |
Output is correct |
10 |
Correct |
90 ms |
28500 KB |
Output is correct |
11 |
Correct |
189 ms |
56404 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 |
348 KB |
Output is correct |
18 |
Correct |
56 ms |
28496 KB |
Output is correct |
19 |
Correct |
47 ms |
28504 KB |
Output is correct |
20 |
Correct |
47 ms |
28508 KB |
Output is correct |
21 |
Correct |
44 ms |
28508 KB |
Output is correct |
22 |
Correct |
169 ms |
41940 KB |
Output is correct |
23 |
Correct |
249 ms |
58964 KB |
Output is correct |
24 |
Correct |
232 ms |
60252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
104 ms |
39896 KB |
Output is correct |
2 |
Correct |
122 ms |
45640 KB |
Output is correct |
3 |
Correct |
44 ms |
28392 KB |
Output is correct |
4 |
Correct |
44 ms |
28500 KB |
Output is correct |
5 |
Correct |
554 ms |
77512 KB |
Output is correct |
6 |
Correct |
377 ms |
77144 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
295 ms |
52820 KB |
Output is correct |
9 |
Correct |
317 ms |
61440 KB |
Output is correct |
10 |
Correct |
105 ms |
39808 KB |
Output is correct |
11 |
Correct |
121 ms |
45620 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 |
436 KB |
Output is correct |
16 |
Correct |
52 ms |
28600 KB |
Output is correct |
17 |
Correct |
46 ms |
28508 KB |
Output is correct |
18 |
Correct |
157 ms |
41820 KB |
Output is correct |
19 |
Correct |
181 ms |
48060 KB |
Output is correct |
20 |
Correct |
136 ms |
39396 KB |
Output is correct |
21 |
Correct |
162 ms |
43604 KB |
Output is correct |
22 |
Correct |
138 ms |
39168 KB |
Output is correct |
23 |
Correct |
150 ms |
43540 KB |
Output is correct |
24 |
Correct |
46 ms |
28504 KB |
Output is correct |
25 |
Correct |
45 ms |
28404 KB |
Output is correct |
26 |
Correct |
77 ms |
31508 KB |
Output is correct |
27 |
Correct |
73 ms |
32596 KB |
Output is correct |
28 |
Correct |
111 ms |
40604 KB |
Output is correct |
29 |
Correct |
110 ms |
39944 KB |
Output is correct |
30 |
Correct |
109 ms |
40368 KB |
Output is correct |
31 |
Correct |
108 ms |
40532 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 |
352 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 |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
344 KB |
Output is correct |
41 |
Correct |
3 ms |
860 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
2 ms |
604 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
604 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
3 ms |
604 KB |
Output is correct |
48 |
Correct |
74 ms |
6576 KB |
Output is correct |
49 |
Correct |
63 ms |
8284 KB |
Output is correct |
50 |
Correct |
49 ms |
7768 KB |
Output is correct |
51 |
Correct |
43 ms |
7764 KB |
Output is correct |
52 |
Correct |
38 ms |
7772 KB |
Output is correct |
53 |
Correct |
99 ms |
15080 KB |
Output is correct |
54 |
Correct |
14 ms |
1880 KB |
Output is correct |
55 |
Correct |
55 ms |
4988 KB |
Output is correct |
56 |
Correct |
2 ms |
604 KB |
Output is correct |
57 |
Correct |
12 ms |
1856 KB |
Output is correct |
58 |
Correct |
5 ms |
1372 KB |
Output is correct |
59 |
Correct |
352 ms |
34064 KB |
Output is correct |
60 |
Correct |
517 ms |
46668 KB |
Output is correct |
61 |
Correct |
608 ms |
48468 KB |
Output is correct |
62 |
Correct |
616 ms |
48720 KB |
Output is correct |
63 |
Correct |
407 ms |
47440 KB |
Output is correct |
64 |
Correct |
611 ms |
48788 KB |
Output is correct |
65 |
Correct |
632 ms |
48724 KB |
Output is correct |
66 |
Correct |
188 ms |
19968 KB |
Output is correct |
67 |
Correct |
659 ms |
52048 KB |
Output is correct |
68 |
Correct |
139 ms |
40300 KB |
Output is correct |
69 |
Correct |
90 ms |
28500 KB |
Output is correct |
70 |
Correct |
189 ms |
56404 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 |
348 KB |
Output is correct |
77 |
Correct |
56 ms |
28496 KB |
Output is correct |
78 |
Correct |
47 ms |
28504 KB |
Output is correct |
79 |
Correct |
47 ms |
28508 KB |
Output is correct |
80 |
Correct |
44 ms |
28508 KB |
Output is correct |
81 |
Correct |
169 ms |
41940 KB |
Output is correct |
82 |
Correct |
249 ms |
58964 KB |
Output is correct |
83 |
Correct |
232 ms |
60252 KB |
Output is correct |
84 |
Correct |
732 ms |
73384 KB |
Output is correct |
85 |
Correct |
759 ms |
75204 KB |
Output is correct |
86 |
Correct |
481 ms |
72000 KB |
Output is correct |
87 |
Correct |
492 ms |
74692 KB |
Output is correct |
88 |
Correct |
0 ms |
348 KB |
Output is correct |
89 |
Correct |
464 ms |
74836 KB |
Output is correct |
90 |
Correct |
554 ms |
74324 KB |
Output is correct |
91 |
Correct |
393 ms |
73044 KB |
Output is correct |