Problem:
Problem C: Warping Girl
Problem
会津魔法学校は, 魔法を使える者が集まる学校である. その学校の生徒の一人であるハルカは, 魔法陣の上でワープの魔法を使う事ができる.
彼女の家から学校には, 長さ
L
の一直線状の道がある. また, この道には所々魔法陣が描かれている.
彼女は毎日この道を使って学校に通っているため, できるだけ短い時間で学校に辿り着きたいと思っている.
そこでプログラマーであるあなたは, 彼女に学校まで行くときにかかる最小の時間を教えてあげることにした.
ハルカは,
1
分で距離
1
だけ歩いて先に進むことができる ( ＊戻ることはできない)．また, ワープの魔法陣が書いてある位置
P
i
で魔法を唱えることで,
P
i
からちょうど
D
i
だけ進んだ場所に
T
i
分で移動することができる.  移動先に魔法陣がある場合も, 連続してワープ魔法を使用することができる.
彼女の家の位置は
0
、学校の位置は
L
である.
Input
L
n
P
1
D
1
T
1
P
2
D
2
T
2
.
.
P
n
D
n
T
n
１行目に道の長さ
L
, 魔法陣の数
n
が与えられる.
次に
n
個の魔法陣の状態が与えられる.
P
i
は
i
番目の魔法陣がある位置,
D
i
は
i
番目の魔法陣からワープする距離,
T
i
は
i
番目の魔法陣を使ってワープしたときにかかる時間を表す.
Constraints
入力は以下の条件を満たす.
与えられる数は全て整数である
1 ≤
L
≤
10
9
0 ≤
n
≤
min(10
3
, L)
(
min
は
A, B
の小さい方を表す)
0 ≤
P
i
≤
L - 1
0 ≤
D
i
≤
L - P
i
0 ≤
T
i
≤
10
3
P
i
≠
P
j
Output
学校に辿り着くための最小の時間を一行に出力せよ.
Sample Input 1
10 3
2 2 1
4 2 1
8 1 1
Sample Output 1
8
Sample Input 2
10 2
2 3 1
3 5 1
Sample Output 2
6
Sample Input 3
1 0
Sample Output 3
1

Submitted solutions:

code_snippet: #include<bits/stdc++.h>

using Int = long long;
using namespace std;

constexpr Int inf = 1e10;

