621A - Wet Shark and Odd and Even - CodeForces Solution


implementation *900

Please click on ads to support us..

Python Code:

N = int(input())
arr =[]
num_array = []
if(1<=N and N<=100000):
    sum = 0
    point = input()
    str_num = ''
    for i in point:
        if ( i.isdigit()):
            str_num += i
        else:
            if ( i == ' '):
                arr.append(int(str_num))
                str_num = ''
    arr.append(int(str_num))
    for i in arr:
        if(i%2==0):
            sum += i

        elif(i%2!=0):
            num_array.append(i)

    cnt_array = len(num_array)

    if (cnt_array % 2 != 0):
        num_array.sort()
        num_array.pop(0)
        for i in num_array:
            sum+=i

    else:
        for i in num_array:
            sum+=i

    print(sum)


else:
    print(0)

C++ Code:

//Believe in yourself ,( try and try and then try ), You can do it
#include <bits/stdc++.h>
#define ll long long
#define line "\n"
using namespace std;
ll summ(vector<ll>&arr );
void fast();
/*
index = (index+1) % n; // to right or back to 0
index = (index+n-1) % n; // to left or back to n-1
*/
int main()
{
  fast();
  unsigned ll n , temp , res=0 , total =0 , smallestOdd=LLONG_MAX , sumAllOdd=0; 
  vector<ll> arr; 
  cin>>n; 
  for(ll i=0; i<n; i++){
      cin>>temp;      
      if(temp%2==0) 
        res+=temp; 
      else {
        arr.push_back(temp);  //push odds only
        smallestOdd=min(smallestOdd , temp); 
        sumAllOdd+=temp; 
        }
      total+=temp ; 
      
    }//arr is all odds
  if(total%2==0) {cout<<total; return 0; }
  ll len =arr.size(); 
//   cout<<len<<" "<<sumAllOdd<<" "<<smallestOdd<<line; 
  if(len%2==0) res+=sumAllOdd; 
  else res+=sumAllOdd-smallestOdd; 
  cout<<res; 
  //idea is even + even is even and odd + odd = even but others are gonna give me odd. 
  //so all evens are gonna make even sum so i execulde them from pushing and i push all odds only 
  //and if # of array of odds is even so all of them are part of pairs of odd + odd but if 
  // # of array of odds is odd then so all of them are part of pairs of odd + odd except
  //one which is the smallest odd cuz i wanna make max even.
  
}

void fast(){
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin.sync_with_stdio(0);
}


Comments

Submit
0 Comments
More Questions

1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal
589. N-ary Tree Preorder Traversal
1299. Replace Elements with Greatest Element on Right Side
1768. Merge Strings Alternately
561. Array Partition I
1374. Generate a String With Characters That Have Odd Counts
1822. Sign of the Product of an Array
1464. Maximum Product of Two Elements in an Array
1323. Maximum 69 Number
832. Flipping an Image
1295. Find Numbers with Even Number of Digits
1704. Determine if String Halves Are Alike
1732. Find the Highest Altitude
709. To Lower Case
1688. Count of Matches in Tournament
1684. Count the Number of Consistent Strings