Under construction

Still being built. Things may change

The K512 Cup Begins!

Interview

Time limit1sMemory limit512 MB

Summary
Starting from 0, use all N add-scrolls and M multiply-scrolls in some order to find the maximum final luck value.
Level

Medium5 of 10

Topics
Dynamic programming, Bit manipulation
Solved
No attempts yet

Problem

Since 2024, the Sogang ICPC Team has held the K512 Cup every year to give its members contest experience and to build friendship among them.

Luck is one of the important factors in a contest. Every participant starts with a luck value of 0. Geunsu gives each participant two kinds of scrolls, NN of the first kind and MM of the second kind. Participants must use up all of the given scrolls in a suitable order to change their luck value.

  1. Each of the NN first-kind scrolls has a number aia_i (1≤i≤N1 \le i \le N). Using a scroll adds aia_i to the current luck value PP, so the luck value becomes P+aiP + a_i.
  2. Each of the MM second-kind scrolls has a number bib_i (1≤i≤M1 \le i \le M). Using a scroll multiplies the current luck value PP by bib_i, so the luck value becomes P×biP \times b_i.

Use all the given scrolls in a suitable order to maximize your luck value.

Input

The first line contains two integers NN and MM, separated by a space (1≤N,M≤121 \le N, M \le 12).

The second line contains NN integers a1,…,aNa_1, \ldots, a_N, separated by spaces (0≤ai≤100 \le a_i \le 10).

The third line contains MM integers b1,…,bMb_1, \ldots, b_M, separated by spaces (0≤bi≤100 \le b_i \le 10).

Output

Print the maximum luck value that can be obtained with the given scrolls.

Hint

Watch out for integer overflow when handling large numbers. Use a 64-bit integer type: long long in C and C++ (with %lld for scanf and printf), or long in Java and Kotlin.

Examples3

  1. Example 1

    Input
    2 2
    1 3
    2 5
    
    Expected output
    40
    
  2. Example 2

    Input
    3 4
    5 1 2
    2 0 2 4
    
    Expected output
    128
    
  3. Example 3

    Input
    1 10
    1
    10 10 10 10 10 10 10 10 10 10
    
    Expected output
    10000000000