Codeforces59 B - Fortune Telling(再水)

来源:互联网 发布:mac开机密码怎么取消 编辑:程序博客网 时间:2024/06/05 10:48

B. Fortune Telling
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Marina loves Sasha. But she keeps wondering whether Sasha loves her. Of course, the best way to know it is fortune telling. There are many ways of telling fortune, but Marina has picked the easiest one. She takes in her hand one or several camomiles and tears off the petals one by one. After each petal she pronounces alternatively “Loves” and “Doesn’t love”, at that Marina always starts with “Loves”. There are n camomiles growing in the field, possessing the numbers of petals equal to a1, a2, … an. Marina wants to pick a bouquet with the maximal possible total number of petals so that the result would still be “Loves”. Help her do that; find the maximal number of petals possible in the bouquet.

Input
The first line contains an integer n (1 ≤ n ≤ 100), which is the number of flowers growing in the field. The second line contains n integers ai (1 ≤ ai ≤ 100) which represent the number of petals on a given i-th camomile.

Output
Print a single number which is the maximal number of petals in the bouquet, the fortune telling on which would result in “Loves”. If there are no such bouquet, print 0 instead. The bouquet may consist of a single flower.

Examples
input
1
1
output
1
input
1
2
output
0
input
3
5 6 7
output
13
分析
题意:有n束花,每束花都有a[i]个花瓣,求在这n束花中选出几束花,使得这几束花的花瓣总数为最大奇数(奇数就表白),输出这几束花总的花瓣数目.
思路:
n个数,找到这n个数中最小的奇数x,并求出这n个数的和sum
如果sum是奇数,printf sum
如果sum是偶数,print sum-x

#include<iostream>#include<cstdio>#include<algorithm>#include<string.h>#include<stdlib.h>using namespace std;int a[105];int main(){    int n;    scanf("%d",&n);    int sum=0;    int mi=105;    for(int i=0;i<n;i++){        scanf("%d",&a[i]);        if(a[i]&1)            mi=min(mi,a[i]);        sum+=a[i];    }    if(sum&1){        printf("%d\n",sum);    }    else{        if(mi!=105)            printf("%d\n",sum-mi);        else            printf("0\n");    }}
0 0
原创粉丝点击