Under construction

Still being built. Things may change

Invisible

Time limit10sMemory limit512 MB

Summary
Two players take turns placing cards from their own decks onto a shared stack, and the task is the optimal score difference under the pass rules.
Level

Medium7 of 10

Topics
Game theory, Dynamic programming, Simulation
Solved
No attempts yet

Problem

Your friend wants to play a card game called "Invisible". The game uses two kinds of cards: scoring cards and interference cards. Each scoring card has a positive value written on it. The rules are as follows.

  • The game is played by two players, Player 1 and Player 2. The game starts with Player 1's turn.
  • The table has one stack and two decks. The stack is made of the cards that both players have placed. Each player's deck consists of that player's scoring cards and interference cards. A player can check the order of the cards in their own deck or the opponent's deck at any time. At the start of the game, the stack is empty.
  • The players take turns. On each turn, a player does exactly one of the following two actions.
    • Place the top card of their own deck on top of the stack. This action cannot be taken if their own deck has no cards.
    • Pass their turn.
  • When a player passes, the following processing happens.
    • Each player takes every scoring card in the stack that meets both of these conditions. The taken scoring cards are removed from the table.
      1. The scoring card was placed on the stack by that player.
      2. The card is above every interference card placed by the opponent. (If the stack has no interference card from the opponent, the player takes all scoring cards they placed on the stack.)
    • All cards are removed from the stack.

If both players pass in a row while the stack is empty, the game ends. Each player's final score is the sum of the numbers on the scoring cards that player took.

Each player plays optimally to maximize their own score minus the opponent's score. For the given decks, compute the difference between Player 1's score and Player 2's score when both players play optimally.

Input

The input consists of a single test case in the following format.

n m
a_1 a_2 ... a_n
b_1 b_2 ... b_m

The first line contains the positive integers nn and mm (1≤n,m≤501 \le n, m \le 50), the sizes of the decks. The second line contains a1,a2,…,ana_1, a_2, \dots, a_n, the cards of Player 1's deck from top to bottom. The third line contains b1,b2,…,bmb_1, b_2, \dots, b_m, the cards of Player 2's deck from top to bottom. Each value is either an integer from 1 to 1,000,000 inclusive or -1. A positive value is a scoring card, and -1 is an interference card.

Output

Print the difference (Player 1's score) minus (Player 2's score) when both players play optimally.

Examples3

  1. Example 1

    Input
    2 2
    100 -1
    200 300
    
    Expected output
    -100
    
  2. Example 2

    Input
    3 5
    10 30 -1
    -1 90 20 10 -1
    
    Expected output
    0
    
  3. Example 3

    Input
    4 5
    15 20 10 30
    50 30 10 20 25
    
    Expected output
    -60