Picky children
The nanny is trying to put the children to sleep, but she can't rest herself: as soon as she gets some to bed, others wake up...
The nanny has N children, each with unique sleep patterns. She knows how many minutes it takes to put the k-th child to sleep (denoted as a_k), and how many minutes the child will stay asleep afterward (denoted as b_k). The nanny can only rest when all the children are asleep simultaneously.
Help the nanny determine the optimal order to put the children to bed so that she can maximize her continuous sleep time.
For instance, consider there are 2 children: a_1=1, b_1=10, a_2=10, b_2=20. If she starts by putting the first child to bed, it will take her 10 minutes to put the second one to sleep, during which the first will wake up. However, if she starts with the second child, she can then put the first to bed and enjoy a full 10 minutes of rest.
Input
The first line contains N (1 ≤ N ≤ 100000). The second line contains natural numbers a_1 … a_n, and the third line contains b_1 … b_n. The numbers do not exceed 10^9 and are separated by spaces.
Output
The output should be a single number T, representing the maximum possible sleep time for the nanny, or 0 if she cannot get any sleep at all.