Problem:
Gym with Many Rules
The gym in Izua Village has $N$ units of training equipment, numbered from 1 to $N$. To use a piece of training equipment once, you need one ticket with the number of the equipment written on it. The number of calories burned when using each piece of training equipment is determined for each piece of equipment.
After receiving some tickets for this gym, Atsushi went to the gym to get some exercise. At this gym, there are rules on the number of times the equipment can be used so that users don't overdo their exercise and hurt their bodies. For example, the rules say, "You can't use equipment number 2 more than or equalt to 5 times than equipment number 3". People who use the equipments have to follow the rules.
Atsushi wants to use the ticket he received to burn as many calories as possible within the limits allowed by the rules.
Given the ticket he received and the information about each equipment, write a program to find the maximum number of calories he can consume by following the rules.
Input
The input is given in the following form.
$N$ $R$
$t_1$ $e_1$
$t_2$ $e_2$
:
$t_N$ $e_N$
$a_1$ $b_1$ $c_1$
$a_2$ $b_2$ $c_2$
:
$a_R$ $b_R$ $c_R$
In the first line, the number of training equipments $N$ ($1 \leq N \leq 100,000$) and the number of rules $R$ ($0 \leq R \leq 100,000$) are given. In the following $N$ lines, the number of tickets Atsushi received for equipment $i$, $t_i$ ($1 \leq t_i \leq 200,000$), and the calories consumed when using the equipment $e_i$ ($0 \leq e_i \leq 100,000$) are given. In the following $R$ lines, we are given the numbers $a_i$ ($1 \leq a_i \leq N$), $b_i$ ($1 \leq b_i \leq N$), and $c_i$ ($1 \leq c_i \leq 100,000$) which represent the rule that the equipment numbered $a_i$ must not be used more than or equal to $c_i$ times than the equipment numbered $b_i$.
The input satisfies the following constraints.
Rules for the same pair of equipments can be given only once ($a_i \ne a_j$ or $b_i \ne b_j$ for $i \ne j$)。
No rule can be given for the same equipment itself($a_i \ne b_i$)。
Output
Output the maximum number of calories consumed in a line.
Sample Input and Output
Sample Input 1
3 2
5 1
10 4
6 2
2 1 3
3 2 1
Sample Output 1
45
The maximum number of times each equipment can be used with the ticket you received and following the rules is $5$ times for the number $1$, $7$ times for the number $2$, and $6$ times for the number $3$, so the maximum number of calories consumed is $5 \times 1 + 7 \times 4 + 6 \times 2 = 45$.
Sample Input 2
4 5
5 1
6 2
2 3
7 1
1 2 4
2 1 3
1 3 2
3 2 3
3 4 2
Sample Output 2
26
Sample Input 3
1 0
200000 100000
Sample Output 3
20000000000

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int N, R;
    if (!(cin >> N >> R)) return 0;
    vector<long long> t(N+1), e(N+1);
    for (int i = 1; i <= N; ++i) {
        long long ti, ei;
        cin >> ti >> ei;
        t[i] = ti;
        e[i] = ei;
    }
    // Graph: edge bi -> ai with weight (ci - 1)
    vector<vector<pair<int,int>>> g(N+1);
    for (int i = 0; i < R; ++i) {
        int a, b, c;
        cin >> a >> b >> c;
        int w = c - 1; // 「ci回以上多く使ってはいけない」 => 差は最大 ci-1
        g[b].emplace_back(a, w);
    }
    
    // Multi-source Dijkstra with initial distances = t[i]
    const long long INF = (1LL<<62);
    vector<long long> dist(N+1, INF);
    priority_queue<pair<long long,int>, vector<pair<long long,int>>, greater<pair<long long,int>>> pq;
    for (int i = 1; i <= N; ++i) {
        dist[i] = t[i];
        pq.emplace(dist[i], i);
    }
    while (!pq.empty()) {
        auto [d,u] = pq.top(); pq.pop();
        if (d != dist[u]) continue;
        for (auto [v,w] : g[u]) {
            long long nd = d + w;
            if (nd < dist[v]) {
                dist[v] = nd;
                pq.emplace(nd, v);
            }
        }
    }
    
    // Sum ei * dist[i]
    __int128 total = 0;
    for (int i = 1; i <= N; ++i) {
        // dist[i] is the maximal feasible usage count for machine i
        total += (__int128)e[i] * dist[i];
    }
    // Print 64-bit
    long long ans = (long long)total;
    cout << ans << '\n';
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 13432
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)

template<typename T> bool chmin(T& a, const T& b) {
	if (a > b) {
		a = b;
		return true;
	}
	else return false;
}


int main() {
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	
	int N, R;
	cin >> N >> R;
	
	vector<ll> E(N);
	vector<vector<pair<int, int>>> G(N + 1);
	rep(i, 0, N) {
		int t;
		cin >> t >> E[i];
		G[N].push_back({i, t});
	}
	
	rep(i, 0, R) {
		int a, b, c;
		cin >> a >> b >> c;
		--a, --b;
		G[b].push_back({a, c - 1});
	}
	
	priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> que;
	vector<ll> dist(N + 1, 1ll << 60);
	que.push({N, 0});
	dist[N] = 0;
	
	while (!que.empty()) {
		int v = que.top().first;
		ll d = que.top().second;
		que.pop();
		
		if (d > dist[v]) continue;
		
		for (pair<int, int> e : G[v]) {
			if (chmin(dist[e.first], dist[v] + e.second)) {
				que.push({e.first, dist[e.first]});
			}
		}
	}
	
	ll ans = 0;
	rep(i, 0, N) ans += dist[i] * E[i];
	cout << ans << '\n';
}
accuracy: 1
time_ms: 100
memory_kb: 13008
final_rank: 15

