#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
#define F first
#define S second
#define all(x) (x).begin(),(x).end()
const int N = 3e5+5;
const int MOD = 1e9+7;
const ll INF = 1e18+5;
#ifdef dremix
#define p(x) cerr<<#x<<" = "<<x<<endl;
#define pv(x) cerr<<#x<<" = {";for(auto v : x)cerr<<v<<", ";cerr<<"}"<<endl;
#else
#define p(x) {}
#define pv(x) {}
#endif
long long max_weights(int n, int m, vector<int> x, vector<int> y, vector<int> w) {
/// pier optimal options are below each fish in each column or full column
/// dp[i][j][k], I am at column i with the following status:
/// at column (i-1) the pier was built below fish j and above that the fish caught were k
int i,j,k;
vector<vector<pi> > col(n,vector<pi>());
//p(1)
for(i=0;i<m;i++){
col[x[i]].push_back({y[i],w[i]});
}
for(i=0;i<n;i++){
sort(all(col[i]));
}
vector<vector<ll> > best(n,vector<ll>()),eaten(n,vector<ll>());
vector<ll> ans(n,-INF);
//p(2)
for(i=0;i<n;i++){
//p(i)
int sz = col[i].size();
best[i].assign(sz+1,-INF);
eaten[i].assign(sz+1,-INF);
}
/// starting at i = 0 (base case)
int sz = col[0].size();
//p(3)
ans[0] = 0;
for(j=0;j<=sz;j++){
best[0][j] = 0;
eaten[0][j] = 0;
}
//p(4)
p("go")
for(i=1;i<n;i++){
/// at column i with sz fish
int sz = col[i].size();
int sz1 = col[i-1].size();
ll tot = 0;
int p1 = 0;
ans[i] = max(ans[i],ans[i-1]);
for(j=0;j<sz;j++){
p(j)
int h = col[i][j].F-1;
/// build pier up to h
/// 2 cases:
/// (i-1) catches my fish
int idx = j;
ll sum = 0;
best[i][j] = max(best[i][j],ans[i-1]);
eaten[i][j] = max(eaten[i][j],ans[i-1]);
for(k=0;k<sz1;k++){
while(idx < sz && col[i][idx].F <= col[i-1][k].F-1){
sum += col[i][idx].S;
idx++;
}
ll temp = best[i-1][k] + sum;
//p(temp)
best[i][j] = max(best[i][j],temp);
eaten[i][idx] = max(eaten[i][idx],temp);
ans[i] = max(ans[i],temp);
}
/// case of full col
while(idx < sz){
sum += col[i][idx].S;
idx++;
}
ll temp = best[i-1][k] + sum;
best[i][j] = max(best[i][j],temp);
eaten[i][idx] = max(eaten[i][idx],temp);
ans[i] = max(ans[i],temp);
/// I catch fish from (i-1)
while(p1 < sz1 && col[i-1][p1].F <= h){
tot += col[i-1][p1].S;
p1++;
}
// I can get up to fish (p1-1)
ll temp2 = tot;
//p(j)
for(k=0;k<p1;k++){
/// I will get fish from {k,p1-1} (temp)
/// i need best dp[i-1][o][oo] with o+oo == k
//p(k)
//p(temp2)
ll temp = eaten[i-1][k] + temp2;
//p(temp)
best[i][j] = max(best[i][j],temp);
eaten[i][j] = max(eaten[i][j],temp);
ans[i] = max(ans[i],temp);
temp2 -= col[i-1][k].S;
}
}
/// case of full col
/// build pier up to h
best[i][j] = max(best[i][j],ans[i-1]);
eaten[i][j] = max(eaten[i][j],ans[i-1]);
/// 2 cases
/// I catch fish from (i-1)
while(p1 < sz1){
tot += col[i-1][p1].S;
p1++;
}
// I can get up to fish (p1-1)
ll temp2 = tot;
//p(temp2)
//p(p1)
for(k=0;k<p1;k++){
/// I will get fish from {k,p1-1} (temp)
/// i need best dp[i-1][o][oo] with o+oo == k
ll temp = eaten[i-1][k] + temp2;
//p(temp)
best[i][j] = max(best[i][j],temp);
eaten[i][j] = max(eaten[i][j],temp);
ans[i] = max(ans[i],temp);
temp2 -= col[i-1][k].S;
}
pv(best[i])
pv(eaten[i])
}
// from last col
pv(ans)
return ans[n-1];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
18076 KB |
Output is correct |
2 |
Correct |
44 ms |
18996 KB |
Output is correct |
3 |
Correct |
13 ms |
14292 KB |
Output is correct |
4 |
Correct |
13 ms |
14324 KB |
Output is correct |
5 |
Execution timed out |
1069 ms |
32376 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1069 ms |
23360 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
14292 KB |
Output is correct |
2 |
Correct |
13 ms |
14292 KB |
Output is correct |
3 |
Correct |
29 ms |
15828 KB |
Output is correct |
4 |
Correct |
33 ms |
16960 KB |
Output is correct |
5 |
Correct |
47 ms |
21452 KB |
Output is correct |
6 |
Correct |
53 ms |
20952 KB |
Output is correct |
7 |
Correct |
48 ms |
21324 KB |
Output is correct |
8 |
Correct |
48 ms |
21416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
300 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
436 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
304 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
300 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
436 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
304 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
300 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
56 ms |
3348 KB |
Output is correct |
18 |
Correct |
64 ms |
3572 KB |
Output is correct |
19 |
Correct |
38 ms |
3532 KB |
Output is correct |
20 |
Correct |
39 ms |
3540 KB |
Output is correct |
21 |
Correct |
38 ms |
3504 KB |
Output is correct |
22 |
Correct |
128 ms |
6712 KB |
Output is correct |
23 |
Correct |
5 ms |
852 KB |
Output is correct |
24 |
Correct |
22 ms |
2300 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
4 ms |
900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
300 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
436 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
304 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
300 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
56 ms |
3348 KB |
Output is correct |
18 |
Correct |
64 ms |
3572 KB |
Output is correct |
19 |
Correct |
38 ms |
3532 KB |
Output is correct |
20 |
Correct |
39 ms |
3540 KB |
Output is correct |
21 |
Correct |
38 ms |
3504 KB |
Output is correct |
22 |
Correct |
128 ms |
6712 KB |
Output is correct |
23 |
Correct |
5 ms |
852 KB |
Output is correct |
24 |
Correct |
22 ms |
2300 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
4 ms |
900 KB |
Output is correct |
27 |
Correct |
2 ms |
880 KB |
Output is correct |
28 |
Correct |
572 ms |
15724 KB |
Output is correct |
29 |
Execution timed out |
1082 ms |
21588 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
14292 KB |
Output is correct |
2 |
Correct |
13 ms |
14292 KB |
Output is correct |
3 |
Correct |
29 ms |
15828 KB |
Output is correct |
4 |
Correct |
33 ms |
16960 KB |
Output is correct |
5 |
Correct |
47 ms |
21452 KB |
Output is correct |
6 |
Correct |
53 ms |
20952 KB |
Output is correct |
7 |
Correct |
48 ms |
21324 KB |
Output is correct |
8 |
Correct |
48 ms |
21416 KB |
Output is correct |
9 |
Correct |
45 ms |
19788 KB |
Output is correct |
10 |
Correct |
37 ms |
11152 KB |
Output is correct |
11 |
Correct |
80 ms |
22116 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
12 ms |
14336 KB |
Output is correct |
19 |
Correct |
13 ms |
14372 KB |
Output is correct |
20 |
Correct |
13 ms |
14300 KB |
Output is correct |
21 |
Correct |
13 ms |
14292 KB |
Output is correct |
22 |
Correct |
49 ms |
18760 KB |
Output is correct |
23 |
Correct |
78 ms |
22152 KB |
Output is correct |
24 |
Correct |
78 ms |
22104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
18076 KB |
Output is correct |
2 |
Correct |
44 ms |
18996 KB |
Output is correct |
3 |
Correct |
13 ms |
14292 KB |
Output is correct |
4 |
Correct |
13 ms |
14324 KB |
Output is correct |
5 |
Execution timed out |
1069 ms |
32376 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |