http://codeforces.com/contest/353/problem/C

先预处理前i个数的和,然后找到第一个出现的1,然后变成0后的和与目前的和比较,如果大就更新。

cf C. Find Maximum_#includecf C. Find Maximum_i++_02
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define maxn 100010
 5 using namespace std;
 6 
 7 int n;
 8 int a[maxn];
 9 int sum[maxn];
10 char str[maxn];
11 int k;
12 
13 int main()
14 {
15     while(scanf("%d",&n)!=EOF)
16     {
17         memset(sum,0,sizeof(sum));
18         for(int i=1; i<=n; i++)
19         {
20             scanf("%d",&a[i]);
21             sum[i]=sum[i-1]+a[i];
22         }
23         scanf("%s",str);
24         int ans=0;
25         for(int i=0; i<=n-1; i++)
26         {
27             if(str[i]=='1')
28             {
29                 ans+=a[i+1];
30                 k=i;
31             }
32         }
33         int max1=ans;
34         int c=0;
35         for(int i=k; i>0; i--)
36         {
37             if(str[i]=='1')
38             {
39                 max1=max(max1,sum[i]+c);
40                 c+=a[i+1];
41             }
42         }
43         printf("%d\n",max1);
44     }
45     return 0;
46 }
View Code