304 North Cardinal St.
Dorchester Center, MA 02124

# Clear the Array CodeChef Solution

## Clear the Array CodeChef Solution in C++17

``````#include <iostream>
#include <bits/stdc++.h>
using namespace std;

#define ll                  long long
#define new1(n)             ll int  n;cin>>n;
#define new2(n,k)           ll int  n,k;cin>>n>>k;
#define new3(a,b,c)         ll int a,b,c;cin>>a>>b>>c;
#define new4(a,b,c,d)       ll int a,b,c,d;cin>>a>>b>>c>>d;
#define fl(n,i)             for(ll int  i=0;i<n;i++)

#define revfl(n)            for(ll int i=n-1;i>=0;i--)
#define mod                 1000000007
#define mp                  make_pair
#define pb                  push_back
#define test                new1(t);while(t--)
#define take(arr,n)         ll arr[n];fl(n,i) cin>>arr[i];
#define INF                 0x3f3f3f3f

void func(int l,int r,int val,vector<int>&vect)

{
if(l>r)return;
if(l==r)vect[l]=val;
else
{
int mid=(r-l+1)/2+l;
vect[mid]=val;
func(l,mid-1,val+1,vect);
func(mid+1,r,val+1,vect);
}
}

int main()
{

test
{
new3(n,k,x);
take(arr,n);
sort(arr,arr+n);
reverse(arr,arr+n);
ll sum=0,ct=0;
for(int i=0;i<n-1,k>0;i++)
{
if(arr[i]+arr[i+1]>x)

{
k--;
sum+=x;
i++;
ct+=2;
}
else break;
}
for(int i=0;i<n;i++)
sum+=arr[i];
for(int i=0;i<ct;i++)
sum-=arr[i];
cout<<sum<<"\n";
}

return 0;

}``````

## Clear the Array CodeChef Solution in C++14

``````#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define ff first
#define ss second
// #define pb push_back
// #define mp make_pair
// #define fr(i,n) for(int i=0;i<n;i++)
#define vi(v) vector<int>v;
// #define fr(i,n,m) for(int i=n;i>=m;i--)
// #define py cout<<"YES\n";
// #define pn cout<<"NO\n";
#define pi 3.141592653589793238
// #define vr(v) v.begin(),v.end()
// #define rv(v) v.end(),v.begin()
#define Code ios_base::sync_with_stdio(false);
#define By cin.tie(NULL);
#define Asquare cout.tie(NULL);

ll gcd(ll a, ll b){if (b == 0)return a;return gcd(b, a % b);}
ll lcm(ll a, ll b){return (a/gcd(a,b)*b);}
bool sorta(const pair<int,int> &a,const pair<int,int> &b){return (a.second < b.second);}
bool sortd(const pair<int,int> &a,const pair<int,int> &b){return (a.second > b.second);}
// void printarr(ll arr[], ll n){fr(i,n) cout << arr[i] << " ";cout << "\n";}
string decToBinary(int n){string s="";int i = 0;while (n > 0) {s =to_string(n % 2)+s;n = n / 2;i++;}return s;}
ll binaryToDecimal(string n){string num = n;ll dec_value = 0;int base = 1;int len = num.length();for(int i = len - 1; i >= 0; i--){if (num[i] == '1')dec_value += base;base = base * 2;}return dec_value;}
bool isPrime(int n){if(n<=1)return false;if(n<=3)return true;if(n%2==0||n%3==0)return false;for(int i=5;i*i<=n;i=i+6)if(n%i==0||n%(i+2)==0)return false;return true;}
bool isPowerOfTwo(int n){if(n==0)return false;return (ceil(log2(n)) == floor(log2(n)));}
bool isPerfectSquare(ll x){if (x >= 0) {ll sr = sqrt(x);return (sr * sr == x);}return false;}
ll moduloMultiplication(ll a,ll b,ll mod){ll res = 0;a %= mod;while (b){if (b & 1)res = (res + a) % mod;b >>= 1;}return res;}
ll powermod(ll x, ll y, ll p){ll res = 1;x = x % p;if (x == 0) return 0;while (y > 0){if (y & 1)res = (res*x) % p;y = y>>1;x = (x*x) % p;}return res;}
int factorial(int n){return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);}

