-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB_Maximum_Sum.cpp
More file actions
76 lines (70 loc) · 1.74 KB
/
B_Maximum_Sum.cpp
File metadata and controls
76 lines (70 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<bits/stdc++.h>
using namespace std ;
# define int long long int
# define vi vector<int>
# define vs vector<string>
# define vb vector<bool>
# define vv vector<vector<int>>
# define f(i,a,b) for(int i=a;i<b;i++)
# define fr(i,a,b) for(int i=a;i>b;i--)
# define in cin>>
# define out cout<<
# define nl <<endl;
# define input(arr) fr(0,arr.size()) in arr[i]
# define output(arr) fr(0,arr.size()) out arr[i]<<' '
# define mod 1e9+7
# define ps(x,y) fixed<<setprecision(y)<<x
# define setbits(x) __builtin_popcountll(x)
# define zrobits(x) __builtin_ctzll(x)
# define inf 1e18
# define sortv(a) sort(a.begin(),a.end())
int gcd(int a, int b){return b == 0 ? a : gcd(b, a % b);}
bool isprime(int x){for(int i=2;i*i<=x;i++) if(x%i==0) return 0 ;return (x==1? 0 : 1);}
int solve(int n,int k,vi a){
sortv(a);
int min_1 = 0;
int min_2 = 1;
int maxi = n-1;
int sum = 0;
f(i,0,n){
sum += a[i];
}
// if(min1+min2=<maxi){
// k--;
// sum = sum - min1 - min2;
// }else{
// k--;
// sum = sum - maxi;
// }
while(k > 0 ) {
if(a[min_1] + a[min_2] <= a[maxi]) {
sum -= (a[min_1] + a[min_2]);
min_1 += 2;
min_2 += 2;
} else {
sum -= a[maxi];
maxi--;
}
k--;
}
return sum;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int T=0;
cin>>T;
while(T--){
int n;
in n;
int k;
in k;
vi a(n);
f(i,0,n){
in a[i];
}
out solve(n,k,a) nl;
}
return 0;
}