code_snippet: #include <bits/stdc++.h>
//#include"atcoder/all"
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
//using namespace atcoder;
#define all(a) a.begin(),a.end()
#define int long long
typedef long long ll;
typedef vector<ll> vi;
typedef pair<int,int> P;
typedef pair<P,P> PP;
const int inf=1ll<<55;
const ll mod=1000000007;
struct edge{
	int to,cost;
};
vector<edge>G[100005];
int t[100005],e[100005];
int d[100005];
signed main(){
	int n,r;cin>>n>>r;
	rep(i,n){
		cin>>t[i]>>e[i];
		G[0].push_back({i+1,t[i]});
	}
	rep(i,r){
		int a,b,c;cin>>a>>b>>c;
		c--;
		G[b].push_back({a,c});
	}
	priority_queue<P,vector<P>,greater<P>>Q;
	Q.push(P(0,0));
	rep(i,n)d[i+1]=inf;
	while(Q.size()){
		P p=Q.top();Q.pop();
		int v=p.second;
		if(d[v]<p.first)continue;
		for(auto e:G[v]){
			if(d[e.to]>d[v]+e.cost){
				d[e.to]=d[v]+e.cost;
				Q.push(P(d[e.to],e.to));
			}
		}
	}
	int ans=0;
	rep(i,n)ans+=d[i+1]*e[i];
	cout<<ans<<endl;
}
accuracy: 1
time_ms: 90
memory_kb: 15320
final_rank: 17

code_snippet: // AOJ #0423 Gym with Many Rules
// 2025.4.30

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = numeric_limits<int>::max();

#define gc() getchar_unlocked()

