/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 28ms 532.0 KiB
#3 Accepted 29ms 532.0 KiB
#4 Accepted 5ms 788.0 KiB
#5 Accepted 36ms 632.0 KiB
#6 Accepted 110ms 1.527 MiB
#7 Accepted 102ms 1.516 MiB
#8 Accepted 130ms 1.289 MiB
#9 Accepted 99ms 3.355 MiB
#10 Accepted 58ms 21.133 MiB
#11 Accepted 58ms 21.02 MiB
#12 Accepted 97ms 21.125 MiB
#13 Accepted 83ms 9.566 MiB
#14 Accepted 153ms 31.27 MiB
#15 Accepted 167ms 25.438 MiB
#16 Accepted 98ms 1.344 MiB

Code

#include <bits/stdc++.h>

// #ifndef ONLINE_JUDGE
// #include "template.cpp"
// #else
// #define debug(...)
// #define debugArr(...)
// #endif

#define F first
#define S second
#define int long long
#define double long double
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
#define mp(x, y) make_pair(x, y)
#define lb lower_bound
#define ub upper_bound
#define ins insert
#define INF (int)1e18
using namespace std;

using pii = pair<int, int>;
using pdd = pair<double, double>;
using pbb = pair<bool, bool>;
using pcc = pair<char, char>;
using pss = pair<string, string>;
using vi = vector<int>;
using vd = vector<double>;
using vb = vector<bool>;
using vc = vector<char>;
using vs = vector<string>;
using vpii = vector<pair<int, int>>;
using vpdd = vector<pair<double, double>>;
using vpbb = vector<pair<bool, bool>>;
using vpcc = vector<pair<char, char>>;
using vpss = vector<pair<string, string>>;
using vvi = vector<vector<int>>;
using vvd = vector<vector<double>>;
using vvb = vector<vector<bool>>;
using vvc = vector<vector<char>>;
using vvs = vector<vector<string>>;
using vvpii = vector<vector<pair<int, int>>>;
using vvpdd = vector<vector<pair<double, double>>>;
using vvpbb = vector<vector<pair<bool, bool>>>;
using vvpcc = vector<vector<pair<char, char>>>;
using vvpss = vector<vector<pair<string, string>>>;

// clang-format off
template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
template <class T> T sqrt_(T elem) {int l=1,r=elem;while(l<=r){int mid=l+(r-l)/2LL;if(mid>elem/mid){r=mid-1;}else{int val=mid*mid;if(val<=elem){l=mid+1;}else{r=mid-1;}}}return r;}
template <class T> T ceil_(T a,T b) {return(a+b-1)/b;};
template <class T> T mod_add(T a,T b,T mod){return((a%mod)+(b%mod))% mod;}
template <class T> T mod_sub(T a,T b,T mod){return((a%mod)-(b%mod)+mod)%mod;}
template <class T> T mod_mul(T a,T b,T mod){return((a%mod)*(b%mod))%mod;}
template <class T> T mod_inv(T a,T mod){T m0=mod,y=0,x=1;if(mod==1)return 0;while(a>1){T q=a/mod;T t=mod;mod=a%mod,a=t;t=y;y=x-q*y;x=t;}if(x<0)x+=m0;return x;}
template <class T> T mod_div(T a,T b,T mod){return mod_mul(a,mod_inv(b,mod),mod);}
// clang-format on

void solve() {
  int n;
  cin >> n;

  vi val(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> val[i];
  }

  vvpii adj(n + 1);

  for (int i = 1; i < n; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    adj[u].eb(v, w);
    adj[v].eb(u, w);
  }

  vi dp(n + 1, -INF);
  vb is_leaf(n + 1, false);
  int ans = -INF;

  auto dfs = [&](auto &&dfs, int node, int par) -> void {
    bool leaf = true;
    for (const auto &[c, w] : adj[node]) {
      if (c != par) {
        leaf = false;
        dfs(dfs, c, node);
      }
    }

    if (leaf) {
      is_leaf[node] = true;
      return;
    }

    int currMx = -INF, cnt = 0;

    for (const auto &[c, w] : adj[node]) {
      if (c != par) {
        cnt++;
        int v1 = dp[c] + val[node] - (w * 2LL);
        int v2 = val[node] + val[c] - (w * 2LL);
        dp[node] = max({dp[node], v1, v2});
        if (!is_leaf[c]) {
          currMx = max(currMx, dp[c] + val[node] - (w * 2LL));
        }
      }
    }

    if (currMx != -INF) {
      ans = max(ans, currMx);
    }

    if (cnt > 1) {
      vi temp;
      for (const auto &[c, w] : adj[node]) {
        if (c != par) {
          temp.eb(max(val[c], dp[c]) - (w * 2LL));
        }
      }
      sort(rall(temp));
      ans = max(ans, temp[0] + temp[1] + val[node]);
    }
  };

  dfs(dfs, 1, 0);
  cout << ans << '\n';
  return;
}

signed main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

  int tt = 1, count = 1;
  cin >> tt;
  while (tt--) {
    // cout << "Case #" << count << ": ";
    solve();
    ++count;
  }
  return 0;
}

Information

Submit By
Type
Submission
Problem
P1205 E. Make Cycle in Tree
Language
C++17 (G++ 13.2.0)
Submit At
2025-07-15 19:24:31
Judged At
2025-07-15 19:24:31
Judged By
Score
100
Total Time
167ms
Peak Memory
31.27 MiB