# codeforce Find Maximum

338人阅读 评论(0)

Find Maximum
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 , where value bit(i) equals one if the binary representation of number xcontains 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 ≤ x ≤ m.

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of array elements. The next line contains n space-separated integersa0, a1, ..., an - 1 (0 ≤ ai ≤ 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. Numberm equals .

Output

Print a single integer — the maximum value of function f(x) for all .

Sample test(s)
input
2
3 8
10

output
3

input
5
17 0 10 2 1
11010

output
27

Note

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.

#include<stdio.h>
#define MAX 100000+5
int A[MAX];
int g_n;
char ch[MAX];

int f_max(int a, int b)
{
return a>b?a:b;
}

int main()
{
int i;
int sum=0,Max=0;
scanf("%d", &g_n);
for(i=0; i<g_n; ++i)
scanf("%d", &A[i]);
scanf("%s", ch);
for(i=0; ch[i]; ++i)
{
if('1'==ch[i])
{
Max=f_max(sum, Max+A[i]);
}
sum+=A[i];
}
printf("%d\n", Max);
return 0;
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：62581次
• 积分：1805
• 等级：
• 排名：千里之外
• 原创：122篇
• 转载：4篇
• 译文：0篇
• 评论：1条
文章分类
阅读排行
评论排行
最新评论