The K512 Cup Begins!
InterviewTime limit1sMemory limit512 MB
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, of the first kind and of the second kind. Participants must use up all of the given scrolls in a suitable order to change their luck value.
- Each of the first-kind scrolls has a number (). Using a scroll adds to the current luck value , so the luck value becomes .
- Each of the second-kind scrolls has a number (). Using a scroll multiplies the current luck value by , so the luck value becomes .
Use all the given scrolls in a suitable order to maximize your luck value.
Input
The first line contains two integers and , separated by a space ().
The second line contains integers , separated by spaces ().
The third line contains integers , separated by spaces ().
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.