AlgoUniversity
  • Go Back
Discussion
Dice Sum :

Author

Akash

Difficulty Level : Medium

Submissions : 810

Asked In : American-Express

Marks :10

: 12 | : 1

You have a traditional $$$6$$$-faced dice, and you rolled it $$$M+N$$$ times. The numbers from rolls of $$$M$$$ times are in $$$A$$$, and the numbers from rolls of $$$N$$$ times are in $$$B$$$. You want each array to have a common sum. You can modify the arrays so that their sums are equal, but you should use the minimum number of turns. Through a turn, you can change a value from an array into any value between $$$1$$$ and $$$6$$$. You cannot delete a value or make it $$$0$$$ or negative.

$$$A$$$'s length and $$$B$$$'s length can be different. If it is impossible to make both arrays sum to a common sum, return $$$-1.$$$

Input

The first line contains two integers, $$$n$$$ $$$(1 \le n \le 10^5)$$$ and $$$m$$$ $$$(1 \le m \le 10^5)$$$.

The second line contains $$$n$$$ integers, $$$A_1,A_2,...A_n$$$ $$$(1 \le A_i \le 6)$$$.

The third line contains $$$m$$$ integers, $$$B_1,B_2,...B_m$$$ $$$(1 \le B_i \le 6)$$$.

Output

Return the minimum number of turns required to make the sums equal and $$$-1$$$ if it's impossible.

Example

Input
3 3
1 4 3
6 6 6
Output
2

You need to login to view your submissions.

You need to login to view all submissions.

Loading...

Result : Executed

Loading...

Feel something is wrong with the test cases?

Result : Accepted

Test Cases :

You need to Log In
We're glad that you want to attempt this problem!

But to Run or Submit the Problem, you need to Log In.

Continue to Log In
Challenge Submitted!

Your challenge has been submitted successfully.

You will get a response soon via WhatsApp or Email.

Challenge
Facing issue while trying to solve the problem! Don't worry, we got you covered!

Do let us know your issue.

Looks good!
Please enter your issue / feedback.

How do we get in touch with you?
Looks good!
Please enter your phone no.
Looks good!
Please enter your email address.