This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//https://www.instagram.com/_modwwe/
#pragma GCC optimize("Ofast,unroll-loops")
#include<bits/stdc++.h>
//#define int long long
#define ll long long
#define down cout<<'\n';
#define debug cout<<" cucuucucuuu",down
#define NHP     ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
#define modwwe  int t;cin>>t; while(t--)
#define bit(i,j) (i>>j&1)
#define sobit(a) __builtin_popcountll(a)
#define task "test"
#define fin(x) freopen(x".inp","r",stdin)
#define fou(x) freopen(x".ans","w",stdout)
#define pb push_back
#define checktime   cerr << (double)clock() / CLOCKS_PER_SEC * 1000  << " ms";
using namespace std;
void phongbeo();
const ll inf=1e18;
const int mod2=1e9+7;
const int  mod1=998244353;
struct icd
{
    long double a;
    int b;
};
struct ib
{
    ll a;
    ll b;
};
struct ic
{
    int a,b,c;
};
struct id
{
    int a,b,c,d;
};
struct ie
{
    int a,b,c,d,e;
};
ll n,m,s1,s2,s4,s3,sf,k,s5,s6,mx,s7,s8,s9,mx2,res,dem2=0,dem=0,s33,dem3,l,r,mid;
int  i,s10,s12;
int kk;
int el=29;
/*
main()
{
#ifndef ONLINE_JUDGE
  fin(task),fou(task);
#endif
   //  NHP
    /// cin>>s1;
    //  modwwe
    phongbeo();
}*/
vector<ib> v[100001];
bool cmp(ib a,ib b)
{
    return a.a<b.a;
}
ll max_weights(int n, int m, vector<int> X, vector<int> Y, vector<int> W)
{
    for(int i=0; i<m; i++)
        v[X[i]+1].pb({Y[i]+1,W[i]});
vector<ll> dp[n+1][2];
    for(int i=1; i<=n; i++)
    {
        v[i].pb({n+1,0});
        sort(v[i].begin(),v[i].end(),cmp);
        dp[i][0].resize(v[i].size()+2);
        dp[i][1].resize(v[i].size()+2);
    }
    for(int i=1;i<=1;i++)
        for(int j=v[i].size()-1;j>=0;--j)
         dp[i][0][j]=-inf;
    for(int i=1; i<=n; i++)
    {
        for(int j=v[i].size()-2; j>=0; j--)
           dp[i][0][j]=max(dp[i][0][j+1],dp[i][0][j])+v[i][j].b,s6=max(s6,dp[i][0][j]);
        l=0;
        dem=0;
        s2=0;
        s3=0;
        if(i!=1){
          //      cout<<i,down
            for(auto x:v[i])
            {
                while(x.a-1>=v[i-1][l].a)
                    s3+=v[i-1][l].b,l++;
                dp[i][1][dem]=max({dp[i][1][dem],s4,dp[i-1][1][l]+s3});
                dem++;
            }
           //  cout<<i,down
            }
        dem=0;
        s4=0;
        l=0;
        if(i!=n){
            // cout<<i,down
            for(auto x:v[i])
            {
                while(x.a-1>=v[i+1][l].a)
                    l++;
                    if(l!=0){
                 //cout<<v[i].size()<<" "<<dem<<" "<<max(dp[i][1][dem],dp[i][0][dem])<<" "<<dp[i+1][0][l-1],down
                dp[i+1][0][l-1]=max({dp[i+1][0][l-1],dp[i][1][dem],dp[i][0][dem]});
                }
                s4=max({s4,dp[i][0][dem],dp[i][1][dem]});
                dem++;
            }
            //cout<<i,down
            }
        s5=v[i][0].b;
        s6=max(s6,dp[i][1][0]);
        for(int j=1; j<v[i].size(); j++)
        {s6=max(s6,dp[i][1][j]);
            dp[i][1][j]=max(dp[i][1][j]-s5,dp[i][1][j-1]);
            s5+=v[i][j].b;
        }
    }
     return s6;
}
/*
void phongbeo()
{
     cin>>n>>m;
     vector<int> x(m);
      vector<int> y(m);
       vector<int> w(m);
      for(int i=0;i<m;i++)
 	cin >> x[i] >> y[i] >> w[i];
      cout<<max_weights(n,m,x,y,w);
     }*/
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:107:17: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
  107 |                 while(x.a-1>=v[i+1][l].a)
      |                 ^~~~~
fish.cpp:109:21: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
  109 |                     if(l!=0){
      |                     ^~
fish.cpp:120:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<ib>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  120 |         for(int j=1; j<v[i].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... |