Given N cards, on both sides of each card is written one number. If you put a card on the table face up, you can not see the number written on the other side.
There are N cards on the table, let Ai be the number written on one side of the card i, a Bi be the number written on the other side of the map i. Cards are initially in a position that can be seen the number Ai.
Given K operations, for each j operation from 1 to K do the following: all cards with visible number less or equal to Tj are upended.
The result is the sum of the numbers that are visible on the cards after this operations.
1<=Ai,Bi,Tj<=1 000 000 000
Input: 5 3 // N and K 4 6 // each N lines contains the numbers on the sides of the cards, Ai and Bi 9 1 8 8 4 2 3 7 8 // each K lines contain Tj 2 9 Output: 18