int main(){
    Int L,N;
    cin >> L >> N;
    vector<tuple<Int,Int,Int>>edge;
    for(int i = 0; i < N; ++i){
        Int p,d,t;
        cin >> p >> d >> t;
        edge.emplace_back(p,d,t);
    }
    sort(edge.begin(),edge.end());
    map<Int,Int>dp; dp[0] = 0;
    if(edge.size() == 0){
        return cout << L << endl,0;
    }
    auto [fp,fd,ft] = edge.front();
    
    dp[fp] = fp;
    dp[fp + fd] = min(fp + fd, dp[fp] + ft);
    
    for(int i = 1; i < edge.size(); ++i){
        auto[p,d,t] = edge[i];
        for(int j = 0; j < i; ++j){
            auto[jp,jd,jt] = edge[j];
            Int cost = dp[jp],diff = p - jp;
            if(dp.find(p) == dp.end()){
                dp[p] = min(p,cost + diff);
            } else {
                dp[p] = min({dp[p],p,cost + diff});
            }
            if(jp + jd > p) continue;
            // guarantee that has visited jp + jd
            cost = dp[jp + jd], diff = p - jp - jd;
            dp[p] = min(dp[p],cost + diff);
        }
        dp[p] = min(dp[p],p);
        if(dp.find(p + d) == dp.end()){
            dp[p + d] = min(p + d, dp[p] + t);
        } else {
            dp[p + d] = min({dp[p + d],p + d, dp[p] + t});
        }
    }
    Int ans = L;
    for(auto itr : dp){
        ans = min(ans, itr.second +L - itr.first);
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 70
memory_kb: 3632
final_rank: 2

code_snippet: #include <iostream>
#include <cstdio>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <utility>
#include <numeric>
#include <algorithm>
#include <functional>
#include <cctype>
#include <complex>
#include <string>
#include <sstream>
#include <cassert>
using namespace std;

//common
typedef int  i32;
typedef long long i64,ll;
#define BR "\n"

#define ALL(c) (c).begin(),(c).end()
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define EACH(it,o) for(auto it = (o).begin(); it != (o).end(); ++it)
#define IN(l,v,r) ((l)<=(v) && (v)<(r))

typedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii;
typedef long long ll; typedef vector<long long> vl; typedef pair<long long,long long> pll; typedef vector<pair<long long,long long> > vpll;
typedef vector<string> vs; typedef long double ld;

//config
#define MODE_DEBUG
//#define INF 1<<30
//#define EPS 1e-8
//const ll MOD =100000007;

//debug
#ifdef NDEBUG
#define DUMP(x)
#define DUMPLN(x)
#define DEBUG(x)
#define DEBUGLN(x)
#define LINE()
#define LINELN()
#define CHECK(exp,act)
#define STOP(e)
#else
#define DUMP(x)  cerr << #x << " = " << (x)
#define DUMPLN(x)  DUMP(x) <<endl
#define DEBUG(x) DUMP(x) << LINE() << " " << __FILE__
#define DEBUGLN(x) DEBUG(x)<<endl
#define LINE()    cerr<< " (L" << __LINE__ << ")"
#define LINELN()    LINE()<<endl
#define CHECK(exp,act)  if(exp!=act){DUMPLN(exp);DEBUGLN(act);}
#define STOP(e)  CHECK(e,true);if(!(e)) exit(1);
#endif

template<class T> inline string toString(const vector<T>& x) {
	stringstream ss;
	REP(i,x.size()){
		if(i!=0)ss<<" ";
		ss<< x[i];
	}
	return ss.str();
}
template<class T> inline string toString(const vector<vector<T> >& map) {
	stringstream ss;
	REP(i,map.size()){
		if(i!=0)ss<<BR;
		ss<< toString(map[i]);
	}
	return ss.str();
}
template<class K,class V>  string toString(map<K,V>& x) {
	string res;stringstream ss;
	for(auto& p:x)ss<< p.first<<":" << p.second<<" ";
	return ss.str();
}

template<typename T,typename V> inline T mod(T v,V MOD){
	return (v%MOD+MOD)%MOD;
}

#define nextInt(n) scanf("%d",&n)
#define nextLong(n) scanf("%lld",&n)
#define nextDouble(n) scanf("%lf",&n)

namespace ShortestPath{

    typedef ll Cost;
    const Cost CINF=1LL<<58;
    typedef  vector<vector<Cost> > Mat;
    struct Edge{
        int from,to;Cost cost;
        Edge(int from,int to,Cost cost)
            : from(from),to(to),cost(cost) {};
    };
    ostream& operator <<(ostream& os,const Edge& e){
            os<<"("<<e.from<<"->"<<e.to<<")";
            return os;
    }
    typedef vector<vector<Edge> > Graph;

    struct Task{
        int prev,pos,cost;
        Task(int prev,int pos,int cost)
            :prev(prev),pos(pos),cost(cost){};
        bool operator>(const Task& r) const{
            return cost>r.cost;
        }
    };

    vector<Cost> dijkstra(const Graph& g,const int s,vector<int>& prev){
        const int V=g.size();
        vector<Cost> d(V,CINF);d[s]=0;
        fill(ALL(prev), -2);
        priority_queue<Task,vector<Task>,greater<Task> > que;
        que.push(Task(-1,s,0));
        while(!que.empty()){
            Task task=que.top();que.pop();
            if(d[task.pos]<task.cost)continue;
            EACH(e,g[task.pos]){
                if(d[e->to]>d[e->from]+e->cost){
                    d[e->to]=d[e->from]+e->cost;
                    que.push(Task(e->from,e->to,d[e->to]));
                }
            }   
        }
        return d;
    }
    vector<Cost> dijkstra(const Graph& g,const int s){
        vector<int> prev(g.size());return dijkstra(g,s,prev);
    }

}
using namespace ShortestPath;



ll INF=1LL<<58;
class Main{
public:	
	void run(){
		int L,n;cin >> L >>n;
		map<int,int> xs;int ind=0;
		xs[0]=ind++;xs[L]=ind++;
		int i=0;
		vector<pair<int,pair<int,int> > > dts(n);
		REP(i,n){
			int p,d,t;cin >> p >> d >> t;
			if(xs.count(p)==0)xs[p]=ind++;
			if(xs.count(p+d)==0)xs[p+d]=ind++;
			dts[i]=make_pair(p,make_pair(p+d,t));
		}
		Graph g(xs.size());
		EACH(it,xs)EACH(it2,xs)if(it2->first>it->first){
			g[it->second].push_back(Edge(it->second,it2->second,(ll)it2->first-it->first));
		}
		REP(i,n){
			g[xs[dts[i].first]].push_back(Edge(xs[dts[i].first],xs[dts[i].second.first],dts[i].second.second));
		}

		cout << dijkstra(g,0)[1] <<endl;
	}
};
 int main(){
 	cout <<fixed<<setprecision(15);
	ios::sync_with_stdio(false);
 	Main().run();
 	return 0;
 }
accuracy: 1
time_ms: 170
memory_kb: 49120
final_rank: 5

code_snippet: #include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
typedef pair<int,int> P;
typedef pair<int,P> PP;
typedef long long ll;
#define F first
#define S second
PP K[1111];
int L;
int n;
ll dp[1111];
int next(int l){
  int st=0,ed=n;
  int ret = -1;
  if( l > L ) return -1;
  while( st<=ed ){
    int h = (st+ed)/2;
    if( K[h].F > l ){
      ed=h-1;
      ret = h;
    }
    else if(K[h].F < l )
      st = h+1;
    else
      return h;
  }
  if( ret == -1 ){
    return n;
  }
  return ret;
}
int solve(int id){
  if( dp[id] != -1 ) return dp[id];
  if( id == n ) return 0;
  int nel = K[id].F+K[id].S.F;
  int nex = next(nel);
  ll res;
  if( nex == -1 ){
    res = solve(id+1)+(K[id+1].F - K[id].F);
  } else {
    res = min( solve(id+1)+(K[id+1].F - K[id].F),
	       solve(nex)+(K[nex].F - nel) +  K[id].S.S);
  }
  // printf("%d - %d = %d\n",K[nex].F,nel,K[nex].F-nel);
  // printf("%d %d  %d\n",id,K[id].F,res);
  return dp[id] = res;
}

int main(){
  cin >> L >> n;
  memset(dp,-1,sizeof(dp));
  for(int i=0;i<n;i++){
    int p,d,t;
    cin >> K[i].F >> K[i].S.F >> K[i].S.S;
  }
  sort(K,K+n);
  K[n].F = L;
  cout << solve(0)+K[0].F << endl;

}
accuracy: 0.3409090909090909
time_ms: 10
memory_kb: 9304
final_rank: 8

code_snippet: #include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>
#include <tuple>
using namespace std;
typedef long long int ll;


// [pos][from] -> cost
unordered_map<int, vector<tuple<int, int>>> field;

unordered_map<int, int> memo;
int solve(int p) {
	if (p == 0) return 0;
	auto it = memo.find(p);
	if (it != memo.end()) {
		return it->second;
	}
	int res = p;
	for (auto nx : field[p]) {
		res = min(res, solve(get<0>(nx)) + get<1>(nx));
	}
	return memo[p] = res;
}

int main() {
	ll L;
	int n;
	while (cin >> L >> n) {
		memo.clear(); field.clear();
		vector<int> points;
		points.push_back(0);
		for (int i = 0; i < n; i++) {
			int p, d, t;
			cin >> p >> d >> t;
			field[p + d].push_back(make_tuple(p, t));
			points.push_back(p);
			points.push_back(p + d);
		}
		points.push_back(L);
		sort(points.begin(), points.end());
		points.erase(unique(points.begin(), points.end()), points.end());
		for (int i = 1; i < points.size(); ++i) {
			const int pos = points[i];
			const int pre = points[i - 1];
			field[pos].push_back(make_tuple(pre, pos - pre));
		}
		// for (auto f : field) {
		// 	cout << f.first << ": ";
		// 	for (auto p : f.second) cout << '(' << get<0>(p)<<','<<get<1>(p) << ") ";
		// 	cout << endl;
		// }
		cout << solve(L) << endl;
	}
	return 0;
}
accuracy: 0.3409090909090909
time_ms: 10
memory_kb: 9456
final_rank: 9

code_snippet: #include <iostream>
#include <map>
#include <algorithm>
using namespace std;

map<int,int> dp;
int l,n;
int p[1001],d[1001],t[1001];

int rec(int now,int sum){
  if(now == l) return sum;
  if((dp[now] != '\0' && dp[now] < sum)) return dp[now]; 
  int ret = 2 << 28;
  for(int i=0;i<n;i++){
    if(now == p[i]){ret = min(rec(now+d[i],sum+t[i]),ret); break; }
  }
  ret = min(rec(now+1,sum+1),ret);
  
  return dp[now] = ret;
}

int main(){
  cin >> l >> n;
  for(int i=0;i<n;i++) cin >> p[i] >> d[i] >> t[i];
  cout << rec(0,0) << endl;;
}
accuracy: 0.09090909090909091
time_ms: 30
memory_kb: 13408
final_rank: 10

code_snippet: #include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
template<class T> inline T sqr(T x) {return x*x;}

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define exist(s,e) ((s).find(e)!=(s).end())
#define range(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)  range(i,0,n)
#define clr(a,b) memset((a), (b) ,sizeof(a))
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi  = acos(-1.0);
const ll INF =1LL << 62;
const int inf =1 << 29;

map<int,int> dp;
vi maho;

struct state{
	int p;
	int d;
	int t;
	bool operator <(state a)const{ return p < a.p; }
	bool operator >(state a)const{ return p > a.p; } 
};

vector<state> m;

int main(void){
	int l,n;
	cin >> l >> n;
	rep(i,n){
		state tmp;
		cin >> tmp.p >> tmp.d >> tmp.t;
		m.pb(tmp);
	}
	
	sort(m.begin(),m.end());
	rep(i,n){
		int p,d,t;
		p=m[i].p;
		d=m[i].d;
		t=m[i].t;
		if(dp.find(p)!=dp.end())
			dp[p]=min(dp[p],p);
		else
			dp[p]=p;
		rep(j,maho.size()){
			int rec=maho[j];
			if(rec<=p && dp.find(rec)!=dp.end())
				dp[p]=min(dp[p],dp[rec]+p-rec);
		}
		if(dp.find(p+d)!=dp.end())
			dp[p+d]=min(dp[p+d], min(dp[p]+t,p+d) );
		else
			dp[p+d]=min(dp[p]+t,p+d);
		maho.pb(p);
		maho.pb(p+d);
	//	cout << p << " "  << dp[p] << endl;
	//	cout << p+d << " "  << dp[p+d] << endl;
	}
	int ans=l;
	rep(i,maho.size()){
		int rec=maho[i];
		rec=l-rec+dp[rec];
		ans=min(rec,ans);
	}
	cout << ans << endl;
	return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 1336
final_rank: 3

code_snippet: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <climits>
#include <sstream>
#include <functional>
#include <complex>

using namespace std;

#define len(array)  (sizeof (array) / sizeof *(array))
#define rep(i, s, e) for(int i = s;i < e;i++)
#define Rep(i, e) for(int i = 0;i < e;i++)
#define rrep(i, e, s) for(int i = e;s <= i;i--)
#define Rrep(i, e) for(int i = e;0 <= i;i--)
#define mrep(i, e, t1, t2) for(map<t1, t2>::iterator i = e.begin(); i != e.end(); i++)
#define vrange(v) v.begin(), v.end()
#define vrrange(v) v.rbegin(), v.rend()
#define vsort(v) sort(vrange(v))
#define vrsort(v) sort(vrrange(v))
#define arange(a) a, a + len(a)
#define asort(a) sort(arange(a))
#define arsort(a, t) sort(arange(a), greater<t>())
#define afill(a, v) fill(arange(a), v)
#define afill2(a, v, t) fill((t *)a, (t *)(a + len(a)), v)
#define fmax(a, b) (a < b? b : a)
#define fmin(a, b) (a > b? b : a)
#define fabs(a) (a < 0? -a : a)
#define pb push_back
#define rg(e, s, t) s <= e && e < t
#define PQDecl(name, tp) priority_queue< tp, vector<tp>, greater<tp> > name;
//#define X real()
//#define Y imag()
//typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
//typedef complex<double> p;
const int INF = (int)2e9;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-10;
//const int dx[] = {1, -1, 0, 0, 1, -1, -1, 1};
//const int dy[] = {0, 0, 1, -1, -1, -1, 1, 1};
//const ll weight[] = {1e0,1e1,1e2,1e3,1e4,1e5,1e6,1e7,1e8,1e9,1e10,1e11,1e12,1e13};
#define MAX_N 1000

typedef struct _Node {
  _Node(int arg1 = 0, int arg2 = 0 , int arg3 = 0) {
	i = arg1;
	j = arg2;
	k = arg3;
  }
  int i,j,k;
  bool operator <(const struct _Node &e) const{
    return i == e.i? (j == e.j? k < e.k : j < e.j) : i < e.i;
  }
  bool operator >(const struct _Node &e) const{
    return i == e.i? (j == e.j? k > e.k : j > e.j) : i > e.i;
  }
}node;

void doIt(){
  int l, n;
  node magic[MAX_N];
  cin >> l >> n;
  Rep(i, n){
    cin >> magic[i].i >> magic[i].j >> magic[i].k;
  }
  sort(magic, magic + n);
  map<ll, ll> mmap[2];
  int prev = 1, now = 0;
  mmap[prev][0] = 0;
  Rep(i, n){
    mmap[now].clear();
    mrep(it, mmap[prev], ll, ll){
      ll e = (*it).first, time = (*it).second;
      // printf("i = %d, e = %lld, time = %lld\n", i, e, time);
      //先にいたら
      if(magic[i].i < e){
        if(mmap[now].count(e)){
          mmap[now][e] = fmin(mmap[now][e], time);
        }
        else{
          mmap[now][e] = time;
        }
      }
      else{
        ll dist = magic[i].i + magic[i].j;
        //warp
        if(mmap[now].count(dist)){
          mmap[now][dist] = fmin(mmap[now][dist], time + magic[i].i - e + magic[i].k);
        }
        else{
          mmap[now][dist] = time + magic[i].i - e + magic[i].k;
        }
        //not warp
        if(mmap[now].count(magic[i].i)){
          mmap[now][magic[i].i] = fmin(mmap[now][magic[i].i], time + magic[i].i - e);
        }
        else{
          mmap[now][magic[i].i] = time + magic[i].i - e;
        }
      }
    }
    prev = 1 - prev;
    now = 1 - now;
  }
  ll ans = l;
  mrep(it, mmap[prev], ll, ll){
    ans = fmin(ans, (*it).second + l - (*it).first);
  }
  cout << ans << endl;
}

int main() {
  doIt();
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 1248
final_rank: 1

code_snippet: #include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
template<class T> inline T sqr(T x) {return x*x;}

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define exist(s,e) ((s).find(e)!=(s).end())
#define range(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)  range(i,0,n)
#define clr(a,b) memset((a), (b) ,sizeof(a))
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi  = acos(-1.0);
const ll INF =1LL << 62;
const int inf =1 << 29;

map<int,int> dp;
vi maho;

int main(void){
	int l,n;
	cin >> l >> n;
	rep(i,n){
		int p,d,t;
		cin >> p >> d >> t;
		
		if(dp.find(p)!=dp.end())
			dp[p]=min(dp[p],p);
		else
			dp[p]=p;
		rep(j,maho.size()){
			int rec=maho[j];
			if(rec<=p && dp.find(rec)!=dp.end())
				dp[p]=min(dp[p],dp[rec]+p-rec);
		}
		if(dp.find(p+d)!=dp.end())
			dp[p+d]=min(dp[p+d], min(dp[p]+t,p+d) );
		else
			dp[p+d]=min(dp[p]+t,p+d);
		maho.pb(p);
		maho.pb(p+d);
	//	cout << p << " "  << dp[p] << endl;
	//	cout << p+d << " "  << dp[p+d] << endl;
	}
	int ans=l;
	rep(i,maho.size()){
		int rec=maho[i];
		rec=l-rec+dp[rec];
		ans=min(rec,ans);
	}
	cout << ans << endl;
	return 0;
}
accuracy: 0.45454545454545453
time_ms: 110
memory_kb: 1316
final_rank: 7

code_snippet: #include<iostream>
#include<queue>
#include<algorithm>
#include<stack>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
struct E{
  ll p,t;
};
bool operator<(E a,E b){
  return a.t>b.t;
}
ll L,n;
set<ll> node;
map<ll,vector<E> > edge;

void input(){
  cin>>L>>n;
  node.insert(0);
  node.insert(L);
  for(ll i=0;i<n;i++){
    ll p,d,t;
    cin>>p>>d>>t;
    edge[p].push_back((E){d,t});
    node.insert(p);
    node.insert(p+d);
  }
  for(auto i:node){
    //cout<<i<<endl;
  }
}

ll solve(){
  priority_queue<E> q;
  q.push((E){0,0});
  map<ll,int> dp;
  for(auto i:node){
    dp[i]=-1;
  }
  while(!q.empty()){
    E tmp = q.top();q.pop();
    if(tmp.p == L)return tmp.t;
    if(dp[tmp.p]!=-1&& tmp.t>=dp[tmp.p])continue;
    dp[tmp.p]=tmp.t;
    //cout<<tmp.p<<" "<<tmp.t<<endl;
    const vector<E> e = edge[tmp.p];
    for(int i=0;i<e.size();i++){
      q.push((E){tmp.p+e[i].p,tmp.t+e[i].t});
    }
    for(auto i:node){
      if(i>tmp.p){
        q.push((E){i,tmp.t + (i-tmp.p)});
      }
    }
  }
  return 0;
}


int main(){
  input();
  cout<<solve()<<endl;
}
accuracy: 1
time_ms: 410
memory_kb: 35792
final_rank: 6

code_snippet: #include <iostream>
#include <string>
#include <queue>
#include <map>
using namespace std;

struct NODE{
	int x,cost;
	NODE(int x,int cost) : x(x) ,cost(cost) {}
};
bool operator <(const NODE &a,const NODE &b){
	return a.cost > b.cost;
}

map<int, vector<NODE> > cost;

int main(){
	int L,n;
	cin >> L >> n;
	vector<int> vertex;
	vertex.push_back(0);
	vertex.push_back(L);
	
	for(int i = 0 ; i < n ; i++){
		int a,b,c;
		cin >> a >> b >> c;
		cost[a].push_back(NODE(b,c));
		vertex.push_back(a);
	}
	priority_queue<NODE> Q;
	map<int,int> memo;
	Q.push(NODE(0,0));
	while( Q.size() ){
		NODE q = Q.top(); Q.pop();
		if( memo[q.x]++ ) continue;
		if( q.x > L ) continue;
		if( q.x == L ){
			cout << q.cost << endl;
			return 0;
		}
		for(int i = 0 ; i < vertex.size() ; i++){
			if( vertex[i] > q.x ){
				Q.push(NODE(vertex[i],q.cost+vertex[i]-q.x));
			}
		}
		for(int i = 0 ; i < cost[q.x].size() ; i++){
			Q.push(NODE(q.x+cost[q.x][i].x,q.cost+cost[q.x][i].cost));
		}
	}
}
accuracy: 1
time_ms: 150
memory_kb: 10472
final_rank: 4