#include using namespace std; struct C { int b,c; long long v; }; int n,m,k,s,t; vector css[200010]; bool byb(C const&c1,C const&c2){ return c1.b>n>>m>>k>>s>>t; for(int i=0;i>a>>c.b>>c.c; css[a].push_back(c); } { C c; c.c=s; c.v=0; css[0].push_back(c); } { C c; c.b=t; css[n].push_back(c); } for(int i=0;ic0.v+(c1.c-c0.c)){ c1.v=c0.v+(c1.c-c0.c); } } for(int j=cc-2;j>=0;--j){ C&c1=css[i][j]; C&c0=css[i][j+1]; if(c1.v>c0.v+(c0.c-c1.c)){ c1.v=c0.v+(c0.c-c1.c); } } sort(css[i+1].begin(),css[i+1].end(),byb); int l=0; for(auto& d:css[i+1]){ while(l