complete 1183.cpp 1190.cpp 1929.cpp

This commit is contained in:
2026-07-10 15:25:46 +09:00
parent 1a69d9b1e9
commit 5d8efe99be
3 changed files with 187 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#define let auto
#define fn auto
#define usize size_t
using namespace std;
fn fastio() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
fn main() -> int {
fastio();
usize n;
cin >> n;
let lengths = vector<usize>(n);
for (let &l: lengths) {
cin >> l;
}
let gt = std::greater<usize>();
make_heap(lengths.begin(), lengths.end(), gt);
usize cost = 0;
while (lengths.size() > 1) {
pop_heap(lengths.begin(), lengths.end(), gt);
let a = lengths.back();
lengths.pop_back();
pop_heap(lengths.begin(), lengths.end(), gt);
let b = lengths.back();
lengths.pop_back();
cost += a + b;
lengths.push_back(a + b);
push_heap(lengths.begin(), lengths.end(), gt);
}
cout << cost << endl;
}