A few years after Trivial Pursuit became a hit, Canadian authors Pierre Berton and Charles Templeton created a rival trivia game called Tour De Force. Unfortunately, it never caught on because it wasn't a very good game.
Like Trivial Pursuit, Tour de Force has question cards but there are only two questions per card and each is worth a different number of points. On your turn, another player asks you the first question on your first card. If you get it right, you get the points and you have the option of trying the second follow-up question on the card (usually on the same subject). If you get that right, you get those points as well, and you have the option of taking another card and continuing. If at any point you get a question wrong, your turn is over, the card you are working on is discarded, and you lose a point. There is no penalty if you stop voluntarily. If you manage to answer
For example, suppose the first card has questions worth
A smart Tour De Force player quits when they're ahead to avoid losing a point for the last question. But Bert is not a smart player. He always goes for a Tour De Force on every turn even though he has never once made it. Your task is to figure out the maximum possible score Bert can get (without scoring a Tour De Force) with any given set of cards.
For example, suppose there are
The input consists of
Sample Input
10
7 1
4 1
3 8
4 6
2 2
4 3
4 5
10 6
8 6
7 2
6
4 4
7 5
2 4
8 1
9 3
5 7
9
2 6
2 8
3 1
6 3
7 4
2 6
1 10
9 9
7 1
8
2 9
1 6
10 3
9 6
6 2
9 9
2 5
4 1
7
4 8
7 1
1 1
3 3
7 3
6 3
3 8
8
6 8
1 2
10 4
7 7
10 8
5 8
8 5
3 10
6
4 9
4 5
8 4
9 3
8 10
10 10
9
6 2
9 9
5 4
10 8
3 7
4 1
2 1
8 6
4 8
8
1 4
10 1
1 10
7 2
8 5
3 3
6 6
5 10
6
1 8
7 6
9 10
1 3
10 7
7 8
Sample Output
87
57
82
81
56
94
80
92
79
73
Educational Computing Organization of Ontario - statements, test data and other materials can be found at ecoocs.org
Comments