1637C - Andrew and Stones - CodeForces Solution


greedy implementation *1200

Please click on ads to support us..

Python Code:

def main():
    
                t = int(input())
    allans = []
    for _ in range(t):
        n = int(input())
        a = readIntArr()
        b = []
        for i in range(1, n - 1):
            b.append(a[i])
        b.sort(reverse = True)
        ans = 0
        for x in b:
            ans += (x + 1) // 2
        if len(b) == 1:
            if b[0] % 2 == 1:
                ans = -1
        elif b[0] % 2 == 1:
            if b[1] == 0:
                if b[0] % 2 == 1 and b[0] < 5:
                    ans = -1
                else:
                    ans += 1             elif b[1] == 1 and b[0] == 1:
                ans = -1
        allans.append(ans)
    multiLineArrayPrint(allans)
    
    return
 
import sys
input=sys.stdin.buffer.readline  
def oneLineArrayPrint(arr):
    print(' '.join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
    print('\n'.join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
    print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))
 
def readIntArr():
    return [int(x) for x in input().split()]
 
def makeArr(defaultValFactory,dimensionArr):     dv=defaultValFactory;da=dimensionArr
    if len(da)==1:return [dv() for _ in range(da[0])]
    else:return [makeArr(dv,da[1:]) for _ in range(da[0])]
 
def queryInteractive(a, b, c):
    print('? {} {} {}'.format(a, b, c))
    sys.stdout.flush()
    return int(input())
 
def answerInteractive(x1, x2):
    print('! {} {}'.format(x1, x2))
    sys.stdout.flush()
 
inf=float('inf')
 
from math import gcd,floor,ceil
import math
 
for _abc in range(1):
    main()

C++ Code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int a, b, c, i, j, k, m, n, o, l, r, f = 0, cnt = 0, even, odd, sum = 0,ans, total = 0, mn = INT_MAX, mx = INT_MIN, sz, len, temp, pos, tc, x, y, z, arr[100000], brr[100000];
    vector<long long int> vrr;
    string s;

    cin>>tc;
    while(tc--)
    {
        cin>>n;
        even = 0;
        odd = 0;
        for( i = 1; i<=n; i++ )
        {
            cin>>arr[i];
        }

        bool has_even=false,all_one=true,possible=true;

        ans = 0 ;
        for( i = 2; i<n; i++ )
        {
            if( arr[i] != 1 )  all_one=false;
            if(arr[i]%2==0)
            {
                has_even=true;
            }


            ans+=((arr[i]+1)/2);

        }

         if(all_one==true || (n==3 and !has_even)){
            possible=false;
        }

        if( possible ) cout<< ans << endl;
        else cout<< -1 << endl;
    }



}


Comments

Submit
0 Comments
More Questions

1342A - Road To Zero
1520A - Do Not Be Distracted
352A - Jeff and Digits
1327A - Sum of Odd Integers
1276A - As Simple as One and Two
812C - Sagheer and Nubian Market
272A - Dima and Friends
1352C - K-th Not Divisible by n
545C - Woodcutters
1528B - Kavi on Pairing Duty
339B - Xenia and Ringroad
189A - Cut Ribbon
1182A - Filling Shapes
82A - Double Cola
45A - Codecraft III
1242A - Tile Painting
1663E - Are You Safe
1663D - Is it rated - 3
1311A - Add Odd or Subtract Even
977F - Consecutive Subsequence
939A - Love Triangle
755A - PolandBall and Hypothesis
760B - Frodo and pillows
1006A - Adjacent Replacements
1195C - Basketball Exercise
1206A - Choose Two Numbers
1438B - Valerii Against Everyone
822A - I'm bored with life
9A - Die Roll
1430B - Barrels