#include "fish.h"
#include <vector>
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll big = 1e18;
long long max_weights(int n, int m, std::vector<int> x, std::vector<int> y,
std::vector<int> w) {
ll sub1 = 1, sub2 = 1, sub3 = 1;
vector<vector<pair<ll,ll>>> a(n+1);
vector<vector<ll>> b(n+1,vector<ll>(1,-1));
for(ll i = 0;i<m;i++){
a[x[i]].push_back({y[i],w[i]});
if(x[i]) b[x[i]-1].push_back(y[i]);
b[x[i]+1].push_back(y[i]);
b[x[i]].push_back(y[i]);
if(x[i]&1) sub1 = 0;
if(x[i]>1) sub2 = 0;
if(y[i]) sub3 = 0;
}
//cerr << sub1 << " " << sub2 << " " << sub3 << "\n";
for(auto &o : a) sort(o.begin(),o.end());
for(auto &o : b) sort(o.begin(),o.end());
if (sub1){
ll ans = 0;
for(ll i : w) ans += i;
return ans;
}
if(sub2){
ll a1 = 0, a2 = 0;
vector<array<ll,3>> o;
for(auto [p,v] : a[0]) a1+=v,o.push_back({p,1,v});
for(auto [p,v] : a[1]) a2+=v,o.push_back({p,0,v});
ll ans = max(a1,a2);
if(n>2){
sort(o.begin(),o.end());
ll dp0 = 0, dp1 = 0;
for(auto &u : o){
if (u[1]){
dp0 += u[2];
dp1 = max(dp1,dp0);
}else{
dp1 += u[2];
}
}
ans = max(ans,dp1);
}
return ans;
}
if (sub3){
ll ans = 0;
for(ll i : w) ans += i;
vector<ll> dp(n+1,big);
dp[0] = 0;
for(ll i = 0;i<n;i++){
ll cur = a[i].size()?a[i][0].second:0;
dp[i+1] = min(dp[i+1],dp[i]+cur);
if(i>=1)dp[i+1] = min(dp[i+1],dp[i-1]+cur);
if(i>=3)dp[i+1] = min(dp[i+1],dp[i-2]+cur);
}
//for(ll i = 0;i<=n;i++) cerr << dp[i] << " \n"[i==n];
ll de = min(dp.back(),dp.end()[-2]);
return ans-de;
}
vector<vector<ll>> dp = b;
for(auto &o : dp)fill(o.begin(),o.end(),0);
vector<vector<ll>> dp0 = dp;
for(auto &o : a){
for(ll i = 1;i<o.size();i++){
o[i].second += o[i-1].second;
}
}
//for(auto &o : b){
// for(ll i = 0;i<o.size();i++) cout << o[i] << " \n"[i+1==o.size()];
//}
vector<vector<ll>> preq(n,vector<ll>(n+5,0));
for(ll i = 0;i<n;i++){
for(auto [p,v] : a[i]){
preq[i][p] = v;
}
for(ll j = 1;j<n+5;j++){
preq[i][j] = max(preq[i][j],preq[i][j-1]);
}
}
auto qry = [&](ll i, ll l, ll r){
if(i<0||i>=n) return 0ll;
//auto &o = a[i];
//auto v1 = lower_bound(o.begin(),o.end(),make_pair(l+1,0ll));
//auto v2 = lower_bound(o.begin(),o.end(),make_pair(r+1,0ll));
//cerr << "qry " << i << " " << l << " " << r << "\n";
ll g1 = l>=0?preq[i][l]:0;
ll g2 = r>=0?preq[i][r]:0;
//cerr << "qry " << i << " " << l << " " << r << " = " << g2-g1 << "\n";
return g2-g1;
};
ll ans = 0;
for(ll i = 1;i<n;i++){
for(ll j = 0;j<b[i].size();j++){
ll y = b[i][j];
for(ll k = 0;k<b[i-1].size();k++){
ll y0 = b[i-1][k];
ll cur = dp0[i-1][k];
if(y0<y) cur += qry(i-1,y0,y);
dp0[i][j] = max(dp0[i][j],cur);
if(y0>y) cur += qry(i,y,y0);
dp[i][j] = max(dp[i][j],cur);
}
if(i>1)for(ll k = 0;k<b[i-2].size();k++){
ll y0 = b[i-2][k];
ll cur = dp[i-2][k];
ll h = max(y,y0);
cur += qry(i-1,-1,h);
dp[i][j] = max(dp[i][j],cur);
dp0[i][j] = max(dp0[i][j],cur);
}
if(i>2)for(ll k = 0;k<b[i-3].size();k++){
ll y0 = b[i-3][k];
ll cur = dp[i-3][k];
cur += qry(i-2,-1,y0);
cur += qry(i-1,-1,y);
dp[i][j] = max(dp[i][j],cur);
dp0[i][j] = max(dp0[i][j],cur);
}
ans = max(ans,dp[i][j]+qry(i+1,-1,y));
ans = max(ans,dp0[i][j]+qry(i+1,-1,y));
//cerr << "dp " << i << " " << j << " = " << dp[i][j] << "\n";
}
}
return ans;
return 0;
}
Compilation message
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:71:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | for(ll i = 1;i<o.size();i++){
| ~^~~~~~~~~
fish.cpp:100:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
100 | for(ll j = 0;j<b[i].size();j++){
| ~^~~~~~~~~~~~
fish.cpp:102:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for(ll k = 0;k<b[i-1].size();k++){
| ~^~~~~~~~~~~~~~
fish.cpp:110:28: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
110 | if(i>1)for(ll k = 0;k<b[i-2].size();k++){
| ~^~~~~~~~~~~~~~
fish.cpp:118:28: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
118 | if(i>2)for(ll k = 0;k<b[i-3].size();k++){
| ~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
13880 KB |
Output is correct |
2 |
Correct |
46 ms |
15164 KB |
Output is correct |
3 |
Correct |
6 ms |
8028 KB |
Output is correct |
4 |
Correct |
6 ms |
8028 KB |
Output is correct |
5 |
Correct |
141 ms |
32992 KB |
Output is correct |
6 |
Correct |
148 ms |
36944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
96 ms |
28440 KB |
Output is correct |
3 |
Correct |
143 ms |
31608 KB |
Output is correct |
4 |
Correct |
36 ms |
13620 KB |
Output is correct |
5 |
Correct |
44 ms |
15164 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
8 ms |
8024 KB |
Output is correct |
11 |
Correct |
7 ms |
8028 KB |
Output is correct |
12 |
Correct |
46 ms |
18344 KB |
Output is correct |
13 |
Correct |
57 ms |
20244 KB |
Output is correct |
14 |
Correct |
43 ms |
17760 KB |
Output is correct |
15 |
Correct |
55 ms |
19372 KB |
Output is correct |
16 |
Correct |
43 ms |
18368 KB |
Output is correct |
17 |
Correct |
52 ms |
18540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
8028 KB |
Output is correct |
2 |
Correct |
7 ms |
9052 KB |
Output is correct |
3 |
Correct |
27 ms |
12880 KB |
Output is correct |
4 |
Correct |
23 ms |
12004 KB |
Output is correct |
5 |
Correct |
43 ms |
17364 KB |
Output is correct |
6 |
Correct |
42 ms |
16976 KB |
Output is correct |
7 |
Correct |
52 ms |
17476 KB |
Output is correct |
8 |
Correct |
58 ms |
17436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
1372 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
1372 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
1372 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
1372 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
2 ms |
1116 KB |
Output is correct |
16 |
Correct |
10 ms |
824 KB |
Output is correct |
17 |
Correct |
746 ms |
7260 KB |
Output is correct |
18 |
Correct |
900 ms |
7612 KB |
Output is correct |
19 |
Correct |
526 ms |
7512 KB |
Output is correct |
20 |
Correct |
525 ms |
7512 KB |
Output is correct |
21 |
Correct |
524 ms |
7248 KB |
Output is correct |
22 |
Execution timed out |
1032 ms |
13392 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
1372 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
1372 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
2 ms |
1116 KB |
Output is correct |
16 |
Correct |
10 ms |
824 KB |
Output is correct |
17 |
Correct |
746 ms |
7260 KB |
Output is correct |
18 |
Correct |
900 ms |
7612 KB |
Output is correct |
19 |
Correct |
526 ms |
7512 KB |
Output is correct |
20 |
Correct |
525 ms |
7512 KB |
Output is correct |
21 |
Correct |
524 ms |
7248 KB |
Output is correct |
22 |
Execution timed out |
1032 ms |
13392 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
8028 KB |
Output is correct |
2 |
Correct |
7 ms |
9052 KB |
Output is correct |
3 |
Correct |
27 ms |
12880 KB |
Output is correct |
4 |
Correct |
23 ms |
12004 KB |
Output is correct |
5 |
Correct |
43 ms |
17364 KB |
Output is correct |
6 |
Correct |
42 ms |
16976 KB |
Output is correct |
7 |
Correct |
52 ms |
17476 KB |
Output is correct |
8 |
Correct |
58 ms |
17436 KB |
Output is correct |
9 |
Runtime error |
910 ms |
2097152 KB |
Execution killed with signal 9 |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
13880 KB |
Output is correct |
2 |
Correct |
46 ms |
15164 KB |
Output is correct |
3 |
Correct |
6 ms |
8028 KB |
Output is correct |
4 |
Correct |
6 ms |
8028 KB |
Output is correct |
5 |
Correct |
141 ms |
32992 KB |
Output is correct |
6 |
Correct |
148 ms |
36944 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
96 ms |
28440 KB |
Output is correct |
9 |
Correct |
143 ms |
31608 KB |
Output is correct |
10 |
Correct |
36 ms |
13620 KB |
Output is correct |
11 |
Correct |
44 ms |
15164 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
8 ms |
8024 KB |
Output is correct |
17 |
Correct |
7 ms |
8028 KB |
Output is correct |
18 |
Correct |
46 ms |
18344 KB |
Output is correct |
19 |
Correct |
57 ms |
20244 KB |
Output is correct |
20 |
Correct |
43 ms |
17760 KB |
Output is correct |
21 |
Correct |
55 ms |
19372 KB |
Output is correct |
22 |
Correct |
43 ms |
18368 KB |
Output is correct |
23 |
Correct |
52 ms |
18540 KB |
Output is correct |
24 |
Correct |
7 ms |
8028 KB |
Output is correct |
25 |
Correct |
7 ms |
9052 KB |
Output is correct |
26 |
Correct |
27 ms |
12880 KB |
Output is correct |
27 |
Correct |
23 ms |
12004 KB |
Output is correct |
28 |
Correct |
43 ms |
17364 KB |
Output is correct |
29 |
Correct |
42 ms |
16976 KB |
Output is correct |
30 |
Correct |
52 ms |
17476 KB |
Output is correct |
31 |
Correct |
58 ms |
17436 KB |
Output is correct |
32 |
Correct |
0 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
3 ms |
1372 KB |
Output is correct |
42 |
Correct |
1 ms |
604 KB |
Output is correct |
43 |
Correct |
2 ms |
1372 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
1116 KB |
Output is correct |
46 |
Correct |
2 ms |
1116 KB |
Output is correct |
47 |
Correct |
10 ms |
824 KB |
Output is correct |
48 |
Correct |
746 ms |
7260 KB |
Output is correct |
49 |
Correct |
900 ms |
7612 KB |
Output is correct |
50 |
Correct |
526 ms |
7512 KB |
Output is correct |
51 |
Correct |
525 ms |
7512 KB |
Output is correct |
52 |
Correct |
524 ms |
7248 KB |
Output is correct |
53 |
Execution timed out |
1032 ms |
13392 KB |
Time limit exceeded |
54 |
Halted |
0 ms |
0 KB |
- |