No.54 Happy Hallowe'en
タグ : / 解いたユーザー数 111
作問者 :
Problem Statement
Today is Halloween, so Taro has decided to visit the houses in his neighborhood to get candy.
There are \(N\) houses in the neighborhood other than Taro's own.
Visiting house \(i\) gives him \(V_i\) pieces of candy, but
in order to distribute candy fairly among the neighborhood children,
if he already has \(T_i\) or more pieces of candy, he receives no candy at all.
Taro initially has no candy and can visit the neighborhood houses in any order he likes.
Find the maximum number of pieces of candy Taro can receive.
Each house can be visited only $1$ time.
Input
\(N\) \(V_1\ T_1\) \(V_2\ T_2\) \(\dots\) \(V_i\ T_i\) \(\dots\) \(V_N\ T_N\)
Line \(1\) contains an integer \(N\ (1 \leq N \leq 10000)\), the number of houses in the neighborhood.
Each of the following \(N\) lines contains, separated by a space, an integer \(V_i\ (1 \leq V_i \leq 10000)\), the number of pieces of candy received at house \(i\) \((1 \leq i \leq N)\), and
an integer \(T_i\ (1 \leq T_i \leq 10000)\), the threshold for receiving candy.
Output
Output the maximum number of pieces of candy Taro can receive.
Print a newline at the end.
Sample
Sample 1
Input
3 1 1 3 2 5 3
Output
6
If he visits house \(1\), he has \(0\) pieces of candy, so he receives \(1\) piece.
If he then visits house \(2\), he has \(1\) piece of candy, so he would receive \(3\) pieces, but
visiting house \(3\) instead gives him \(5\) pieces, so he can receive at most \(1+5=6\) pieces.
Sample 2
Input
5 1 1 1 4 1 2 1 5 1 3
Output
5
By visiting all the houses in an appropriate order, he can receive at most \(5\) pieces of candy.
Sample 3
Input
7 2 1 2 3 9 2 3 1 6 4 3 5 4 8
Output
11
He visits houses \(1,2,6,7\) in this order, receiving \(2,2,3,4\) pieces of candy, respectively.
Sample 4
Input
2 1 3 100 2
Output
101
Provided by sugim.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。