Saturday, 15 July 2017

Marc's Cakewalk Hacker Rank Problem Solution.


Problem:

Marc loves cupcakes, but he also likes to stay fit. He eats  cupcakes in one sitting, and each cupcake  has a calorie count, . After eating a cupcake with  calories, he must walk at least  (where  is the number cupcakes he has already eaten) miles to maintain his weight.
Given the individual calorie counts for each of the  cupcakes, find and print a long integer denoting the minimum number of miles Marc must walk to maintain his weight. Note that he can eat the cupcakes in any order.
Input Format
The first line contains an integer, , denoting the number of cupcakes.
The second line contains  space-separated integers describing the respective calorie counts of each cupcake, .
Constraints
Output Format
Print a long integer denoting the minimum number of miles Marc must walk to maintain his weight.
Sample Input 0
3
1 3 2
Sample Output 0
11
Explanation 0
Let's say the number of miles Marc must walk to maintain his weight is . He can minimize  by eating the  cupcakes in the following order:
  1. Eat the cupcake with  calories, so .
  2. Eat the cupcake with  calories, so .
  3. Eat the cupcake with  calories, so .
We then print the final value of , which is , as our answer.

Solution:

#include <bits/stdc++.h>
using namespace std;
/*
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main() { long long n,a,sum = 0; cin >> n; vector<long long> ar; for(long long i = 0; i < n; i++){ cin >> a; ar.push_back(a); } sort(ar.begin(),ar.end()); for(int i = ar.size() - 1,j = 0; i >= 0; i--,j++){ sum += (ar[i] * pow(2,j)); } cout << sum << endl; return 0; }

No comments:

Post a Comment