문제접근
•
걸리는 시간이 짧을수록 전체적인 시간이 짧아짐
놓쳤던 부분
코드
2016 KB
0 ms
#include <iostream>
#include <algorithm>
#include <vector>
int n;
std::vector<int> line;
void input_setting()
{
std::ios_base::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
}
void input()
{
std::cin >> n;
line.resize(n);
for (int i = 0; i < n; i++)
std::cin >> line[i];
}
void solution()
{
int sum;
sum = 0;
std::sort(line.begin(), line.end());
for (int i = 0; i < n; i++)
sum += line[i] * (n - i);
std::cout << sum;
}
int main(void)
{
input_setting();
input();
solution();
return (0);
}
C++
복사
2020 KB 0ms
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(void)
{
int n;
int input;
vector<int> arr;
int sum = 0;
int answer = 0;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> input;
arr.push_back(input);
}
sort(arr.begin(), arr.end());
for (int i = 0; i < n; i++)
{
sum = sum + arr[i];
answer += sum;
}
cout << answer;
return (0);
}
C++
복사