-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathShop in Candy Store.cpp
59 lines (55 loc) · 1.03 KB
/
Shop in Candy Store.cpp
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
//{ Driver Code Starts
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution
{
public:
vector<int> candyStore(int c[], int n, int k)
{
// Write Your Code here
sort(c,c+n);
int free=n-1;
int mini=0;
int buy=0;
while(buy<=free)
{
mini+=c[buy];
buy++;
free-=k;
}
int maxi=0;
free=0;
buy=n-1;
while(buy>=free)
{
maxi+=c[buy];
buy--;
free+=k;
}
vector<int>v;
v.push_back(mini);
v.push_back(maxi);
return v;
}
};
//{ Driver Code Starts.
int main()
{
int t;
cin >> t;
while (t--)
{
int N, K;
cin >> N >> K;
int candies[N];
for (int i = 0; i < N; i++)
{
cin >> candies[i];
}
Solution ob;
vector<int> cost = ob.candyStore(candies, N, K);
cout << cost[0] << " " << cost[1] << endl;
}
return 0;
}