1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
//#include "stdafx.h"

#include <cassert>
#include <iostream>
#include <istream>
#include <fstream>
#include <algorithm>
#include <cstdio>
#include <complex>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <string>
#include <cstdlib>
#include <memory>
#include <ctime>
#include <bitset>
#include <queue>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <climits>

using namespace std;

#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define REP(i,n) for(int i = 0; i < (n); ++i)
#define REPD(i, a, b) for(int i = (a); i > (b); i--)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define RS(X) scanf("%s", (X))
#define CASET int ___T, case_n = 1; scanf("%d ", &___T); while (___T-- > 0)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define LL long long
#define ULL unsigned long long
#define PII pair<int,int>
#define VI vector<int>
#define VLL vector<long long>
#define VULL vector<unsigned long long>
#define VPII vector<pair<int,int> >
#define PLL pair<long long,long long>
#define VPLL vector<pair<long long,long long> >
#define F first
#define S second
#define forIt(it, a) for(__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define forRev(it, a) for(__typeof((a).rbegin()) it = (a).rbegin(); it != (a).rend(); it++)
#define tr(container, it) for (typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define present(container, element) (container.find(element) != container.end())
#define ft(a) __typeof((a).begin())
#define ld long double
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define bitcount(n) __builtin_popcountll(n)
#define pii pair<int, int>

typedef complex<ld> cplex;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;

#define SORT(v)			sort((v).begin(),(v).end())
#define UN(v)			SORT(v),(v).erase(unique((v).begin(),(v).end()),(v).end()) 

const int MOD = (int)1e9 + 7;
const int SIZE = (int)1e6 + 10;
const double EPS = (double)1e-12;
const double PI = (double)acos(-1);

class Change {
public: 
	int option;
	int city1;
	int city2;
	LL val;
};

class Ban2017
{
public:

	int n, q;
	LL *citySales;
	LL **connections;
	vector<int> solutions;
	vector<Change> changes;
	bool* visited;
	vector < vector<int> > graph;

	LL **create() {
		LL ** game = new LL*[n];
		for (int i = 0; i < n; ++i) {
			game[i] = new LL[n];
		}
		return game;
	}

	void Read() {
		cin >> n;
		cin >> q;
		citySales = new LL[n];
		visited = new bool[n];

		for (int i = 0; i < n; i++) {
			cin >> citySales[i];
			visited[i] = false;
			vector<int> empty;
			graph.push_back(empty);
		}

		connections = create();

		for (int i = 0; i < n - 1; i++) {
			int ai, bi, ci;
			cin >> ai >> bi >> ci;
			connections[ai - 1][bi - 1] = ci;
			connections[bi - 1][ai - 1] = ci;

			graph[ai - 1].push_back(bi - 1);
			graph[bi - 1].push_back(ai - 1);
		}
		for (int i = 0; i < q; i++) {
			int option;
			Change change;
			cin >> option;
			change.option = option;

			if (option == 1) {
				int vi;
				LL di;
				cin >> vi >> di;
				change.city1 = vi-1;
				change.val = di;
			}
			else {
				int ai, bi;
				LL di;
				cin >> ai >> bi >> di;
				change.city1 = ai-1;
				change.city2 = bi-1;
				change.val = di;
			}

			changes.push_back(change);
		}
	}

	LL currentConnectionCost = 0;
	LL bestProfit = LLONG_MIN;
	int bestCityNr = -1;

	void dfs(int nr_from, int nr_to, LL currentCost) {
		visited[nr_to] = true;

		LL toCitySale = citySales[nr_to];
		LL connectionCost = connections[nr_from][nr_to];
		currentCost += connectionCost;

		if (bestProfit < toCitySale - currentCost) {
			bestProfit = toCitySale - currentCost;
			bestCityNr = nr_to;
		}
		else if (bestProfit == toCitySale - currentCost) {
			if (nr_to < bestCityNr) {
				bestCityNr = nr_to;
			}
		}

		for (int next : graph[nr_to]) {
			if (!visited[next]) {
				dfs(nr_to, next, currentCost);
			}
		}
	}

	void dfs(int nr_from, LL currentCost) {
		visited[nr_from] = true;

		for (int next : graph[nr_from]) {
			if (!visited[next]) {
				dfs(nr_from, next, currentCost);
			}
		}
	}

	void cleanVisited() {
		for (int i = 0; i < n; i++)
		{
			visited[i] = false;
		}
	}

	void update(int i) {
		Change change = changes[i];
		if (change.option == 1) {
			citySales[change.city1] = change.val;
		}
		else {
			connections[change.city1][change.city2] = change.val;
		}

		currentConnectionCost = 0;
		bestProfit = LLONG_MIN;
		bestCityNr = -1;

		cleanVisited();
	}

	//ifstream fcin;
	void virtual Solve()
	{
		int from = 0;
		for (int i = 0; i < q; i++) {
			update(i);
			dfs(from, 0);
			solutions.push_back(bestCityNr + 1);
			from = bestCityNr;
		}
	}

	void Clean() {
		delete citySales;
		delete visited;
		//TODO: Clean connections
	}

	void Print() {
		for (int i = 0; i < q; i++) {
			cout << solutions[i] << " ";
		}
	}
};

int main()
{
	Ban2017 *p = new Ban2017();
	p->Read();
	p->Solve();
	p->Clean();
	p->Print();

	delete p;
	return 0;
}