#include <bits/stdc++.h> typedef long long ll; const int maxx = 200010; const int inf = 0x3f3f3f3f; using namespace std; int d[maxx], pre[maxx], fa[maxx], vis[maxx], num[maxx]; vector<int> g[maxx]; bool cmp(int a, int b) { return d[a] > d[b]; } int getf(int a) { if (pre[a] == a) return pre[a]; return pre[a] = getf(pre[a]); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t, n, m; cin >> t; while (t--) { cin >> n >> m; for (int i = 1; i <= n; i++) g[i].clear(); for (int i = 1; i <= n; i++) { cin >> d[i]; pre[i] = i; vis[i] = fa[i] = 0; num[i] = i; } sort(num + 1, num + 1 + n, cmp); for (int i = 1; i <= m; i++) { int a, b; cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } for (int i = 1; i <= n; i++) { int x = num[i]; vis[x] = 1; for (auto it : g[x]) { if (!vis[it]) continue; int fy = getf(it); if (fy == x) continue; fa[fy] = pre[fy] = x; } } ll res = 0; for (int i = 1; i <= n; i++) res += d[i] - d[fa[i]]; cout << res << endl; } return 0; }
#include <bits/stdc++.h> typedef long long ll; const int maxx = 1010; const int inf = 0x3f3f3f3f; using namespace std; int f[maxx][maxx][5], ans[maxx]; ll res; int n, m; void dfs(int x, int a, int b, int c, int d) { if (x < 1) { ll tmp = 1ll * a * b * c * d; res = max(res, tmp); return; } if (!ans[x]) { dfs(x - 1, a, b, c, d); return; } for (int i = 1; i <= ans[x]; i++) dfs(x - 1, f[x][i][1] + a, f[x][i][2] + b, f[x][i][3] + c, f[x][i][4] + d); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { res = 0; cin >> n >> m; for (int i = 1; i <= m; i++) ans[i] = 0; for (int i = 1; i <= n; i++) { int cnt; cin >> cnt; ans[cnt]++; for (int j = 1; j <= 4; j++) cin >> f[cnt][ans[cnt]][j]; } dfs(1, 100, 100, 100, 100); cout << res << endl; } return 0; }