cswork / OAOpen practice workspace

Abaka.aiOA #2

Maximize the Power

Estimated MediumJudge-ready

Topics (AI-labelled)GreedyPrefix & Ranges

The power array has even length. In each operation, pick any two of its position values x and y, gain the sum of the closed interval arr[min(x,y)..max(x,y)], and then remove these two elements from power. Maximize the total gain after all operations, modulo 10^9+7.

Input

The input contains N and K, then N non-negative arr values, then K positions. This site adds 1≤N≤200000, 2≤K≤2000 with K even, every position in [0,N−1], and arr[i]≤10^9.

Output

Output the maximum total gain modulo 10^9+7.

Example 1

Input:

5 4
3 5 6 0 7
3 1 0 2

Output:

25

Example 2

Input:

3 2
1 2 3
0 2

Output:

6

Example 3

Input:

2 2
0 0
1 0

Output:

0

Editorial

Loading solution…