QOJ.ac

QOJ

実行時間制限: 3 s メモリ制限: 1024 MB 満点: 100 ハック可能 ✓

#18340. Tree Kadane

統計

You are given a tree $T$ with $N$ vertices numbered $1$ to $N$. Each vertex $i$ has an integer weight $A_i$.

A connected subset $S$ of $T$ is a nonempty subset of vertices in $T$ such that, for any two vertices $a$, $b$ in $S$, there exists a path between $a$, $b$ in $T$ that only uses the vertices from $S$.

You need to perform the following $Q$ queries:

  • $k$ $x$ : change $A_k$ to $x$ and print the maximum value of $\sum_{i \in S}{A_i}$ where $S$ is a connected subset of $T$.

Input

The first line contains an integer $N$ --- the number of vertices.

The second line contains $N$ integers $A_1, A_2, \dots, A_N$ separated by a space --- the weights of each vertex.

The $i$-th of the next $N - 1$ lines contains two integers $u_i$ and $v_i$ separated by a space --- the endpoints of the $i$-th edge.

The next line contains an integer $Q$ --- the number of queries.

Each of the next $Q$ lines contains two integers $k$ and $x$ separated by a space --- arguments of each query.

Output

Print $Q$ lines. In each line, print the answer to each query.

Scoring

  • $1 \leq N \leq 10^5$
  • $-10^9 \leq A_i \leq 10^9$ ($1 \leq i \leq N$)
  • $1 \leq u_i, v_i \leq N$ ($1 \leq i \leq N-1$)
  • The given graph is a tree.
  • $1 \leq Q \leq 10^5$
  • $1 \leq k \leq N$ for each query
  • $-10^9 \leq x \leq 10^9$ for each query

  • Subtask 1 (10 points): $N, Q \leq 10^3$

  • Subtask 2 (20 points): Each vertex has a degree less than $3$.
  • Subtask 3 (70 points): No additional constraints.

Examples

Input 1

5
3 -1 -4 -1 -5
1 2
1 3
3 4
3 5
5
1 -2
3 2
4 1
2 5
1 -9

Output 1

-1
2
3
6
5

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.