#P15825. [JOI Open 2014] Fortune Telling 2

[JOI Open 2014] Fortune Telling 2

Description

Prof. K is the President of the Japanese Committee for the International Olympiad in Informatics. He loves fortune-telling. He is always doing several kinds of fortune-tellings. Today, he decided to do a fortune-telling using cards to see the results of the Japanese delegation of this year’s IOI.

An integer is written on each side of each card. Integers on both sides of a card are not necessarily equal. If you put a card on a table so that you can see the integer on one side, you cannot see the integer on the other side.

The fortune-telling is done as follows.

  • First, Prof. K will put NN cards on a table. The cards are numbered from 1 to NN. The integer AiA_i is written on one side of the card ii, and the integer BiB_i is written on the other side of the card ii. He will put the cards on the table so that AiA_i is shown on the card ii for each ii.
  • For j=1,,Kj = 1, \dots, K, he will do the following operation: “If the integer shown on each card is less than or equal to TjT_j, he will turn it upside down.”
  • The result of the fortune-telling is the sum of the integers shown on the cards on the table after these KK operations are finished.

At some point, Prof. K realized that deciding which cards are to be turned upside down is a boring job. He finally gave up doing the fortune-telling by using the cards. He only wants to know the sum of the integers shown on the cards on the table after these KK operations are finished.

Task

Write a program which, given the integers written on the cards and the information of the operations, calculates the sum of the integers shown on the cards on the table after all operations are finished.

Input Format

Read the following data from the standard input.

  • The first line contains two space separated integers NN and KK. This means there are NN cards and the number of operations is KK.
  • The ii-th line (1iN1 \le i \le N) of the following NN lines contains two space separated integers AiA_i and BiB_i. This means the integers written on the card ii are AiA_i and BiB_i.
  • The jj-th line (1jK1 \le j \le K) of the following KK lines contains an integer TjT_j. This means, in the jj-th operation, if the integer shown on a card is less than or equal to TjT_j, it will be turned upside down.

Output Format

To the standard output, write the sum of the integers shown on the cards on the table after KK operations are finished.

5 3
4 6
9 1
8 8
4 2
3 7
8
2
9
18

Hint

Sample 1 Explanation

First, the five integers shown on the cards are 4, 9, 8, 4, 3, respectively. The operations are done as follows.

  • If the integer shown on each card is less than or equal to 8, it will be turned upside down. After the operation, the integers shown on the cards are 6, 9, 8, 2, 7, respectively.

  • If the integer shown on each card is less than or equal to 2, it will be turned upside down. After the operation, the integers shown on the cards are 6, 9, 8, 4, 7, respectively.

  • If the integer shown on each card is less than or equal to 9, it will be turned upside down. After the operation, the integers shown on the cards are 4, 1, 8, 2, 3, respectively.

After all operations are finished, the sum of the integers shown on the cards on the table is 4+1+8+2+3=184 + 1 + 8 + 2 + 3 = 18.

Constraints

All input data satisfy the following conditions.

  • 1N2000001 \le N \le 200000.
  • 1K2000001 \le K \le 200000.
  • 1Ai10000000001 \le A_i \le 1000000000 (1iN1 \le i \le N).
  • 1Bi10000000001 \le B_i \le 1000000000 (1iN1 \le i \le N).
  • 1Tj10000000001 \le T_j \le 1000000000 (1jK1 \le j \le K).

Subtask

Subtask 1 [4 points]

The following conditions are satisfied.

  • N1000N \le 1000.
  • K1000K \le 1000.

Subtask 2 [31 points]

The following conditions are satisfied.

  • N40000N \le 40000.
  • K40000K \le 40000.

Subtask 3 [65 points]

There are no additional constraints.