This page is still under construction.

Parts of this page are still being built. What you see may change.

Binary Transformations

Time limit1sMemory limit256 MB

Summary
Given starting bits, target bits, and per-bit costs, flipping a bit i costs the sum of costs of all bits equal to 1 after the flip; find the minimum total price to reach the target.
Level

Hard8 of 10

Topics
Dynamic programming, Greedy, Sorting, Implementation
Solved
No attempts yet

Problem

There are nn bits numbered from 11 to nn. Bit ii starts with the value aia_i, which is 00 or 11, and its cost is cic_i.

One operation picks a bit ii and flips its value, so 00 becomes 11 and 11 becomes 00. The price of that operation is the sum of cjc_j over every bit jj whose value is 11 after the flip. When bit ii itself turns into 11, its own cost cic_i is part of that sum.

You may apply the operation to any bit, any number of times, in any order. Find the smallest total price that makes bit ii hold the value bib_i for every ii.

Input

The first line contains the integer nn (1≤n≤5 0001 \le n \le 5\,000), the number of bits.

The second line contains nn integers c1,c2,…,cnc_1, c_2, \dots, c_n (1≤ci≤1091 \le c_i \le 10^9), the cost of each bit.

The third line contains a string of nn characters a1a2…ana_1 a_2 \dots a_n, the starting values.

The fourth line contains a string of nn characters b1b2…bnb_1 b_2 \dots b_n, the required values.

Output

Print the smallest total price on one line.

Examples2

  1. Example 1

    Input
    5
    5 2 6 1 5
    01110
    10011
    
    Expected output
    21
    
  2. Example 2

    Input
    6
    100 1 1 1 1 1
    111000
    100111
    
    Expected output
    112