c++10分,新手看不懂题解,求指点
查看原帖
c++10分,新手看不懂题解,求指点
1070752
a13901280570楼主2024/1/19 23:25
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool cmp (long long x, long long y) {
	return x < y;
} 
int main() {
	long long n;
	cin >> n;
	vector<long long> a(n);
	for (long long i = 0; i < n; i++) {
		cin >> a[i];
	}
	sort (a.begin(), a.end(), cmp);
    long long s = 0;
    long long z = 0;

    for (long long i = 0; i < n - 1; i++) {
        z = a[i] + a[i + 1];
        s += z;
        a[i + 1] = z;
    }

    cout << s << endl;
	return 0;
}
2024/1/19 23:25
加载中...