3459.Find Maximum

Time Limit: 1s Memory Limit: 256MB

Valera has array a, consisting of n integers a0,a1,...,an-1, and function f(x), taking an integer from 0 to 2n-1 as its single argument. Value f(x) is calculated by formula 3459_1.png, where value bit(i) equals one if the binary representation of number x contains a 1 on the i-th position, and zero otherwise.For example, if n=4 and x=11 (11=20+21+23), then f(x)=a0+a1+a3.Help Valera find the maximum of function f(x) among all x, for which an inequality holds: 0 \le x \le m.

Input Format(From the terminal/stdin)

Input The first line contains integer n (1 \le n \le 105) - the number of array elements. The next line contains n space-separated integers a0,a1,...,an-1 (0 \le ai \le 104) - elements of array a.The third line contains a sequence of digits zero and one without spaces s0s1... sn-1 - the binary representation of number m. Number m equals 3459_2.png.

Output Format(To the terminal/stdout)

Output Print a single integer - the maximum value of function f(x) for all 3459_3.png.

Sample Input 1

Copy
2
3 8
10
 \n
 · \n
  \n

Sample Output 1

Copy
3
 \n

Sample Input 2

Copy
5
17 0 10 2 1
11010
 \n
  · ·  · · \n
     \n

Sample Output 2

Copy
27
  \n

Hints

In the first test case m=20=1,f(0)=0,f(1)=a0=3.In the second sample m=20+21+23=11, the maximum value of function equals f(5)=a0+a2=17+10=27.

Submit

请先 登录

© 2025 FAQs