//#pragma GCC optimize ("O2")
//#pragma GCC target ("avx2")
//#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;

#include<cstdio>
#include<cstring>

using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

int N, M;
int to[400005], ne[400005], c[200005], he[100001];
int kotae[100001];

const int CM = 1 << 17, CL = 12;
char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
	if (ci - owa > 0) {
		memcpy(cn, owa, CL);
		ci -= CM;
		fread(cn + CL, 1, CM, stdin);
	}
	ll tmp = *(ll*)ci;
	int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
	tmp = tmp << dig & ma1;
	tmp = tmp * 10 + (tmp >> 8) & ma2;
	tmp = tmp * 100 + (tmp >> 16) & ma3;
	tmp = tmp * 10000 + (tmp >> 32) & ma4;
	ci += 72 - dig >> 3;
	return tmp;
}
const int MAX = 1000;
class shuturyoku_unko {
public:
	char C[MAX * 4];
	constexpr shuturyoku_unko() : C() {
		rep(i, MAX) {
			int X = i;
			rep(j, 3) {
				C[i * 4 + 2 - j] = '0' + X % 10;
				X /= 10;
			}
			C[i * 4 + 3] = ' ';
		}
	}
};
constexpr shuturyoku_unko f;
const int dm = 1 << 17;
char* dn = cn, * di = dn, * owad = dn + dm - 20;
void putint(int A) {
	if (owad < di) {
		fwrite(dn, 1, di - dn, stdout);
		di = dn;
	}
	if (A >= 1000) {
		int dig = 1;
		if (A >= 100000) dig = 3;
		else if (A >= 10000) dig = 2;
		memcpy(di, f.C + A / 1000 * 4 + 3 - dig, dig);
		memcpy(di + dig, f.C + A % 1000 * 4, 4);
		di += dig + 4;
	}
	else {
		int dig = 1;
		if (A >= 100) dig = 3;
		else if (A >= 10) dig = 2;
		memcpy(di, f.C + A * 4 + 3 - dig, dig + 1);
		di += dig + 1;
	}
}

int main() {
	//cin.tie(0);
	//ios::sync_with_stdio(false);


	N = getint();
	M = getint();

	int k = 2;
	rep1(i, M) {
		int u = getint(), v = getint();
		c[i] = getint();
		to[k] = v;
		ne[k] = he[u];
		he[u] = k++;
		to[k] = u;
		ne[k] = he[v];
		he[v] = k++;
	}

	int Q[100001], q = 0;
	Q[q++] = 1;
	kotae[1] = 1;
	while (q) {
		int tmp = Q[--q];
		int oya = kotae[tmp];
		for (int k = he[tmp]; k; k = ne[k]) {
			int t = to[k];
			if (!kotae[t]) {
				int e = c[k >> 1];
				kotae[t] = e + (oya == e);
				Q[q++] = t;
			}
		}
	}

	rep1(i, N) putint(kotae[i] - ((kotae[i] > N) << 1));
	fwrite(dn, 1, di - dn, stdout);

	Would you please return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define fi first
#define se second
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep0(i, n) for (ll i = 0; i <= (n); ++i)
#define rep1(i, n) for (ll i = 1; i <= (n); ++i)
#define rrep1(i, n) for (ll i = (n); i > 0; --i)
#define rrep0(i, n) for (ll i = (n); i >= 0; --i)
#define srep(i, s, t) for (ll i = s; i < t; ++i)
#define srep1(i, s, t) for (ll i = s; i <= t; ++i)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define isin(x, l, r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)), x.end())
#define snuke srand((unsigned)clock() + (unsigned)time(NULL));
#define show(x) cerr << #x << " = " << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define bn(x) ((1 << x) - 1)
#define dup(x, y) (((x) + (y)-1) / (y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;
constexpr ll MAX = 200002;

int main()
{
    ll n;
    cin >> n;
    vl a(MAX, 0);
    ll b = 0, maxb = 0, sum = 0, ans = 0;
    rep1(i, n) cin >> a[i];
    rep1(i, n)
    {
        b += a[i];
        maxb = max(maxb, b);
        ans = max(ans, sum + maxb);
        sum += b;
    }
    cout << ans << endl;
    return 0;
}