int Cin() {
	int n = 0; int c = gc();
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

int main(){
    int n = Cin(), r = Cin();
    vector<int> t(n+1), e(n+1);
    for(int i = 1; i <= n; i++) t[i] = Cin(), e[i] = Cin();

    vector<vector<pair<int,int>>> g(n+1);
    for(int i = 1; i <= n; i++) g[0].emplace_back(i, t[i]);

    for(int i = 0; i < r; i++){
        int a = Cin(), b = Cin(), c = Cin()-1;
        g[b].emplace_back(a, c);
    }

    vector<int> dist(n+1, INF);
    dist[0] = 0;
    priority_queue<pair<int,int>, vector<pair<int,int>>, greater<>> pq;
    pq.emplace(0, 0);

    while(!pq.empty()){
        auto [d, u] = pq.top(); pq.pop();
        if(d > dist[u]) continue;
        for(auto &ed : g[u]){
            int v = ed.first, w = ed.second;
            if(dist[v] > d + w){
                dist[v] = d + w;
                pq.emplace(dist[v], v);
            }
        }
    }
    ll ans = 0;
    for(int i = 1; i <= n; i++) ans += (ll)dist[i] * e[i];
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 11968
final_rank: 2

code_snippet: // AOJ #0423 Gym with Many Rules
// 2025.4.30

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1LL << 60;

#define gc() getchar_unlocked()

int Cin() {
	int n = 0; int c = gc();
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

int main(){
    int n = Cin(), r = Cin();
    vector<ll> t(n+1), e(n+1);
    for(int i = 1; i <= n; i++) t[i] = Cin(), e[i] = Cin();

    vector<vector<pair<int,ll>>> g(n+1);
    for(int i = 1; i <= n; i++) g[0].emplace_back(i, t[i]);
    for(int i = 0; i < r; i++){
        int a = Cin(), b = Cin();
        ll c = Cin()-1;
        g[b].emplace_back(a, c);
    }

    vector<ll> dist(n+1, INF);
    dist[0] = 0;
    priority_queue<pair<ll,int>, vector<pair<ll,int>>, greater<>> pq;
    pq.emplace(0, 0);

    while(!pq.empty()){
        auto [d, u] = pq.top(); pq.pop();
        if(d > dist[u]) continue;
        for(auto &ed : g[u]){
            int v = ed.first;
            ll w = ed.second;
            if(dist[v] > d + w){
                dist[v] = d + w;
                pq.emplace(dist[v], v);
            }
        }
    }
    __int128 ans = 0;
    for(int i = 1; i <= n; i++) ans += (__int128)dist[i] * e[i];

    if(ans == 0) cout << 0 << endl;
    else {
        string s;
        bool neg = false;
        if(ans < 0) neg = true, ans = -ans;
        while(ans > 0){
            int digit = ans % 10;
            s.push_back('0' + digit);
            ans /= 10;
        }
        if(neg) s.push_back('-');
        reverse(s.begin(), s.end());
        cout << s << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 15652
final_rank: 8

code_snippet: #include<iostream>
#include<vector>
#include<queue>
using namespace std;
int main()
{
  int n,m;
  cin >> n >> m;
  vector<pair<long long,long long>>num(n);
  for(int i=0;i<n;i++)cin >> num[i].first >> num[i].second;
  vector<vector<pair<int,int>>>graph(n);
  for(int i=0;i<m;i++){
    int a,b,c;
    cin >> a >> b >> c;
    graph[b-1].push_back(make_pair(a,c));
  }
  vector<int>ans(n);
  for(int i=0;i<n;i++)ans[i]=num[i].first;
  priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>prq;
  for(int i=0;i<n;i++)prq.push(make_pair(ans[i],i+1));
  while(prq.size()){
    pair<int,int>hako=prq.top();
    prq.pop();
    int a=hako.second;
    if(ans[a-1]!=hako.first)continue;
    for(int i=0;i<graph[a-1].size();i++)if(ans[a-1]+graph[a-1][i].second-1<ans[graph[a-1][i].first-1]){
      ans[graph[a-1][i].first-1]=ans[a-1]+graph[a-1][i].second-1;
      prq.push(make_pair(ans[graph[a-1][i].first-1],graph[a-1][i].first));
    }
  }
  long long Ans=0;
  for(int i=0;i<n;i++)Ans+=ans[i]*num[i].second;
  cout << Ans << endl;
}
accuracy: 1
time_ms: 90
memory_kb: 11580
final_rank: 10

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

int main() {
    int n, r;
    std::cin >> n >> r;
    std::vector<int> t(n), e(n);
    for (int i = 0; i < n; i++) std::cin >> t[i] >> e[i];
    std::vector graph(n, std::vector<std::pair<int, int>>());
    for (int i = 0; i < r; i++) {
        int a, b, c;
        std::cin >> a >> b >> c;
        a--; b--; c--;
        graph[b].emplace_back(a, c);
    }
    using P = std::pair<int, int>;
    std::priority_queue<P, std::vector<P>, std::greater<P>> que;
    for (int i = 0; i < n; i++) que.emplace(t[i], i);
    long long ans = 0;
    while (!que.empty()) {
        auto [p, u] = que.top();
        que.pop();
        if (t[u] < p) continue;
        ans += (long long)p * e[u];
        for (auto [v, w]: graph[u]) {
            if (t[v] > t[u] + w) {
                t[v] = t[u] + w;
                que.emplace(t[v], v);
            }
        }
    }
    std::cout << ans << '\n';
}
accuracy: 1
time_ms: 90
memory_kb: 10376
final_rank: 9

code_snippet: #include<bits/stdc++.h>
#define overload(a,b,c,d,...) d
#define rep1(a) for(ll _=0;_<(ll)a;++_)
#define rep2(i,n) for(ll i=0;i<(ll)n;++i)
#define rep3(i,l,r) for(ll i=(ll)l;i<(ll)r;++i)
#define rep(...) overload(__VA_ARGS__,rep3,rep2,rep1)(__VA_ARGS__)
#define rrep1(i,a) for(ll i=(ll)a-1;i>=0;--i)
#define rrep2(i,a,b) for(ll i=(ll)b-1;i>=(ll)a;--i)
#define rrep(...) overload(__VA_ARGS__,rrep2,rrep1)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pb push_back
#define eb emplace_back
using namespace std;
using ll=long long;
using ull=unsigned long long;
using ld=long double;
template<typename T,typename U>inline bool chmin(T&a,const U&b){return (a>b?a=b,true:false);}
template<typename T,typename U>inline bool chmax(T&a,const U&b){return (a<b?a=b,true:false);}
static constexpr ll mod1=998244353;
static constexpr ll mod2=1000000007;
const ll base1=random_device()()%mod1;
const ll base2=random_device()()%mod2;
int main(){
	ll n,r;cin>>n>>r;
	vector<ll>t(n),e(n);
	rep(i,n)cin>>t[i]>>e[i];
	vector<vector<pair<ll,ll>>>g(n);
	vector<ll>cnt(n);
	rep(i,r){
		ll a,b,c;cin>>a>>b>>c;
		a--,b--;
		g[b].eb(a,c);
		cnt[a]++;
	}
	set<pair<ll,ll>>st;
	rep(i,n)st.emplace(t[i],i);
	auto ans=t;
	queue<ll>que;
	rep(i,n)if(cnt[i]==0)que.emplace(i);
	while(!que.empty()||!st.empty()){
		//cerr<<st.size()<<endl;
		if(que.empty()){
			auto [ma,id]=*st.begin();
			cnt[id]=0;
			que.emplace(id);
			st.erase(st.begin());
		}
		else{
			auto id=que.front();que.pop();
			st.erase({ans[id],id});
			for(auto [to,c]:g[id])if(cnt[to]!=0){
				cnt[to]--;
				st.erase({ans[to],to});
				chmin(ans[to],ans[id]+c-1);
				st.emplace(ans[to],to);
				if(cnt[to]==0)que.emplace(to);
			}
		}
	}	
	ll sum=0;
	rep(i,n){
		//cerr<<ans[i]<<endl;
		sum+=ans[i]*e[i];
	}
	cout<<sum<<endl;
}
accuracy: 1
time_ms: 150
memory_kb: 17860
final_rank: 20

code_snippet: #include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <climits>
#include <functional>
#include <cassert>
#include <numeric>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define per(i, n) for(int i = (n) - 1; i >= 0; i--)
using ll = long long;
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
constexpr int mod = 1000000007;
using namespace std;
template<class T, class U>
bool chmax(T &a, const U &b){ return a < b ? (a = b, 1) : 0; }
template<class T, class U>
bool chmin(T &a, const U &b){ return a > b ? (a = b, 1) : 0; }
template <class T>
struct Edge {
  int to;
  T cost;
  Edge(){};
  Edge(int t, T c=1) : to(t), cost(c){}
  operator int() const{ return to; }
  bool operator<(const Edge &e){ return cost < e.cost; }
};
template <class T>
struct Graph : vector<vector<Edge<T>>> {
  Graph(){}
  Graph(const int &n) : vector<vector<Edge<T>>>(n){}
  void add_edge(int a, int b, T c=1){
    (*this)[a].push_back({ b, c });
  }
};
using graph = Graph<int>;
int main(){
  int n,m;
  cin >> n >> m;
  graph root(n + 1);
  vl c(n);
  rep(i, n){
    int t;
    cin >> t >> c[i];
    root[0].push_back({ i+1, t });
  }
  rep(i, m){
    int a,b,c;
    cin >> a >> b >> c;
    root[b].push_back({ a, c-1 });
  }
  priority_queue<pii, vector<pii>, greater<pii>> que;
  que.push({ 0, 0 });
  vi dist(n+1, mod);
  dist[0] = 0;
  while(!que.empty()){
    int val,pos;
    tie(val,pos) = que.top();
    que.pop();
    if(dist[pos] < val) continue;
    for(auto &x : root[pos]){
      if(chmin(dist[x], val + x.cost)){
        que.push({ dist[x], x });
      }
    }
  }
  ll ans = 0;
  rep(i, n) ans += c[i] * dist[i+1];
  cout << ans << "\n";
}
accuracy: 1
time_ms: 90
memory_kb: 11896
final_rank: 12

code_snippet: /* -*- coding: utf-8 -*-
 *
 * 0423.cc: Gym with Many Rules
 */

#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
#include<utility>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_M = 100000;

/* typedef */

typedef long long ll;
typedef pair<int,int> pii;
typedef vector<pii> vpii;

/* global variables */

int ts[MAX_N], es[MAX_N];
vpii nbrs[MAX_N];

/* subroutines */

/* main */

int main() {
  int n, m;
  scanf("%d%d", &n, &m);

  for (int i = 0; i < n; i++) scanf("%d%d", ts + i, es + i);

  for (int i = 0; i < m; i++) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    u--, v--;
    nbrs[v].push_back(pii(u, w - 1));
  }

  priority_queue<pii> q;
  for (int i = 0; i < n; i++) q.push(pii(-ts[i], i));

  while (! q.empty()) {
    pii ue = q.top(); q.pop();
    int ut = -ue.first, u = ue.second;
    if (ts[u] != ut) continue;

    for (auto vw: nbrs[u]) {
      int v = vw.first, vt = ut + vw.second;
      if (ts[v] > vt) {
	ts[v] = vt;
	q.push(pii(-vt, v));
      }
    }
  }

  ll sum = 0;
  for (int i = 0; i < n; i++) sum += (ll)ts[i] * es[i];

  printf("%lld\n", sum);
  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 9852
final_rank: 1

code_snippet: #ifdef LOGX
#define _GLIBCXX_DEBUG
#endif
#include<bits/stdc++.h>
using namespace std;

using ll=long long;
#define rep(i,n) for(int i=0;i<int(n);i++)
#define rep2(i,s,n) for(int i=int(s);i<int(n);i++)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
const int big=1e9+7;
//const int big=998244353;
const char newl='\n';
const int inf=1e9+10;
const ll INF=ll(1e18)+10;
const int dx[]={1,0,-1,0};
const int dy[]={0,1,0,-1};

using P=pair<ll,int>;
priority_queue<P,vector<P>,greater<P>> q;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(10);

    int n,r;
    cin >> n >> r;
    vector<ll> e(n);
    rep(i,n){
        int t;
        cin >> t >> e[i];
        q.emplace(t,i);
    }
    vector<vector<pair<int,ll>>> g(n);
    rep(i,r){
        int a,b,c;
        cin >> a >> b >> c;
        a--,b--;
        g[b].emplace_back(a,c-1);
        //[a]<[b]+c
    }

    ll ans=0;
    while(!q.empty()){
        auto now=q.top();ll T=now.first;int i=now.second;
        q.pop();
        if(e[i]==-1)continue;
        ans+=e[i]*T;
        e[i]=-1;
        for(auto ne:g[i]){
            q.emplace(T+ne.second,ne.first);
        }
    }
    cout << ans << newl;
}
//x[i]<x[j]+c
//x[i]-x[j]<c
//tが最小のところは上限に設定すべき。
//そこからは、tと新たにできた上限の小さい方を取りながらpriority_queueかな
//既に作られた条件を満たしつつ最大？
accuracy: 1
time_ms: 50
memory_kb: 10956
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
const long long mod1 = 1000000007;
const long long mod2 = 998244353;
double pi = 3.141592653589;
using ll = long long;
vector<ll> dx = {-1,0,1,0,1,1,-1,-1};
vector<ll> dy = {0,1,0,-1,1,-1,1,-1};
vector<char> abc = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
vector<char> ABC = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
 
struct UnionFind{
    vector<ll> par, siz;
    
    UnionFind(ll n):par(n,-1),siz(n,1){}
    
    ll root(ll x){//親を返す
        if(par[x] == -1) return x;
        else return par[x] = root(par[x]);
    }
    
    bool issame(ll x,ll y){//同じグループかどうか
        return root(x) == root(y);
    }
    
    bool unite(ll x, ll y){//合体
        x = root(x); y = root(y);
        if(x == y) return false;
        if(siz[x] < siz[y]) swap(x,y);
        par[y] = x;
        siz[x] += siz[y];
        return true;
    }
    
    ll size(ll x){//xがいるグループのサイズ
        return siz[root(x)];
    }
    
    
    
};
 
long long pow2(long long x,long long n){
    long long ans = 1;
    while(n != 0){
        long long a = x;
        long long z = 1;
        while(z*2 <= n){
            a *=a;
            a=a%mod2;
            z*=2;
        }
        ans*=a;
        ans=ans%mod2;
        n-=z;
    }
    return ans; 
}//累乗 x^n
 
template<typename T>
void input_vec(vector<T> &A){
    ll N = A.size();
    for(int i = 0; i < N; i++){
        cin >> A.at(i);
    }
    return;
}//入力を配列で受け取る
 
template<typename T>
void output_vec(vector<T> &A){
    ll N = A.size();
    for(int i = 0; i < N; i++){
        if(i != 0){
            cout << " ";
        }
        cout << A.at(i);
    }
    cout << endl;
    return;
}//配列を出力
 
template<typename T>
long long count_x(vector<T> &A,T x){
    long long counter = 0;
    for(int i = 0; i < (int)A.size(); i++){
        if(A.at(i) == x){
            counter++;
        }
    }
    return counter;
}//配列内のxの個数をreturn
 
template<typename T>
long long duplicate(vector<T> &A){
    ll ans = 0;
    ll N = A.size();
    T a = A.at(0);
    for(int i = 1; i < N; i++){
        if(a == A.at(i)) ans++;
        else a = A.at(i);
    }
    return ans;
}
 
template<typename T>
void chmin(T &a,T b){
    if(a>b){
        a = b;
    }
    return;
}
 
template<typename T>
void chmax(T&a,T b){
    if(a < b){
        a = b;
    }
    return;
}
 
/*
絶対値   abs()
配列の要素の追加   push_back() 
char→intに変換   -'0'
string→intに変換   atoi(文字列名.c_str())
配列の中身をN個の０で初期化   配列名.resize(N)
配列の中身をN個のaで初期化   配列名.assign(N,a)
配列の中からK以上の数字の最小値を出力　lower_bound(配列.begin(),配列.end(),K)
*/
int main(){
    ll N,R; cin >> N >> R;
    vector<ll> t(N),e(N);
    for(int i = 0; i < N; i++) cin >> t[i] >> e[i];
    vector<vector<pair<ll,ll>>> m(N);
    priority_queue<pair<ll,ll>,vector<pair<ll,ll>>, greater<pair<ll,ll>>> pq;
    for(int i = 0; i < R; i++){
        ll a,b,c; cin >> a >> b >> c;
        a--;b--;c--;
        m[b].push_back(make_pair(a,c));
        pq.push(make_pair(t[b]+c,a));
        //cout << t[b]+c << " " << a << endl;
    }
    //cout << endl;
    while(!pq.empty()){
        //cout << pq.size() << endl;
        ll a,b; tie(b,a) = pq.top();
        //cout << a << " " << b << endl;
        pq.pop();
        if(t[a]<b)continue;
        t[a] = b;
        
        for(auto z:m[a]){
            ll x,y; tie(x,y) = z;
            //cout <<" "<< x << " " << y << endl;
            if(t[x]<=y+t[a])continue;
            pq.push(make_pair(y+t[a],x));
            t[x] = t[a]+y;
        }
    }
    ll ans = 0;
    //output_vec(t);
    for(int i = 0; i < N; i++){
        ans+=t[i]*e[i];
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 90
memory_kb: 11820
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T> using vc = vector<T>;
template<class T> using vvc = vc<vc<T>>;
template<class T> using vvvc = vc<vvc<T>>;
template<class T> using vvvvc = vvc<vvc<T>>;
template<class T> using PQ = priority_queue<T>;
template<class T> using invPQ = priority_queue<T, vector<T>, greater<T>>;
using IP = pair<int, int>;
using LP = pair<ll, ll>;
#define all(x) begin(x), end(x)
#define allr(x) rbegin(x), rend(x)
#define rep(i, n) for (int i = 0; (i) < (int)(n); i++)
#define rep3(i, m, n) for (int i = (m); (i) < (int)(n); i++)
#define repr(i, n) for (int i = (n) - 1; (i) >= 0; i--)
#define rep3r(i, m, n) for (int i = (n) - 1; (i) >= (int)(m); i--)
template<class T> inline bool chmax(T & a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T & a, T b) { if (a > b) { a = b; return 1; } return 0; }

// ll dfs2(int v, int p, int s, vvc<IP> &graph, int lli, vc<int> &cnt, vc<int> &llmemo, vc<int> &e) {
//     if (v == s) {
//         return 0;
//     }
//     cnt[v]--;
//     ll res = 0;
//     rep (i, graph[v].size()) {
//         int u = graph[v][i].first;
//         if (u == p || cnt[u] == 0) continue;
//         res += dfs2(u, v, s, graph, min(lli, llmemo[v]) + graph[v][i].second - 1, cnt, llmemo, e);
//     }
//     return res + (ll)min(lli, llmemo[v]) * e[v];
// }

// int dfs1(int v, int p, int s, int mini, vvc<IP> &graph, vc<int> &cnt, vc<int> &llmemo, vc<int> &e, ll &ans) {
//     if (v == s) {
//         return mini;
//     }
//     rep (i, graph[v].size()) {
//         int u = graph[v][i].first;
//         if (u == p || cnt[u] == 0) continue;
//         int m = dfs1(u, v, s, min(mini, llmemo[v]), graph, cnt, llmemo, e, ans);
//         if (llmemo[u] == m) {
//             ans += dfs2(v, p, v, graph, llmemo[v], cnt, llmemo, e);
//             return -1;
//         }
//         return m;
//     }
//     return -1;
// }

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int N, R;
    cin >> N >> R;
    vc<int> t(N), e(N), a(R), b(R), c(R);
    rep (i, N) cin >> t[i] >> e[i];
    rep (i, R) {
        cin >> a[i] >> b[i] >> c[i];
        a[i]--; b[i]--; c[i];
    }

    vvc<IP> graph(N), rgraph(N);
    rep (i, R) {
        graph[a[i]].push_back({b[i], c[i]});
        rgraph[b[i]].push_back({a[i], c[i]});
    }

    vc<int> cnt(N);
    rep (i, N) cnt[i] = graph[i].size();
    vc<int> llmemo(N);
    rep (i, N) llmemo[i] = t[i];
    ll ans = 0;
    set<IP> st;
    rep (i, N) st.insert({t[i], i});
    while (!st.empty()) {
        int p = (*st.begin()).second;
        st.erase({llmemo[p], p});
        ans += (ll)llmemo[p] * e[p];
        rep (i, rgraph[p].size()) {
            int np = rgraph[p][i].first;
            cnt[np]--;
            if (llmemo[np] > llmemo[p] + rgraph[p][i].second - 1) {
                st.erase({llmemo[np], np});
                llmemo[np] = llmemo[p] + rgraph[p][i].second - 1;
                st.insert({llmemo[np], np});
            }
        }
    }

    // rep (i, N) {
    //     if (cnt[i] != 0) {
    //         dfs1(i, -1, i, 200000, rgraph, cnt, t, e, ans);
    //     }
    // }

    cout << ans << endl;
}
accuracy: 1
time_ms: 70
memory_kb: 21492
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(){
  int N, R;
  cin >> N >> R;
  vector<int> t(N), e(N);
  for (int i = 0; i < N; i++){
    cin >> t[i] >> e[i];
  }
  vector<int> a(R), b(R), c(R);
  for (int i = 0; i < R; i++){
    cin >> a[i] >> b[i] >> c[i];
    a[i]--;
    b[i]--;
  }
  vector<vector<pair<int, int>>> E(N);
  for (int i = 0; i < R; i++){
    E[b[i]].push_back(make_pair(c[i] - 1, a[i]));
  }
  vector<int> d(N, -1);
  priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
  for (int i = 0; i < N; i++){
    pq.push(make_pair(t[i], i));
  }
  while (!pq.empty()){
    int dd = pq.top().first;
    int v = pq.top().second;
    pq.pop();
    if (d[v] == -1){
      d[v] = dd;
      for (auto P : E[v]){
        int w = P.second;
        if (d[w] == -1){
          pq.push(make_pair(dd + P.first, w));
        }
      }
    }
  }
  long long ans = 0;
  for (int i = 0; i < N; i++){
    ans += (long long) d[i] * e[i];
  }
  cout << ans << endl;
}
accuracy: 1
time_ms: 90
memory_kb: 11980
final_rank: 13

code_snippet: #include<iostream>
#include<queue>
#include<vector>
using namespace std;
int N,M;
long T[1<<17],E[1<<17];
bool out[1<<17];
vector<pair<int,int> >G[1<<17];
main()
{
	cin>>N>>M;
	priority_queue<pair<int,int> >P;
	for(int i=0;i<N;i++)
	{
		cin>>T[i]>>E[i];
		P.push(make_pair(-T[i],i));
	}
	for(int i=0;i<M;i++)
	{
		int a,b,c;cin>>a>>b>>c;
		G[b-1].push_back(make_pair(a-1,c-1));
	}
	long ans=0;
	while(!P.empty())
	{
		int u=P.top().second;P.pop();
		if(out[u])continue;
		out[u]=true;
		ans+=T[u]*E[u];
		for(pair<int,int>e:G[u])
		{
			int v=e.first,t=T[u]+e.second;
			if(T[v]>t)
			{
				T[v]=t;
				P.push(make_pair(-t,v));
			}
		}
	}
	cout<<ans<<endl;
}
accuracy: 1
time_ms: 120
memory_kb: 11512
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using VI = vector<int>;
using VL = vector<ll>;
using VS = vector<string>;
template<class T> using PQ = priority_queue<T, vector<T>, greater<T>>;
#define FOR(i,a,n) for(int i=(a);i<(n);++i)
#define eFOR(i,a,n) for(int i=(a);i<=(n);++i)
#define rFOR(i,a,n) for(int i=(n)-1;i>=(a);--i)
#define erFOR(i,a,n) for(int i=(n);i>=(a);--i)
#define SORT(a) sort(a.begin(),a.end())
#define rSORT(a) sort(a.rbegin(),a.rend())
#define fSORT(a,f) sort(a.begin(),a.end(),f)
#define all(a) a.begin(),a.end()
#define out(y,x) ((y)<0||h<=(y)||(x)<0||w<=(x))
#define tp(a,i) get<i>(a)
#ifdef _DEBUG
#define line cout << "-----------------------------\n"
#define stop system("pause")
#endif
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
constexpr ld eps = 1e-10;
constexpr ld pi = 3.1415926535897932;
template<class T>inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; }return false; }
template<class T>inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; }return false; }
inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }
template<class T>inline istream& operator>>(istream& is, vector<T>& v) { for (auto& a : v)is >> a; return is; }
template<class T, class U>inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template<class T>inline vector<T> vec(size_t a) { return vector<T>(a); }
template<class T>inline vector<T> defvec(T def, size_t a) { return vector<T>(a, def); }
template<class T, class... Ts>inline auto vec(size_t a, Ts... ts) { return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...)); }
template<class T, class... Ts>inline auto defvec(T def, size_t a, Ts... ts) { return vector<decltype(defvec<T>(def, ts...))>(a, defvec<T>(def, ts...)); }
template<class T>inline void print(const T& a) { cout << a << "\n"; }
template<class T, class... Ts>inline void print(const T& a, const Ts&... ts) { cout << a << " "; print(ts...); }
template<class T>inline void print(const vector<T>& v) { for (int i = 0; i < v.size(); ++i)cout << v[i] << (i == v.size() - 1 ? "\n" : " "); }
template<class T>inline void print(const vector<vector<T>>& v) { for (auto& a : v)print(a); }
inline string reversed(const string& s) { string t = s; reverse(all(t)); return t; }
template<class T>inline T sum(const vector<T>& a, int l, int r) { return a[r] - (l == 0 ? 0 : a[l - 1]); }
template<class T>inline void END(T s) { print(s); exit(0); }
void END() { exit(0); }

int main() {
    init();

    int n, m; cin >> n >> m;
    VI t(n), e(n);
    FOR(i, 0, n)cin >> t[i] >> e[i];

    vector<vector<pair<int, int>>> g(n + 1);
    FOR(i, 0, n)g[n].emplace_back(i, t[i]);
    FOR(i, 0, m) {
        int a, b, c; cin >> a >> b >> c;
        g[b - 1].emplace_back(a - 1, c - 1);
    }

    VL dp(n + 1, LLINF);
    dp[n] = 0;
    PQ<pair<ll, int>> dik;
    dik.emplace(0, n);
    while (!dik.empty()) {
        ll d; int cur;
        tie(d, cur) = dik.top();
        dik.pop();
        if (dp[cur] < d)continue;
        for(auto [to,cost] : g[cur]) {
            if (!chmin(dp[to], d + cost))continue;
            dik.emplace(d + cost, to);
        }
    }

    ll ans = 0;
    FOR(i, 0, n)ans += ll(dp[i]) * ll(e[i]);
    print(ans);

    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 12908
final_rank: 7

code_snippet: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n); i--;)
#define all(x) x.begin(), x.end()
using ll = long long;

template <class T, class U>
bool chmin(T& x, U y) {
    if (x > y) {
        x = y;
        return true;
    } else {
        return false;
    }
}

template <class T, class U>
bool chmax(T& x, U y) {
    if (x < y) {
        x = y;
        return true;
    } else {
        return false;
    }
}

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);

    using T = std::pair<int, int>;

    int n, r; std::cin >> n >> r;

    std::vector<int> t(n), e(n);
    rep(i, n) std::cin >> t[i] >> e[i];

    std::vector<std::vector<T>> graph(n);
    rep(i, r) {
        int a, b, c; std::cin >> a >> b >> c;
        a--; b--;
        graph[b].emplace_back(a, c - 1);
    }

    std::priority_queue<T, std::vector<T>, std::greater<>> pque;
    rep(i, n) pque.emplace(t[i], i);
    while (!pque.empty()) {
        int d, v; std::tie(d, v) = pque.top(); pque.pop();
        if (t[v] < d) continue;
        for (auto [v2, dv] : graph[v])
            if (chmin(t[v2], d + dv))
                pque.emplace(d + dv, v2);
    }

    ll res = 0;
    rep(i, n) res += (ll)t[i] * e[i];
    std::cout << res << "\n";
}
accuracy: 1
time_ms: 40
memory_kb: 10400
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)

struct Edge { int to; LL d; };
struct QueueNode {
	int p; LL d;
	bool operator<(QueueNode r) const { return d > r.d; }
};

int N, M;
vector<vector<Edge>> E;
vector<LL> D;

LL W[100000];

int main() {
	cin >> N >> M;
	E.resize(N + 1);
	rep(i, N) {
		int t, e; cin >> t >> e;
		E[N].push_back({ i,t });
		W[i] = e;
	}
	rep(i, M) {
		int u, v, c; cin >> u >> v >> c; u--; v--;
		E[v].push_back({ u,c-1 });
	}

	D.assign(N + 1, -1);

	priority_queue<QueueNode> G;
	G.push({ N,0 });
	while (G.size()) {
		int p = G.top().p;
		LL d = G.top().d;
		G.pop();
		if (D[p] != -1) continue;
		D[p] = d;
		for (Edge& e : E[p]) {
			G.push({ e.to, d + e.d });
		}
	}

	LL ans = 0;
	rep(i, N) ans += D[i] * W[i];
	cout << ans << endl;

	return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 14596
final_rank: 18

code_snippet: #include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <random>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define repp(i,n,m) for (int i = m; i < (n); ++i)
#define repl(i,n) for (long long i = 0; i < (n); ++i)
#define reppl(i,n,m) for (long long i = m; i < (n); ++i)
//#define int long long
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PI = pair<pair<int,int>,int>;
using PL = pair<long long, long long>;
using Pxy = pair<double, double>;
using Tiib = tuple<int, int, bool>;
const int INF = 1001001007;
const long long mod = 1000000007LL;
const int MOD = 998244353;
const ll inf = 1e18;
template <typename AT>
void printvec(vector<AT> &ar){
    rep(i,ar.size()-1) cout << ar[i] << " ";
    cout << ar[ar.size()-1] << endl;
}
template <typename Q>
void printvvec(vector<vector<Q>> &ar){
    rep(i,ar.size()){
        rep(j,ar[0].size()-1) cout << ar[i][j] << " ";
        cout << ar[i][ar[0].size()-1] << endl;
    }
}
template <typename S>
bool range(S a, S b, S x){
    return (a <= x && x < b);
}

void yes(){
    cout << "Yes" << endl;
}

void no (){
    cout << "No" << endl;
}

ll cel (ll a, ll b){
    if (a % b == 0) return a / b;
    else return a / b + 1;
}

ll gcds(ll a, ll b){
    ll c = a % b;
    while (c != 0){
        a = b;
        b = c;
        c = a % b;
    }
    return b;
}

struct vs{
    ll cal;
    ll ma;
    vector<PL> to;
};

int main(){
    int n, r; cin >> n >> r;
    vector<vs> ar(n);
    rep(i,n){
        ll a, b; cin >> a >> b;
        ar[i].cal = b;
        ar[i].ma = a;
    }
    rep(i,r){
        int a, b; cin >> a >> b; ll c; cin >> c;
        ar[b-1].to.emplace_back(P(a-1,c-1LL));
    }
    priority_queue<PL,vector<PL>,greater<PL>> que;
    rep(i,n){
        que.push(PL(ar[i].ma,i));
    }
    while (!que.empty()){
        PL p = que.top(); que.pop();
        ll v = p.second;
        if (ar[v].ma < p.first) continue;
        rep(i,ar[v].to.size()){
            int tt = ar[v].to[i].first;
            if (ar[tt].ma > ar[v].ma + ar[v].to[i].second){
                ar[tt].ma = ar[v].ma + ar[v].to[i].second;
                que.push(P(ar[tt].ma,tt));
            }
        }
    }
    ll ans = 0LL;
    rep(i,n) ans += ar[i].cal * ar[i].ma;
    cout << ans << endl;
}
accuracy: 1
time_ms: 110
memory_kb: 11892
final_rank: 14

code_snippet: #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

template <typename T>
struct edge {
    int from, to; T cost;
    edge(int to, T cost) : from(-1), to(to), cost(cost) {}
    edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}
};

