codeforce Find Maximum

原创 2013年12月04日 20:46:13
 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.



英语水平差的真是没法说啊。看这个题目算是看了好久才明白。其实题目要我们求的并不难,首先是给出一个n(表示数组元素的个数),下一行则给出n个数,即为数组里面的元素。第三行则给出的是一个数的二进制的表示形式。然后要求我们按照题目所给的公式进行求值。就是这样。

代码如下:

#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;
}

相关文章推荐

448. Find All Numbers Disappeared in an Array \ 414. Third Maximum Number

448法一:class Solution { public: vector findDisappearedNumbers(vector& nums) { int nums_le...

HDU 4002Find the maximum(欧拉函数)

Find the maximum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others...
  • opm777
  • opm777
  • 2013年08月19日 13:43
  • 696

Find the maximum/hdoj 4002 2011 Regional Dalian Online euler fuction

这题是用来求1到n间i/euler(i)最大值。可得x/(x(1-1/p1)(1-1/p2)(1-1/p3).....(1-1/pn))  (pi为x的质因数),则有p1*p2*p3....*pn/(...

HDU 4002 Find the maximum(C++大整数实现+打表)

Problem Description Euler's Totient function, φ (n) [sometimes called the phi function], is used to...

HDU 4002 Find the maximum(JAVA 大数找规律 )

HDU 4002 Find the maximum(JAVA 大数找规律 )

Leetcode--->Given n points on a 2D plane, find the maximum number of points that lie on the same str

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line...

Codeforces 353C Find Maximum【贪心】

C. Find Maximum time limit per test 1 second memory limit per test 256 megabytes input s...

hdu 1231, dp ,maximum consecutive sum of integers, find the boundaries, possibly all negative, C++

hdu 1231, maximum consecutive sum of integers, finding the boundaries, possibly all negative, C++
  • qeatzy
  • qeatzy
  • 2015年07月12日 03:24
  • 494

HDU 4002 Find the maximum (欧拉函数-积性函数的性质(2011年大连赛区网络赛第二题)

【题目链接】:click here~~ 【题目大意】: 给出一个整数n,求一个数x,x在1到n之间,并且x/φ(x)最大(其中φ(x)为x的欧拉函数)。 【思路】: 由欧拉函数为积性函数,即:如果 ...

CCI: Find maximum subsquare

Imagine you have a square matrix, where each cell is filled with either black or white. Design an al...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:codeforce Find Maximum
举报原因:
原因补充:

(最多只允许输入30个字)