Codeforces Round #205 (Div. 2) / 353C Find Maximum (贪心)

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.

本文URL地址:http://www.bianceng.cn/Programming/sjjg/201410/45352.htm

思路:从左往右读,读到0就积累,读到1的时候,就看是修改这个1为0然后把前面的0改为1更大,还是不变更大。

修改的话,就从当前这个修改成0的1开始积累。

最终修改结果就是我们想要的x。

完整代码:

/*62ms,500KB*/

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxm = 100005;  

int a[maxm];
char s[maxm];  

int main()
{
    int n, sum, maxn, i;
    scanf("%d", &n);
    for (i = 0; i < n; ++i)
        scanf("%d", &a[i]);
    getchar();
    gets(s);
    sum = maxn = 0;
    for (i = 0; i < n; ++i)
    {
        if (s[i] & 15)
        {
            ///看是前面的一串0大,还是当前位置的1大
            if (maxn > a[i])
            {
                sum += maxn;
                maxn = a[i]; ///重新积累
            }
            else sum += a[i];
        }
        else maxn += a[i];///积累maxn
    }
    printf("%d\n", sum);
    return 0;
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索function
, 贪心
, integer
, contains
, maximum
, of
The
codeforces、codeforces round 305、codeforces 下载、codeforces round 389、codeforces hack,以便于您获取更多的相关知识。

时间: 2024-08-04 11:53:22

Codeforces Round #205 (Div. 2) / 353C Find Maximum (贪心)的相关文章

Codeforces Round #157 (Div. 1) C. Little Elephant and LCM (数学、dp)

C. Little Elephant and LCM time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output The Little Elephant loves the LCM (least common multiple) operation of a non-empty set of positive integers. The

接上一篇 580 B. Kefa and Company [ Codeforces Round #321 (Div. 2)]

B. Kefa and Company time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company. Kefa has n friends, each

Codeforces Round #201 (Div. 1) / 346A Alice and Bob:数论&amp;amp;想法题

A. Alice and Bob http://codeforces.com/problemset/problem/346/A time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output It is so boring in the summer holiday, isn't it? So Alice and Bob have inven

580A. Kefa and First Steps (Codeforces Round #321 (Div. 2))

A. Kefa and First Steps time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Kefa decided to make some money doing business on the Internet for exactly n days. He knows that on the i-th day (1 

Codeforces Round #322 (Div. 2) A、B、C

A. Vasya the Hipster time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output One day Vasya the Hipster decided to count how many socks he had. It turned out that he had a red socks and b blue socks

Codeforces Round #311 (Div. 2) B. Pasha and Tea

题目链接:http://codeforces.com/contest/557/problem/B 题意:给你n个boy,n个girl ,然后w表示茶壶的最大容量,然后n个茶杯,每个都有不同的容量,要求boy的茶杯里的茶水是girl的两倍,且boy和boy的容量一样,girl和girl的容量一样,问如何倒茶,求最大的倒茶量 #include <iostream> #include <cstdio> #include <algorithm> using namespace

Codeforces Round #299 (Div. 2) A. Tavas and Nafas

题目链接:http://codeforces.com/problemset/problem/535/A #include <iostream> #include <string> using namespace std; int main() { string s1[10]={"zero","one","two","three","four","five",&qu

Codeforces Round #308 (Div. 2) A. Vanya and Table

题目链接:http://codeforces.com/contest/552/problem/A hint: 就是求几个矩形的面积 #include <iostream> #include <cmath> using namespace std; struct point { int x; int y; }a[2]; int main() { int m; while(cin>>m) { int sum=0; while(m--) { //注释的是方法一 cin>

Codeforces Round #311 (Div. 2) A. Ilya and Diplomas

题目链接:http://codeforces.com/contest/557/problem/A #include <iostream> using namespace std; int minn[3]; int maxx[3]; int main() { int m; while(cin>>m) { int ans[3]={0}; for(int i=0; i<3; i++) cin>>minn[i]>>maxx[i]; for(int i=0; i