Eating chocolate

Pau has a chocolate bar with n × m (1 ≤ n, m ≤ 10⁵) pieces and he wants
to eat it.

In order to do so, Pau decides to apply the following procedure: he
selects either a row or a column with at least one remaining piece and
then he eats all the pieces that were left.

Each row and each column has a pleasure accumulator associated,
indicating the number of pleasure units that Pau will obtain for each
piece he eats if he selected that row or column. Considering all of
this, compute the maximum amount of pleasure that Pau can achieve.

Input

The input contains several test cases. Each case begins with two
integers n and m. A line follows containing n numbers with the pleasure
accumulators of the n rows, then another line with m numbers with the
pleasure accumulators of the m columns. Every pleasure accumulator is an
integer number between 1 and 10⁴.

Output

For each case, print the maximum pleasure that can be achieved.

Observation

Notice that the final result is too big to store in an int variable in
C++. Use long long.

Problem information

Author: Unknown
Translator: Alex Alvarez

Generation: 2026-01-25T13:52:11.603Z

© Jutge.org, 2006–2026.
https://jutge.org
