## Christmas Cards

View as PDF

Points: 15 (partial)
Time limit: 2.0s
Memory limit: 256M

Authors:
Problem type

For Christmas, Firebat received a new Hearthstone (a card game) expansion! In this game there is a deck of cards. Unfortunately for him, this expansion only contained draw cards. Each card has a mana cost and draw cards from the top of the deck. Still the optimist, Firebat created a deck out of only these draw cards. We all know Firebat is the ultimate hearthstone player, and thus, knows exactly the order of the cards in his deck, does not have a hand limit and can use more than 10 mana. Nevertheless, he would like to maximize value. He starts off the game with only the first card of his deck in his hand (it's only fair for his opponent). Help him by determining the minimum mana he needs to spend in order to draw his entire deck (and possible overdraw).

It will always be possible to draw all cards. Once Firebat plays a card, he cannot play it again.

#### Input Specification

First line: , the number of cards in his deck.
Next lines: the and values of the card of Firebat's deck (from top to bottom). .

#### Output Specification

A single integer, the minimum mana he must spend to draw his entire deck.

#### Sample Input

8
20 3
5 0
13 3
9 3
18 2
4 1
17 1
8 0

#### Sample Output

33

#### Explanation

For the minimal cost, Firebat should play the first card, drawing the next three. He should then play the fourth card and then the sixth card, emptying the deck. This gives a total of mana.

• imaxblue  commented on Aug. 14, 2018, 1:08 p.m.

I don't see why he doesn't just play Myra's Unstable Element

• Natsuki  commented on Jan. 20, 2019, 1:09 p.m.

This comment is hidden due to too much negative feedback. Click here to view it.

• Ken_Shi  commented on Jan. 1, 2017, 7:07 p.m.

When my favorite game connects to contest, only sadness left...

• alei  commented on Jan. 1, 2017, 11:28 a.m.

is possible to play the same card multiple times?

• FatalEagle  commented on Jan. 1, 2017, 11:28 a.m.

No, each card can only be played once.

• grikukan  commented on Jan. 1, 2017, 9:36 a.m.

What should I output if it's impossible to draw all cards?

• imaxblue  commented on Jan. 1, 2017, 9:44 a.m.

It is guaranteed to be possible. This has been updated in the problem statement. Sorry for any inconvenience.