Under construction

Still being built. Things may change

Stone Placement Game

Time limit1sMemory limit1024 MB

Summary
On a circle of valued cells, find each player's optimal score when they alternately place stones next to their own stones.
Level

Medium7 of 10

Topics
Greedy, Sorting, Array
Solved
No attempts yet

Problem

A game board has NN cells arranged in a circle, and Cheolsu and Younghee play a game on it. The ii-th cell has a non-negative integer xix_{i} written on it. Each cell can hold at most one stone.

Cheolsu has NN black stones, and Younghee has NN white stones. First, the two players place stones on some predetermined cells. After that, Cheolsu starts, and they take turns. On a turn, a player looks at the empty cells adjacent to the cells holding their own stones. If there is at least one such cell, the player picks one and places a stone there. If there is none, the turn passes to the opponent. If neither player can place a stone, the game ends.

Each player wants to maximize the sum of the values of the cells holding their own stones. Given the stone placement before the turns begin, find each player's score when both play optimally.

Input

The first line contains NN (3≤N≤200 0003\leq N\leq 200\,000).

The second line contains c1,⋯ ,cNc_{1}, \cdots, c_{N}, separated by spaces, which describe the initial stone placement (0≤ci≤20\leq c_{i}\leq 2). ci=1c_{i}=1 means cell ii holds a black stone, ci=2c_{i}=2 means it holds a white stone, and ci=0c_{i}=0 means it is empty.

The third line contains x1,⋯ ,xNx_{1}, \cdots, x_{N}, separated by spaces (0≤xi≤1090\leq x_{i}\leq 10^{9}).

Output

Print the scores of Cheolsu and Younghee, separated by a space, on the first line.

Examples4

  1. Example 1

    Input
    9
    0 1 0 2 0 0 2 0 0
    1 2 3 4 5 6 7 8 9
    
    Expected output
    12 33
    
  2. Example 2

    Input
    4
    0 0 0 0
    6 9 8 8
    
    Expected output
    0 0
    
  3. Example 3

    Input
    8
    1 0 0 0 0 0 0 1
    2 9 4 8 1 8 5 0
    
    Expected output
    37 0
    
  4. Example 4

    Input
    36
    1 1 0 0 2 2 0 1 0 0 0 0 2 0 0 0 1 0 0 0 1 0 0 2 0 0 0 2 0 0 0 1 0 0 0 1
    18 23 18 20 40 30 19 15 13 11 19 21 12 25 43 37 23 21 10 4 9 7 3 60 54 32 18 39 42 55 71 92 4 2 40 1
    
    Expected output
    493 458