QOJ.ac

QOJ

Time Limit: 3 s Memory Limit: 1024 MB Total points: 100

#6174. Optimal Profit Problem

Statistics

The task scheduling problem with continuous time periods and varying profits can be described as follows:

  1. Given a set $A$ of $n$ tasks.
  2. Tasks completed in the morning must be performed in the order of the given sequence $x_1, x_2, \dots, x_n$; tasks completed in the afternoon must be performed in the order of the given sequence $y_1, y_2, \dots, y_m$.
  3. The profit obtained from completing a task varies depending on the time period. Tasks in the morning sequence $x_i$ yield a corresponding profit $a_i$, while tasks in the afternoon sequence $y_i$ yield a corresponding profit $b_i$.
  4. Each task can be completed either in the morning or in the afternoon, but at most once.

Let $1 \leq l_a \leq r_a \leq n$ and $1 \leq l_b \leq r_b \leq m$, where $[l_a, r_a]$ and $[l_b, r_b]$ are the continuous task intervals completed in the morning and afternoon, respectively. Two continuous task intervals are said to be non-overlapping if $\{x_{l_a}, x_{l_a+1}, \dots, x_{r_a}\} \cap \{y_{l_b}, y_{l_b+1}, \dots, y_{r_b}\} = \varnothing$, meaning that all tasks in these two intervals are distinct. In this case, the total profit obtained from these two intervals is:

$$\sum_{i=l_a}^{r_a} a_i + \sum_{i=l_b}^{r_b} b_i$$

Optimal Profit Problem: For the given morning and afternoon task sequences, calculate the maximum profit that can be obtained from all pairs of non-overlapping continuous task intervals. The continuous task intervals for the morning and afternoon can both be empty, in which case there are no tasks in the corresponding interval.

Input

The first line contains two positive integers $n$ and $m$, representing the lengths of the morning and afternoon task sequences, respectively.

The second line contains $n$ integers, representing the morning task sequence $x_1, x_2, \dots, x_n$.

The third line contains $n$ integers, representing the profits corresponding to the morning task sequence $a_1, a_2, \dots, a_n$.

The fourth line contains $m$ integers, representing the afternoon task sequence $y_1, y_2, \dots, y_m$.

The fifth line contains $m$ integers, representing the profits corresponding to the afternoon task sequence $b_1, b_2, \dots, b_m$.

Output

Output a single integer representing the answer.

Examples

Input 1

8 5
12 10 2 1 6 7 4 8
1 9 2 8 2 10 9 10
6 9 7 8 3
7 7 8 3 3

Output 1

58

Subtasks

For $40\%$ of the test data, $n, m \leq 10\,000$.

For $100\%$ of the test data, $1 \leq n, m \leq 500\,000$, $1 \leq a_i, b_i \leq 10^9$, and $1 \leq x_i, y_i \leq n+m$.

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.