621A. Wet Shark and Odd and Even





  1. A. Wet Shark and Odd and Even
     Today, Wet Shark is given n integers. Using any of these integers no more than once, Wet Shark wants to get maximum possible even (divisible by 2) sum. Please, calculate this value for Wet Shark.
    Note, that if Wet Shark uses no integers from the n integers, the sum is an even integer 0.
    Input
    The first line of the input contains one integer, n (1 ≤ n ≤ 100 000). The next line contains n space separated integers given to Wet Shark. Each of these integers is in range from 1 to 109, inclusive.
    Output
    Print the maximum possible even sum that can be obtained if we use some of the given integers.
    Examples
    input
    Copy
    3
    1 2 3
    output
    Copy
    6
    input
    Copy
    5
    999999999 999999999 999999999 999999999 999999999
    output
    Copy
    3999999996
    Note
    In the first sample, we can simply take all three integers for a total sum of 6.
    In the second sample Wet Shark should take any four out of five integers 999 999 999.




  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. int main(){
  5. long long n,od=0,ev=0,sum=0;
  6. cin>>n;
  7. long long a[n];
  8. for(int i=0;i<n;i++){
  9. cin>>a[i];
  10. if(a[i]%2) od++;
  11. else ev++;
  12. }
  13. sort(a,a+n);
  14. reverse(a,a+n);
  15. for(int i=0;i<n;i++){
  16. if(a[i]%2==0) sum=sum+a[i];
  17. }
  18. if(od%2==0){
  19. for(int i=0;i<n;i++)
  20. if(a[i]%2==1) sum=sum+a[i];
  21. }
  22.  
  23. else if(od%2==1){int c=0;
  24. for(int i=0;i<n;i++)
  25. if(a[i]%2==1)
  26. {
  27. if(c==od-1) break;
  28. else
  29. sum=sum+a[i];
  30. c++;
  31. }
  32. }
  33. cout<<sum;
  34.  
  35.  
  36. return 0;
  37. }

Comments

Popular posts from this blog

Codeforce Problem 1703A. YES or YES?

Aptitude test assistant programmer 2018

1041A. Heist solution