Advertisement
Guest User

Untitled

a guest
Aug 9th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.60 KB | None | 0 0
  1. #include <fstream>
  2. #include <queue>
  3. #include <functional>
  4. using namespace std;
  5.  
  6.  
  7. int main()
  8. {
  9.     int N = 0;
  10.     priority_queue<int, vector<int>, greater<int>> values;
  11.     fstream iof{ "input.txt", ios::in };
  12.     iof >> N;
  13.     for (int i = 0; i < N; i++)
  14.     {
  15.         int letto;
  16.         iof >> letto;
  17.         values.push(letto);
  18.     }
  19.     iof.close();
  20.  
  21.     int cont = 0, price = 0;
  22.     while (cont < N - 1)
  23.     {
  24.         int somma = values.top();
  25.         values.pop();
  26.         somma += values.top();
  27.         values.pop();
  28.         price += somma;
  29.         values.push(somma);
  30.         cont++;
  31.     }
  32.  
  33.     iof.open("output.txt", ios::out);
  34.     iof << price;
  35.     iof.close();
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement