Language: C++

#include<queue>
#include<cstdio>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

typedef long long ll;

const int CAPA_INF=1<<29;
const int COST_INF=1<<29;

template<class T,class U>
struct Edge{
	int v,rev;
	T capa;
	U cost;
	T flow;
};

template<class T,class U>
struct AdjList:public vector< vector< Edge<T,U> > >{
	AdjList(int n):vector< vector< Edge<T,U> > >(n){}
};

template<class T,class U>
void add_edge(AdjList<T,U> &adj,int u,int v,T capa,U cost){
	adj[u].push_back((Edge<T,U>){v,adj[v].size()  ,capa, cost});
	adj[v].push_back((Edge<T,U>){u,adj[u].size()-1,   0,-cost});
}

template<class T,class U>
pair<T,U> augment(AdjList<T,U> &adj,int s,int t,vector<U> &pot){
	int n=adj.size();
	vector<int> pre(n);
	vector<U> d(n,COST_INF); d[s]=0;

	// Dijkstra
	bool ok=false;
	priority_queue< pair<U,int> > pq; pq.push(make_pair(0,s));
	while(!pq.empty()){
		int u=pq.top().second;
		U cost=-pq.top().first; pq.pop();

		if(cost<d[u]) continue;
		if(u==t) ok=true;

		rep(i,adj[u].size()){
			Edge<T,U> &e=adj[u][i];
			U cost2=cost+e.cost+pot[u]-pot[e.v];
			if(e.capa-e.flow>0 && cost2<d[e.v]){
				d[e.v]=cost2;
				pre[e.v]=e.rev;
				pq.push(make_pair(-cost2,e.v));
			}
		}
	}

	if(!ok) return make_pair(0,0);

	T water=CAPA_INF;
	for(int v=t;v!=s;){
		Edge<T,U> &e1=adj[v][pre[v]];
		Edge<T,U> &e2=adj[e1.v][e1.rev];
		water=min(water,e2.capa-e2.flow);
		v=e1.v;
	}

	U cost=0;
	for(int v=t;v!=s;){
		Edge<T,U> &e1=adj[v][pre[v]];
		Edge<T,U> &e2=adj[e1.v][e1.rev];
		e1.flow-=water;
		e2.flow+=water;
		cost+=water*e2.cost;
		v=e1.v;
	}

	rep(u,n) pot[u]+=d[u];

	return make_pair(water,cost);
}

template<class T,class U>
pair<T,U> primal_dual(AdjList<T,U> &adj,int s,int t){
	T ans1=0;
	U ans2=0;
	vector<U> pot(adj.size());
	while(1){
		pair<T,U> tmp=augment(adj,s,t,pot);
		if(tmp.first==0) break;
		ans1+=tmp.first;
		ans2+=tmp.second;
	}
	return make_pair(ans1,ans2);
}

int main(){
	int n,v,xl,xr; scanf("%d%d%d%d",&n,&v,&xl,&xr);
	int x[3000],t[3000],p[3000];
	rep(i,n) scanf("%d%d%d",x+i,t+i,p+i);

	AdjList<int,int> adj(2*n+5);

	ll ans=0;

	int src=2*n,snk=2*n+1;
	int src2=src+2,snk2=snk+2;
	add_edge(adj,src2,src,2,0);
	add_edge(adj,snk,snk2,2,0);
	rep(i,n){
		add_edge(adj,src  ,2*i  ,1,0);
		add_edge(adj,2*i+1,snk  ,1,0);

		// 2*i ©ç 2*i+1 ÖeÊ 1, RXg -p[i] ÌÓðÍé (Óðµ¤½ßÉOtðÏ`, a{ p.205)
		add_edge(adj,src2 ,2*i+1,1,0);
		add_edge(adj,2*i  ,snk2 ,1,0);
		add_edge(adj,2*i+1,2*i  ,1,p[i]);

		// à®ç i ð@¢Ä©ç¯¶èÅà®ç j ð@¯éÈçÓðÍé
		rep(j,n) if(i!=j && abs(x[i]-x[j])<=v*(t[j]-t[i])) {
			add_edge(adj,2*i+1,2*j,1,0);
		}

		ans+=p[i];
	}
	// ¬Êð 2+n É§À·é½ßÌÓ
	int snk3=snk+3;
	add_edge(adj,snk2,snk3,2+n,0);

	ans-=primal_dual(adj,src2,snk3).second;

	printf("%d\n",ans);

	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:121:37: error: 'abs' was not declared in this scope; did you mean 'ans'?
  121 |                 rep(j,n) if(i!=j && abs(x[i]-x[j])<=v*(t[j]-t[i])) {
      |                                     ^~~
      |                                     ans
a.cc: In instantiation of 'void add_edge(AdjList<T, U>&, int, int, T, U) [with T = int; U = int]':
a.cc:109:10:   required from here
  109 |         add_edge(adj,src2,src,2,0);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~
a.cc:28:51: warning: narrowing conversion of '(&(& adj)->AdjList<int, int>::std::vector<std::vector<Edge<int, int>, std::allocator<Edge<int, int> > >, std::allocator<std::vector<Edge<int, int>, std::allocator<Edge<int, int> > > > >.std::vector<std::vector<Edge<int, int>, std::allocator<Edge<int, int> > >, std::allocator<std::vector<Edge<int, int>, std::allocator<Edge<int, int> > > > >::operator[](((std::vector<std::vector<Edge<int, int>, std::allocator<Edge<int, int> > >, std::allocator<std::vector<Edge<int, int>, std::allocator<Edge<int, int> > > > >::size_type)v)))->std::vector<Edge<int, int>, std::allocator<Edge<int, int> > >::size()' from 'std::vector<Edge<int, int>, std::allocator<Edge<int, int> > >::size_type' {aka 'long unsigned int'} to 'int' [-Wnarrowing]
   28 |         adj[u].push_back((Edge<T,U>){v,adj[v].size()  ,capa, cost});
      |                                        ~~~~~~~~~~~^~
a.cc:29:53: warning: narrowing conversion of '((&(& adj)->AdjList<int, int>::std::vector<std::vector<Edge<int, int>, std::allocator<Edge<int, int> > >, std::allocator<std::vector<Edge<int, int>, std::allocator<Edge<int, int> > > > >.std::vector<std::vector<Edge<int, int>, std::allocator<Edge<int, int> > >, std::allocator<std::vector<Edge<int, int>, std::allocator<Edge<int, int> > > > >::operator[](((std::vector<std::vector<Edge<int, int>, std::allocator<Edge<int, int> > >, std::allocator<std::vector<Edge<int, int>, std::allocator<Edge<int, int> > > > >::size_type)u)))->std::vector<Edge<int, int>, std::allocator<Edge<int, int> > >::size() - 1)' from 'std::vector<Edge<int, int>, std::allocator<Edge<int, int> > >::size_type' {aka 'long unsigned int'} to 'int' [-Wnarrowing]
   29 |         adj[v].push_back((Edge<T,U>){u,adj[u].size()-1,   0,-cost});
      |                                        ~~~~~~~~~~~~~^~