Submission #68376642
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
#ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG
#include <iostream>
template <typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {return os<<'(' <<p.first<<", "<<p.second<<')';}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {os<<'[';for(auto it=v.begin();it!=v.end();++it){os<<*it; if(next(it)!=v.end()){os<<", ";}}return os<<']';}
template <typename T>
ostream& operator<<(ostream& os, const vector<vector<T>>& vv) {os<<"[\n";for(auto it=vv.begin();it!=vv.end();++it){os<<" "<<*it;if(next(it)!=vv.end()){os<<",\n";}else{os<<"\n";}}return os<<"]";}
#define dump(x) cerr << #x << " = " << (x) << '\n'
#else
#define dump(x) (void)0
#endif
using ll = long long;
using ld = long double;
using P = pair<ll,ll>;
template<class T> using pq = priority_queue<T>;
template<class T> using pq_g = priority_queue<T, vector<T>, greater<T>>;
#define out(x) cout<<x<<'\n'
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define OVERLOAD_REP(_1,_2,_3,name,...) name
#define REP1(i,n) for (auto i = std::decay_t<decltype(n)>{}; (i) != (n); ++(i))
#define REP2(i, l, r) for (auto i = (l); (i) != (r); ++(i))
#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)
template<class T> inline bool chmin(T& a, T b) {if(a > b){a = b; return true;} else {return false;}};
template<class T> inline bool chmax(T& a, T b) {if(a < b){a = b; return true;} else {return false;}};
const ll INF=(1LL<<60);
const ll mod=998244353;
using Graph = vector<vector<ll>>;
using Network = vector<vector<pair<ll,ll>>>;
using Grid = vector<string>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int dx2[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy2[8] = {1, 0, -1, 1, -1, 1, 0, -1};
void solve() {
ll N;cin>>N;
vector<ll> x(N);
rep(i, N) cin>>x[i];
Network g(N);
rep(i, N-1) {
ll u, v, w;cin>>u>>v>>w;
u--, v--;
g[u].push_back({v, w});
g[v].push_back({u, w});
}
vector<ll> dp(N, 0);
auto dfs = [&](auto&& self, ll v, ll p) -> ll {
ll res = x[v];
for (auto [u, w] : g[v]) {
if (u == p) continue;
res += self(self, u, v);
}
return dp[v] = res;
};
dfs(dfs, 0, -1);
ll ans = 0;
auto dfs2 = [&](auto&& self, ll v, ll p) -> void {
for (auto [u, w] : g[v]) {
if (u == p) continue;
ans += abs(dp[u]) * w;
self(self, u, v);
}
};
dfs2(dfs2, 0, -1);
out(ans);
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int t = 1;
while (t--) {
solve();
}
return 0;
}
Submission Info
Submission Time |
|
Task |
E - Pair Annihilation |
User |
tatesoto |
Language |
C++ 23 (gcc 12.2) |
Score |
425 |
Code Size |
2893 Byte |
Status |
AC |
Exec Time |
38 ms |
Memory |
15492 KiB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
425 / 425 |
Status |
|
|
Set Name |
Test Cases |
Sample |
00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt |
All |
00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 01_test_01.txt, 01_test_02.txt, 01_test_03.txt, 01_test_04.txt, 01_test_05.txt, 01_test_06.txt, 01_test_07.txt, 01_test_08.txt, 01_test_09.txt, 01_test_10.txt, 01_test_11.txt, 01_test_12.txt, 01_test_13.txt, 01_test_14.txt, 01_test_15.txt, 01_test_16.txt, 01_test_17.txt, 01_test_18.txt, 01_test_19.txt, 01_test_20.txt, 01_test_21.txt, 01_test_22.txt, 01_test_23.txt, 01_test_24.txt |
Case Name |
Status |
Exec Time |
Memory |
00_sample_01.txt |
AC |
8 ms |
3384 KiB |
00_sample_02.txt |
AC |
1 ms |
3412 KiB |
00_sample_03.txt |
AC |
1 ms |
3632 KiB |
01_test_01.txt |
AC |
11 ms |
5640 KiB |
01_test_02.txt |
AC |
36 ms |
11940 KiB |
01_test_03.txt |
AC |
5 ms |
4376 KiB |
01_test_04.txt |
AC |
35 ms |
12032 KiB |
01_test_05.txt |
AC |
32 ms |
11260 KiB |
01_test_06.txt |
AC |
35 ms |
12036 KiB |
01_test_07.txt |
AC |
23 ms |
8896 KiB |
01_test_08.txt |
AC |
38 ms |
11952 KiB |
01_test_09.txt |
AC |
32 ms |
11256 KiB |
01_test_10.txt |
AC |
36 ms |
12140 KiB |
01_test_11.txt |
AC |
36 ms |
12000 KiB |
01_test_12.txt |
AC |
36 ms |
11992 KiB |
01_test_13.txt |
AC |
15 ms |
7164 KiB |
01_test_14.txt |
AC |
37 ms |
12124 KiB |
01_test_15.txt |
AC |
18 ms |
7580 KiB |
01_test_16.txt |
AC |
38 ms |
12036 KiB |
01_test_17.txt |
AC |
11 ms |
5756 KiB |
01_test_18.txt |
AC |
37 ms |
12072 KiB |
01_test_19.txt |
AC |
25 ms |
9416 KiB |
01_test_20.txt |
AC |
37 ms |
12080 KiB |
01_test_21.txt |
AC |
26 ms |
15492 KiB |
01_test_22.txt |
AC |
25 ms |
15428 KiB |
01_test_23.txt |
AC |
21 ms |
11784 KiB |
01_test_24.txt |
AC |
20 ms |
11728 KiB |