1609A - Divide and Multiply - CodeForces Solution


greedy implementation math number theory *900

Please click on ads to support us..

Python Code:

import sys
input=lambda:sys.stdin.readline().rstrip()
def solve():
	N=int(input())
	a=list(map(int,input().split()))
	p=0
	for i in range(N):
		while a[i]%2==0:
			a[i]//=2
			p+=1
	a=sorted(a)
	print(sum(a[:N-1])+a[N-1]*2**p)
T=int(input())
for i in range(T):
	solve()

C++ Code:

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define fast ios_base::sync_with_stdio(NULL);cin.tie(0);cout.tie(0)
#define F first
#define S second
#define yes cout << "YES\n"
#define no cout << "NO\n"
#define answer cout << ans1 << "\n"
using namespace std;
ll sum=0,d,ans2=1,maxi=0,mini=0,k;
ll tt=1,n,m,a,b,c,w,mod=7+1e9,ans1=0,h;
//bool f=0;
string s,t;
vector<ll> v,v2;


bool isKthBitSet(int n, int k)
{
  return ((n & (1 << k)) ? 1 : 0);
}

ll power(ll aa,ll bb, ll momo)
{
  ll res=1;
  while(bb)
  {
    if(bb%2)res=(res*aa)%momo;
    aa=(aa*aa)%momo;
    bb/=2;
  }
  return res;
}

ll modInverse(ll x,int p)
{
    return power(x, p - 2, p);
}

ll nCrModPFermat(ll x,int r, int p)
{
    // If n<r, then nCr should return 0
    if (x < r)
        return 0;
    // Base case
    if (r == 0)
        return 1;

    // Fill factorial array so that we
    // can find all factorial of r, n
    // and n-r
    unsigned long long fac[x + 1];
    fac[0] = 1;
    for (int i = 1; i <= x; i++)
        fac[i] = (fac[i - 1] * i) % p;

    return (fac[x] * modInverse(fac[r], p) % p * modInverse(fac[x - r], p) % p) % p;
}

string tostr(ll x)
{
  string s;
  while(x>9)
  {
    int tem=x%10;
    s.insert(s.begin(),char(tem+48));
    x/=10;
  }
  if(x!=0)s.insert(s.begin(),char(x+48));
  if(s.empty())s.insert(s.begin(),'0');
  return s;
}

bool ispalin(string s)
{
  int l=0,r=s.size()-1;
  bool ret=1;
  while(l<r)
  {
    if(s[l]!=s[r]){ret=0;break;}
    l++;r--;
  }
  return ret;
}

long long gcd(long long int a, long long int b)
{
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

long long lcm(int a, int b)
{
    return (a / gcd(a, b)) * b;
}

void solve()
{
  cin >> n;a=0;b=0;c=0;ans1=0;
  multiset<ll> st;
  for(int i=0;i<n;i++){cin >> a;while(a%2==0){a/=2;c++;}st.insert(a);b=max(b,a);}
  st.erase(st.find(b));
  ans1+=b*power(2,c,1e18);
  for(auto x : st)ans1+=x;
  answer;
}


int main(){
  fast;
  //freopen("input.txt","r",stdin);
  //freopen("output.txt","w",stdout);
  cin >> tt;
  while(tt--)
  {
    solve();
  }
  return 0;
}


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST