Training for the World Finals P51803


Statement
 

pdf   zip

The UPC team 12 seconds could barely train for the World Finals. For instance, sometimes only Ferran and Ángel could join for a training. In those cases, a possible strategy was to make Ferran think about how to solve the problems, and to make Ángel program the solutions previously found and written down on paper by Ferran.

Supose that a training had nn problems. Let tit_i be the time that Ferran needed to solve the ii-th problem, and pip_i be the time that Ángel needed to program the ii-th problem. Both Ferran and Ángel could only perform one task at a time, but could decide the order of thinking and the order of programming, with just one restriction: Ferran had to completely solve a problem before Ángel could start programming its solution.

Given all this information, can you please minimize the total time to solve and program all the problems?

Input

Input consists of several cases, each one with nn followed by t1,,tnt_1, \dots, t_n, followed by p1,,pnp_1, \dots, p_n. Assume 1n1051 \le n \le 10^5, and that all tit_i and pip_i are between 1 and 10910^9.

Output

For every case, print the minimum time to solve and program all the problems.

Public test cases
  • Input

    2
    80 90
    100 100
    
    4
    4 4 4 4
    1 2 4 6
    
    5
    5 1 2 3 6
    3 1 3 5 5
    
    3
    1000000000 1000000000 1000000000
    1000000000 1000000000 1000000000
    

    Output

    280
    17
    20
    4000000000
    
  • Information
    Author
    Ferran Alet and Ángel García
    Language
    English
    Official solutions
    C++
    User solutions
    C++