#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <map>
#include <set>
#include <unordered_set>
#include <cstring>
#include <queue>
#include <array>
using namespace std; using ll=int64_t;
#define rep(i,n) for(ll i=0; i<ll(n); ++i)
#define vi vector<ll>
#define all(x) begin(x), end(x)
#define pi pair<ll, ll>
ll max_weights(ll N, ll M, vi X, vi Y, vi W){
vector<array<ll,3>> fish(M);
vector<vi> prefixWeight(N, vi(N, 0)); // Prefix weight for column.
rep(i,M){
fish[i]={Y[i],X[i],W[i]};
prefixWeight[Y[i]][X[i]] += W[i];
}
sort(all(fish)); // Sort by height
for(ll c=0; c<N; ++c){
for(ll r=1; r<N; ++r){
prefixWeight[r][c] += prefixWeight[r-1][c];
}
}
vi heightBuilt(N, 0); // Height built per column.
// If losing less than winning try adding it?
auto diff = [&](ll new_row, ll col){
// How much new weight it would add to built up to new_row in a column besides col from col.
return prefixWeight[new_row][col]
- prefixWeight[heightBuilt[col]][col];
};
for(auto f : fish){
ll curr_row = f[0], curr_col=f[1];
ll rightLoss=0, leftLoss=0;
if(curr_col < N-1) rightLoss = diff(curr_row, curr_col +1);
if(curr_col > 0) leftLoss = diff(curr_row, curr_col-1);
ll gain = diff(curr_row, curr_col);
// cout << "Weight : " << f[2] << " rightLoss " << rightLoss << " leftLoss " << leftLoss << " gain " << gain << endl;
if(curr_col < N-1 && (rightLoss <= leftLoss || curr_col == 0) && rightLoss < gain){
heightBuilt[curr_col+1] = curr_row;
}
if(curr_col > 0 && leftLoss <= (rightLoss || curr_col == N-1) && leftLoss < gain){
heightBuilt[curr_col-1] = curr_row;
}
}
ll ans=0;
for(auto f : fish){
ll row=f[0], col=f[1], weight=f[2];
if(heightBuilt[col] >= row) continue; // Inside of bridge.
bool ok=false;
if(col < N-1) if(heightBuilt[col+1] >= row) ok=true;
if(col > 0) if(heightBuilt[col-1] >= row) ok=true;
if(ok) ans+=weight;
}
for(auto h : heightBuilt){
cout << h << " ";
}
cout << endl;
return ans;
}
/*
int main(){
ll n, m; cin>>n>>m;
vi x(n), y(n), w(n);
rep(i,m){
cin>>x[i]>>y[i]>>w[i];
}
cout << max_weights(n,m,x,y,w) << endl;
return 0;
}
*/
Compilation message
/usr/bin/ld: /tmp/ccDyXcgu.o: in function `main':
grader.cpp:(.text.startup+0x25e): undefined reference to `max_weights(int, int, std::vector<int, std::allocator<int> >, std::vector<int, std::allocator<int> >, std::vector<int, std::allocator<int> >)'
collect2: error: ld returned 1 exit status