Advertisement
Guest User

Untitled

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