This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "fish.h"
#include<bits/stdc++.h>
using namespace std;
#define in insert
#define all(x) x.begin(),x.end()
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
// #define int long long
typedef long long ll;
typedef vector<int> vi;
typedef set<int> si;
typedef multiset<int> msi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
template<typename T>
void mxx(T &a, T b) {
if(a < b) a = b;
}
long long max_weights(int n, int m, vector<int> X, vector<int> Y, vector<int> W) {
vector<vector<pair<int, long long>>> pos(n);
for(int i = 0; i < m; i++) {
pos[X[i]].eb(Y[i], W[i]);
}
vector<vector<pair<long long, long long>>> dp(n);
for(int i = 0; i < n; i++) {
sort(all(pos[i]));
dp[i].resize(pos[i].size(), make_pair(0, 0));
}
long long ans = 0;
for(int i = 1; i < n; i++) {
int sz = (int)pos[i].size(), l = 0;
long long mx = ans;
for(int j = 0; j < sz; j++) {
while(l < pos[i - 1].size() && pos[i - 1][l].ff < pos[i][j].ff) {
mx = max(mx, dp[i - 1][l].ff) + pos[i - 1][l].ss;
l++;
}
dp[i][j].ff = mx;
}
if(i - 2 >= 0) {
long long sum = 0;
for(int j = (int)pos[i - 2].size() - 1; j >= 0; j--) {
sum += pos[i - 2][j].ss;
mxx(ans, dp[i - 2][j].ff + sum);
}
}
mx = ans;
int r = (int)pos[i - 1].size() - 1;
for(int j = sz - 1; j >= 0; j--) {
while(r >= 0 && pos[i - 1][r].ff > pos[i][j].ff) {
mxx(mx, max(dp[i - 1][r].ff, dp[i - 1][r].ss));
r--;
}
if(j + 1 < sz) dp[i][j].ss = dp[i][j + 1].ss + pos[i][j].ss;
mxx(dp[i][j].ss, mx + pos[i][j].ss);
}
for(int j = 0; j < (int)pos[i - 1].size(); j++) {
mxx(ans, dp[i - 1][j].ss);
mxx(ans, dp[i - 1][j].ff);
}
}
long long sum = 0;
for(int j = (int)pos[n - 2].size() - 1; j >= 0; j--) {
sum += pos[n - 2][j].ss;
mxx(ans, dp[n - 2][j].ff + sum);
}
for(int j = 0; j < pos.back().size(); j++) {
mxx(ans, dp.back()[j].ff);
mxx(ans, dp.back()[j].ss);
}
return ans;
}
Compilation message (stderr)
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:41:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | while(l < pos[i - 1].size() && pos[i - 1][l].ff < pos[i][j].ff) {
| ~~^~~~~~~~~~~~~~~~~~~
fish.cpp:74:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for(int j = 0; j < pos.back().size(); j++) {
| ~~^~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |