#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double ld;
#define int ll
#define yes cout<<"Yes" << endl;
#define no cout<<"No" << endl;
#define pb push_back
#define F first
#define S second
#define INF 1000000000000000000
ll t=1, n, m, k, q;
ll a, b, c=0, d;
ll c1, c2, c3, c4;
ll x = -1, y, z, nx, ny;
ll s, sum, res, mid, anns;
ll mx[] = {1, -1, 0, 0};
ll my[] = {0, 0, 1, -1};
string str, str1, str2;
bool tf=0, t1, t2;
char ch;
const int N = 1e6;
int mod = 1e9+7;
void prime(ll n) {
bool tf = 1;
if(n%2 == 0)
{cout << "2\n"; tf = 0;}
for(ll i = 3; i < n; i+=2) {
if(n % i == 0) {
cout << i << "\n";
tf = 0;
}
}
if(tf)
cout << n << " is prime\n";
}
ll num(char ch) {
return ch - '0';
}
ll mpower(ll x, ll y) {
if(y == 0)
return 1;
ll p = mpower(x, y/2);
if(y & 1)
return (((p * p)%mod) * x)%mod;
return (p * p)%mod;
}
ll power(ll x, ll y) {
if(y == 0)
return 1;
ll p = power(x, y/2);
if(y & 1)
return p * p * x;
return p * p;
}
void commented_Code() {/*const int MAXN = 100009;
const long long MOD = 1000000007;
long long fact[MAXN + 1], invFact[MAXN + 1];
void init() {
fact[0] = 1;
for (int i = 1; i <= MAXN; i++)
fact[i] = fact[i - 1] * i % MOD;
}
long long nCr(int n, int k) {
if (k < 0 || k > n) return 0;
return fact[n] * mpower(fact[k], mod-2) % MOD * mpower(fact[n-k], mod-2) % MOD;
}
*/}
vector<vector<pair<ll, ll>>> adj;
vector<ll> cost;
vector<bool> used;
void solve() {
cin >> n >> m;
adj.assign(n, vector<pair<ll, ll>>()), cost.assign(n, -1), used.assign(n, 0);
for(auto & i : cost) cin >> i;
for(ll i =0; i < m; i++) {
cin >> a >> b >> c;
adj[--a].pb({--b, c}), adj[b].pb({a, c});
}
priority_queue<pair<ll, ll>> q;
for(ll i = 0; i < n; i++) q.push({cost[i], i});
while(!q.empty()) {
tie(a, b) = q.top(), q.pop();
if(used[b]) continue;
used[b] = 1;
for(auto it : adj[b]) {
if(cost[it.F] < cost[b] - it.S) {
cost[it.F] = cost[b] - it.S;
q.push({cost[it.F], it.F});
}
}
}
for(auto i : cost)
cout << i << endl;
}
int32_t main() {
ios_base:: sync_with_stdio(false),cin.tie(NULL);
//freopen("revegetate.in" , "r" , stdin) ;
//freopen("revegetate.out" , "w" , stdout) ;
//init();
//cin >> t;
while(t--) {
solve();
}
/*
* https://codeforces.com/blog/entry/146051
* https://codeforces.com/blog/entry/22616
*/
return 0;
}