/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 35ms 17.203 MiB
#2 Wrong Answer 150ms 21.484 MiB
#3 Wrong Answer 137ms 20.742 MiB

Code

# ruff: noqa: E731, E741
import math
import sys

read = sys.stdin.readline
input = lambda: read().rstrip()
ir = lambda: int(read())
rir = lambda: range(int(read()))
mir = lambda: map(int, read().split())
lmir = lambda: list(map(int, read().split()))


def solve():
    n = ir()
    values = lmir()
    tree = [dict() for _ in range(n)]
    for _ in range(n - 1):
        u, v, w = mir()
        tree[u - 1][v - 1] = w
        tree[v - 1][u - 1] = w

    parents = [None] * n
    depth = [-1] * n
    depth[0] = 0
    q = [0]
    for i in q:
        for j in tree[i]:
            if depth[j] == -1:
                parents[j] = i
                depth[j] = depth[i] + 1
                q.append(j)

    branch_scores = [0] * n
    full_scores = [-math.inf] * n
    for i in reversed(q):
        best_branch_score = None
        child_scores = []
        for j, w in tree[i].items():
            if j == parents[i]:
                continue
            branch_score = branch_scores[j] + values[i] - 2 * w
            best_branch_score = (
                max(best_branch_score, branch_score)
                if best_branch_score
                else branch_score
            )
            child_scores.append(branch_scores[j] - 2 * w)
        if best_branch_score is not None:
            full_scores[i] = best_branch_score
        if len(child_scores) >= 2:
            child_scores.sort()
            full_scores[i] = max(
                full_scores[i], values[i] + child_scores[-2] + child_scores[-1]
            )
        branch_scores[i] = (
            values[i]
            if best_branch_score is None
            else max(best_branch_score, values[i])
        )

    # print(branch_scores)
    # print(full_scores)
    print(max(full_scores))
    # print(values)


def main():
    for _ in rir():
        solve()


def test():
    pass


if __name__ == "__main__":
    if sys.stdin.isatty():
        test()
    else:
        main()

Information

Submit By
Type
Submission
Problem
P1205 E. Make Cycle in Tree
Contest
Educational Round 1
Language
PyPy 3 (Python 3.9.18 PyPy 7.3.15)
Submit At
2025-07-14 17:35:41
Judged At
2025-07-14 17:35:41
Judged By
Score
0
Total Time
150ms
Peak Memory
21.484 MiB