Search
Duplicate
📗

귀찮음

주차
문제번호
16208
언어
C++
티어
실버
유형
수학
그리디
정렬
nj_Blog
nj_상태
이해도
100%
풀이
사람
이해도 2
13 more properties

문제접근

최소 * 최대들을 합했을때 가장 최소의 결과가 나옴
입력받은 값들을 정렬하고 앞에서부터 하나씩 자라내면서 값을 구해나감

놓쳤던 부분

코드

3924 KB

48 ms

#include <iostream> #include <vector> #include <algorithm> using namespace std; int main(void) { int n; vector<int> stick; int totalLength = 0; long long answer = 0; ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; stick.resize(n); for (int i = 0; i < n; i++) { cin >> stick[i]; totalLength += stick[i]; } sort(stick.begin(), stick.end()); for (int i = 0; i < n - 1; i++) { totalLength -= stick[i]; answer += stick[i] * totalLength; } cout << answer; return (0); }
C++
복사