void solution(){
ll n,k,x;
cin>>n>>k>>x;
vector<ll> a(n);
for(int i=0;i<n;i++)
cin>>a[i];
sort(a.begin(),a.end());

ll ans=0;
while(k>0 && a.size()>1){
int j=a.size();
ll sum=a[j-1]+a[j-2];

if(sum>=x){
ans+=x;
a.pop_back();
a.pop_back();
k--;
}

else
break;

}
ans+=accumulate(a.begin(),a.end(),0ll);
cout<<ans<<"\n";
}
int main(){
ll t;
cin>>t;
while(t--){
solution();
}
return 0;
}``````

## Clear the Array CodeChef Solution in PYTH 3

``````# cook your dish here
for _ in range(int(input())):
n,k,x=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
l.reverse()
i,j=0,1
cs,ans=sum(l),sum(l)
while(k>0):
p=l[i]+l[i+1]
cs-=p
if j*x+cs<ans:
ans=j*x+cs
i+=2
j+=1
k-=1
print(ans)``````

## Clear the Array CodeChef Solution in C

``````#include <stdio.h>

int main(void) {
int testCase;
scanf("%d",&testCase);
long long int n,x,k;
while(testCase-->0){
scanf("%lld", &n);scanf("%lld", &k);scanf("%lld", &x);

long long int nums[n];
for(long long int i=0;i<n;++i)scanf("%lld", &nums[i]);
mergeSort(nums,n);

long long int sum=0,ct=0;

for(int i=0;i<n-1,k>0;i++)
if(nums[i]+nums[i+1]>x){k--;sum+=x;i++;ct+=2;}
else break;

for(int i=0;i<n;i++)sum+=nums[i];
for(int i=0;i<ct;i++)sum-=nums[i];

printf("%lld\n",sum);
}

return 0;
}

void mergeSort(long long int nums[],long long int numsSize){
if(numsSize>1){
long long int mid=numsSize/2;
long long int left[numsSize-mid];
long long int right[mid];
for(int i=0;i<numsSize-mid;++i){left[i]=nums[i];}
for(int i=0;i<mid;++i){right[i]=nums[i+numsSize-mid];}
mergeSort(left,numsSize-mid);mergeSort(right,mid);
long long int l=0; long long int r=0; long long int n=0;
while(l<(numsSize-mid) && r<mid){
if(left[l]>right[r]){nums[n]=left[l];++l;++n;}
else{nums[n]=right[r];++r;++n;}
}
while(l<(numsSize-mid)){nums[n]=left[l];++l;++n;}
while(r<mid){nums[n]=right[r];++r;++n;}
}
}``````

## Clear the Array CodeChef Solution in JAVA

``````import java.util.*;
public class Main {
public static void main(String args[]) {

Scanner sc = new Scanner(System.in);
int t = sc.nextInt();

while(t-->0){

int n = sc.nextInt();
int k = sc.nextInt();
long x = sc.nextLong();

long arr[] = new long[n];
for(int i = 0; i < n; i++){
arr[i] = sc.nextLong();

}

Arrays.sort(arr);
long sum = 0;
for(int i = n-1; i >= 0;){
if(i == 0 && k > 0){
sum += Math.max(arr[i],x);
i--;
}
else if(k > 0 &&  (i > 0 && arr[i] + arr[i-1] >= x)){
k--;
sum += x;
i-=2;
}else{
sum += arr[i];
i--;
k = 0;
}
}

System.out.println(sum);
}

}

}``````

## Clear the Array CodeChef Solution in PYPY 3

``````# cook your dish here
from heapq import  heapify, heappop, heappush
for _ in range(int(input())):
n,k,x=map(int,input().split())
arr=list(map(int, input().split()))
heap=[]
for y in arr:
heappush(heap, -y)
ans=0
while(k):
if len(heap)>=2:
a, b=-heappop(heap), -heappop(heap)
if a+b<x:
heappush(heap, -a)
heappush(heap, -b)
break
ans+=x
k-=1
else:
break
while(heap):
ans+=-heappop(heap)
print(ans)``````

## Clear the Array CodeChef Solution in PYTH

``````t = int(raw_input())
for i in range(t):
st = raw_input().split()
N = int(st[0])
K = int(st[1])
X = int(st[2])
st = raw_input().split()
A = []
for x in st:
A.append(int(x))
# endfor x
A.sort()
m = N-2*K
tot = 0
for p in range(m):
tot += A[p]
# endfor p
p = m
while p < N:
v = A[p]+A[p+1]
p += 2
tot += min(v,X)
# endwhile
print tot
# endfor i
``````

## Clear the Array CodeChef Solution in C#

``````using System;

public class Test
{
public static void Main ()
{
string[] input = Console.In.ReadToEnd().Split(new char[] {' ', '\n', '\t', '\r' }, StringSplitOptions.RemoveEmptyEntries);

int indx = 0;
{
long sum, closeSum;

long[] data = new long[5001];

for (int i = 1, n, k, x, j; i < input.Length;)
{
n = int.Parse(input[i++]);

k = int.Parse(input[i++]);

x = int.Parse(input[i++]);

for (j = 0; j < n;)
data[j++] = long.Parse(input[i++]);

sum = 0L;

if (k > 0)
{
Array.Sort(data, 0, n--);

while(k-->0)
{
closeSum = data[n];

data[n--] = 0L;

closeSum += data[n];

data[n--] = 0L;

if (closeSum > x) sum += x;

else { sum += closeSum; break; }
}
}

while(n>=0) { sum += data[n]; data[n--] = 0; }

input[indx++] = sum.ToString();
}
for (int i = 0; i < indx; i++) Console.WriteLine(input[i]);
}
}
}``````

## Clear the Array CodeChef Solution in GO

``````package main

import (
"bufio"
"bytes"
"fmt"
"os"
"sort"
)

func main() {
var buf bytes.Buffer
for tc > 0 {
tc--
res := solve(n, k, x, A)
buf.WriteString(fmt.Sprintf("%d\n", res))
}
fmt.Print(buf.String())
}

func readInt(bytes []byte, from int, val *int) int {
i := from
sign := 1
if bytes[i] == '-' {
sign = -1
i++
}
tmp := 0
for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
tmp = tmp*10 + int(bytes[i]-'0')
i++
}
*val = tmp * sign
return i
}

return
}

a, b = res[0], res[1]
return
}

a, b, c = res[0], res[1], res[2]
return
}

res := make([]int, n)
x := 0
for i := 0; i < n; i++ {
for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
x++
}
}
return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
i := from

var tmp uint64
for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
tmp = tmp*10 + uint64(bytes[i]-'0')
i++
}
*val = tmp

return i
}

func solve(n int, k, x int, A []int) int64 {
sort.Ints(A)
X := int64(x)
var res int64
i := n - 1
for i > 0 && k > 0 {
tmp := int64(A[i]) + int64(A[i-1])
if tmp < X {
break
}
res += X
i -= 2
k--
}

for i >= 0 {
res += int64(A[i])
i--
}
return res
}

func min(a, b int64) int64 {
if a <= b {
return a
}
return b
}``````
##### Clear the Array CodeChef Solution Review:

In our experience, we suggest you solve this Clear the Array CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Clear the Array CodeChef Solution.

Find on CodeChef

##### Conclusion:

I hope this Clear the Array CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!