template <typename T>
vector<T> dijkstra(int s,vector<vector<edge<T>>> &G){
    auto n = G.size();
    vector<T> d(n, INF<T>);
    priority_queue<pair<T, int>,vector<pair<T, int>>,greater<>> Q;
    d[s] = 0;
    Q.emplace(0, s);
    while(!Q.empty()){
        T cost; int i;
        tie(cost, i) = Q.top(); Q.pop();
        if(d[i] < cost) continue;
        for (auto &&e : G[i]) {
            auto cost2 = cost + e.cost;
            if(d[e.to] <= cost2) continue;
            d[e.to] = cost2;
            Q.emplace(d[e.to], e.to);
        }
    }
    return d;
}

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<edge<int>>> G(n+1);
    vector<ll> e(n);
    for (int i = 0; i < n; ++i) {
        int t;
        scanf("%d %lld", &t, &e[i]);
        G[0].emplace_back(i+1, t);
    }
    for (int i = 0; i < m; ++i) {
        int a, b, c;
        scanf("%d %d %d", &a, &b, &c);
        G[b].emplace_back(a, c-1);
    }
    auto d = dijkstra(0, G);
    ll ans = 0;
    for (int i = 0; i < n; ++i) {
        ans += e[i]*d[i+1];
    }
    cout << ans << "\n";
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 12876
final_rank: 6