cswork / OAOpen practice workspace

AmazonOA #267

Maximize Protected City Population

Estimated MediumJudge-ready

Topics (AI-labelled)GreedyState Machines

Cities are arranged in a row, and unit[i]=1 means a unit is initially there. A unit may stay or move once to the adjacent city on its left; a unit in the first city can only stay. In the end, a city with at least one unit contributes population[i] once; overlapping units do not contribute again. Find the maximum protected population.

Input

The first line contains n, the second line population, and the third line a 01 string unit of length n. The original text only kept the constraint that the two arrays have equal length, with no numeric bounds; this site uses 1≤n≤200000, 0≤population[i]≤10^9.

Output

Output a single integer, the answer.

Example 1

Input:

5
10 5 8 9 6
01101

Output:

27

Example 2

Input:

2
7 4
01

Output:

7

Example 3

Input:

3
1 100 1
011

Output:

101

Example notes

The answers are 27, 7 and 101. In the third example two units cannot count the 100 in the middle twice.

Editorial

Loading solution…