Codeforces59 B - Fortune Telling(再水)

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");
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章