- 난이도: 실버 3
- 알고리즘: 그리디 알고리즘
이전 문제(전구와 스위치)는 난이도에 비해 정말 어려웠지만, 이 문제는 난이도에 비해 너무 쉬웠다. 첫 번째 사람이 최대한 빨리 나와줘야 다른 사람들의 시간도 전체적으로 감소하므로, 가장 작은 숫자를 가장 많이 세면 된다. 따라서 오름차순으로 정렬한 뒤 total += (vec[i] * (n - i))
연산을 통해 결과를 구했다.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n;
cin >> n;
vector<int> vec;
for (int i = 0; i < n; i++) {
int p;
cin >> p;
vec.emplace_back(p);
}
sort(vec.begin(), vec.end());
int total = 0;
for (int i = 0; i < n; i++) {
total += (vec[i] * (n - i));
}
cout << total;
}