-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnikunjAndDonuts.cpp
More file actions
58 lines (39 loc) · 1.35 KB
/
nikunjAndDonuts.cpp
File metadata and controls
58 lines (39 loc) · 1.35 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
// Nikunj and Donuts
// Send Feedback
// Nikunj loves donuts, but he also likes to stay fit. He eats n donuts in one sitting, and each donut has a calorie count, ci. After eating a donut with k calories, he must walk at least 2^j x k(where j is the number donuts he has already eaten) miles to maintain his weight.
// Given the individual calorie counts for each of the n donuts, find and print a long integer denoting the minimum number of miles Nikunj must walk to maintain his weight. Note that he can eat the donuts in any order.
// Input
// The first line contains an integer, n, denoting the number of donuts.
// The second line contains n space-separated integers describing the respective calorie counts of each donut I, i.e ci.
// Output
// Print a long integer denoting the minimum number of miles Nikunj must walk to maintain his weight.
// Constraints
// 1 ≤ n ≤ 40
// 1 ≤ ci ≤ 1000
// Sample Input
// 3
// 1 3 2
// Sample Output
// 11
#include<bits/stdc++.h>
using namespace std;
long getMinMiles(int *cal, int n){
sort(cal, cal+n, greater<int>());
long minMiles = 0;
for(int i = 0; i < n; i++) {
minMiles += cal[i] * (1 << i);
}
return minMiles;
}
int main() {
int n;
cin >> n;
int *cal = new int[n];
for(int i = 0; i < n; i++) {
cin >> cal[i];
}
long ans = getMinMiles(cal, n);
cout << ans << endl;
delete [] cal;
return 0;
}