Under construction

Still being built. Things may change

Card Factory (Small)

Time limit1sMemory limit256 MB

Summary
One card is flipped a number of times; report the side showing after all flip orders, where the visible side never changes.
Level

Easy2 of 10

Topics
Implementation, Math
Solved
No attempts yet

Statement

Jinseo is a worker at the CTP card factory. The factory has N cards, and each card has a number written on its front and back. Jinseo flips cards on the orders of the factory manager Nojin. There are M orders, and each order goes as follows.

"When factory manager Nojin says a number K, Jinseo flips every one of the N cards whose visible side is at most K."

After all the manager's orders are given, Jinseo must report the sum of the numbers on the visible sides of the cards to the manager.

For example, see the figure below.

All cards start with their front side visible, and the numbers on the cards are positive integers at most 10,000.

Input

The first line contains N and M. (N = 1, M is a positive integer at most 100)

The next N lines contain the front side Ai and the back side Bi of each card. (Ai and Bi are positive integers at most 10,000)

The next M lines contain the number K that the manager says. (K is a positive integer at most 10,000)

Output

Print the sum of the visible card values after all orders are given.

Examples1

  1. Example 1

    Input
    1 3
    2 3
    1
    1
    1
    
    Expected output
    2