QOJ.ac

QOJ

実行時間制限: 2 s メモリ制限: 1024 MB 満点: 150

#4090. Monkey

統計

Two pillars, A and B, are placed side by side. Each pillar has $N$ handles, numbered from 1 to $N$ from bottom to top. Each handle has zero or more bananas hanging from it. $A_i$ represents the number of bananas hanging from handle $i$ of pillar A, and $B_j$ represents the number of bananas hanging from handle $j$ of pillar B. These values are integers between 0 and $10^9$ inclusive.

A monkey can use its two arms to hold handles on different pillars. Note that the monkey cannot hold two handles on the same pillar. Furthermore, the monkey cannot hold just any pair of handles. A pair of handles that the monkey can hold can be represented as $(x, y)$, which means the monkey can simultaneously hold handle $x$ of pillar A and handle $y$ of pillar B. When doing so, the monkey can eat all the bananas remaining on both handles. Naturally, once eaten, the bananas disappear. There are a total of $M$ such pairs.

Initially, the monkey starts at one of the possible pairs of handles it can hold. When the monkey is currently at position $(x, y)$, it can move to another pair of handles $(x', y')$ if and only if $x < x'$ and $y = y'$, or $x = x'$ and $y < y'$.

Naturally, the monkey wants to eat as many bananas as possible. Given the information about the handles that can be held and the number of bananas hanging from them, write a program to find the maximum number of bananas the monkey can eat.

Implementation Details

You must implement the following function:

long long int max_bananas(vector<int> A, vector<int> B, vector< pair<int, int> > P)
  • This function is called exactly once.
  • The length of $A$ is $N$, and $A[i]$ is the number of bananas hanging from the $(i+1)$-th handle of pillar A ($0 \le i \le N-1$).
  • The length of $B$ is $N$, and $B[i]$ is the number of bananas hanging from the $(i+1)$-th handle of pillar B ($0 \le i \le N-1$).
  • The length of $P$ is $M$. If $(x, y)$ is included in $P$, the monkey can simultaneously hold handle $x$ of pillar A and handle $y$ of pillar B. At this time, it can eat all the bananas remaining on both handles. It is guaranteed that the same pair is not given multiple times.
  • This function must calculate and return the maximum number of bananas the monkey can eat based on the input information.

You must not execute any input/output functions in any part of your submitted source code.

Constraints

  • $1 \le N \le 500\,000$
  • $1 \le M \le 500\,000$
  • $M \le N^2$
  • $0 \le A_i \le 10^9$ ($1 \le i \le N$)
  • $0 \le B_i \le 10^9$ ($1 \le i \le N$)
  • The pairs $(x, y)$ given in argument $P$ are all distinct, and satisfy $1 \le x \le N, 1 \le y \le N$.

Subtasks

  1. (11 points)

    • $M \le 16$
  2. (42 points)

    • $M \le 5\,000$
  3. (97 points)

    • No additional constraints.

Scoring

The data is considered correct only if the number of bananas returned by the max_bananas function matches the correct answer.

Note that the score for each subtask is the minimum score among all data for that subtask.

Examples

Input 1

3 3
2 3 1
3 2 4
1 1
2 1
1 3

Output 1

9

Note

Starting at position $(1, 1)$ and moving to position $(1, 3)$, one can eat a total of $2 + 3 + 4 = 9$ bananas, and there is no way to eat more bananas than this. Therefore, the max_bananas function must return 9. This example satisfies the conditions of all subtasks.

Sample Grader

The sample grader receives input in the following format:

N M
A[0] A[1] ... A[N-1]
B[0] B[1] ... B[N-1]
P[0].first P[0].second
...
P[M-1].first P[M-1].second

The sample grader outputs in the following format:

(result of max_bananas)

Note that the sample grader may differ from the grader used in actual evaluation.

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.