Problem 1565 - B - Magic
Time Limit: 1000MS Memory Limit: 65536KB
Total Submit: 477 Accepted: 180 Special Judge: No
Description
Here are n numbers.
You have a magic : first , you choose a interval [l,r],and then each Si(l<=i<=r) will be ( 10 – Si ) % 10.
You can use the magic at most once to make sum of all the numbers to be maximum.
What is the maximum sum you can get?
Input
First line of each case contains a number n.(1 ≤ n ≤ 1 000 000).
Next line contains n numbers without space-separated. Each position corresponds to a number of 0-9.
Output
Output the answer on a single line for each case.
Sample Input
10
3775053808
10
2580294019
10
4701956095
Sample Output
50
50
54
Hint
Time Limit: 1000MS Memory Limit: 65536KB
Total Submit: 477 Accepted: 180 Special Judge: No
Description
Here are n numbers.
You have a magic : first , you choose a interval [l,r],and then each Si(l<=i<=r) will be ( 10 – Si ) % 10.
You can use the magic at most once to make sum of all the numbers to be maximum.
What is the maximum sum you can get?
Input
First line of each case contains a number n.(1 ≤ n ≤ 1 000 000).
Next line contains n numbers without space-separated. Each position corresponds to a number of 0-9.
Output
Output the answer on a single line for each case.
Sample Input
10
3775053808
10
2580294019
10
4701956095
Sample Output
50
50
54
Hint
Source
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int shu[1001000];
int main()
{
char ch[1001000];
int n;
while (~scanf("%d",&n))
{
scanf("%s",ch);
int s,p;
s=0;
for (int i=0;i<n;i++)
{
p=ch[i]-'0';
s+=p;
shu[i]=(10-p)%10-p;
}
int ss=0,ka=0;
for (int i=0;i<n;i++)
{
ka+=shu[i];
ss=max(ss,ka);
if (ka<0)
ka=0;
}
if (ss>0)
s+=ss;
printf("%d\n",s);
}
return 0;
}