rt。
#include <iostream>
#define N 105
using namespace std;
int nxt[N], h[N], w[N], t[N], cnt;
void add(int x, int y, int v) {
nxt[++cnt] = h[x], h[x] = cnt, t[cnt] = y, w[cnt] = v;
}
int ind[N], oud[N], q[N], st = 1, ed;
int n, p, c[N], u[N];
int main() {
cin >> n >> p;
for (int i = 1; i <= n; i++) cin >> c[i] >> u[i];
if (!p) return cout << "1 " << c[1] << "\n", 0;
for (int i = 1; i <= p; i++) {
int x, y, v; cin >> x >> y >> v;
add(x, y, v); ind[y]++, oud[x]++;
}
for (int i = 1; i <= n; i++)
if (!ind[i]) q[++ed] = i;
while (st <= ed) {
int x = q[st++];
c[x] -= u[x];
if (c[x] <= 0) continue;
for (int i = h[x]; i; i = nxt[i]) {
int v = t[i]; ind[v]--;
if (!ind[v]) q[++ed] = v;
c[v] += w[i] * c[x];
}
}
bool Null = 1;
for (int i = 1; i <= n; i++)
if (!oud[i] && c[i] > 0) {
Null = 0;
cout << i << " " << c[i] << "\n";
}
if (Null) puts("NULL");
return 0;
}