Skip to content

Instantly share code, notes, and snippets.

@nomarlo
Created December 29, 2015 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nomarlo/0c00cb88d2eac33c17a0 to your computer and use it in GitHub Desktop.
Save nomarlo/0c00cb88d2eac33c17a0 to your computer and use it in GitHub Desktop.
/**
La idea de solución es tomar siempre los valoresminimos disponibles.
Para ello utilizamos una cola de prioridad(minima) y simplemente,
simulamos el proceso
**/
#include <iostream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <cstdio>
using namespace std;
struct compare
{
bool operator()(const int& l, const int& r)
{
return l > r;
}
};
priority_queue<int, vector<int>,compare > PQ;
int n,res,aux;
int main(){
scanf("%d",&n);
while(n){
res=0;
while(n--){
scanf("%d",&aux);
PQ.push(aux);
}
while(!PQ.empty()){
int ax=PQ.top();
PQ.pop();
ax+=PQ.top();
PQ.pop();
if(!PQ.empty())
PQ.push(ax);
res+=ax;
}
printf("%d\n",res);
scanf("%d",&n);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment