#include<iostream>
#include<algorithm>
using namespace std;
int** readCij(int** c, int n) {
for(int i = 0; i < n; i++) {
int k = n-i;
c[i] = new int[k];
for(int j = 0; j < k; j++) {
cin >> c[i][j];
}
}
return c;
}
void deleteC(int** c, int n) {
for(int i = 0; i < n; i++) {
delete [] c[i];
}
delete[] c;
}
int main() {
int n;
cin >> n;
int** c = readCij(new int*[n], n);
int s[2*n-1];
int bound = 0;
for(int j = n-1 ; j > -1; j--) {
const int k = n-j;
for(int i = 0; i < k; i++) {
s[bound++] = c[i][j];
}
sort(s, s + bound);
bound = k;
}
long sum = 0;
for(int i = 0; i < n; i++) {
sum += s[i];
}
cout << sum << endl;
deleteC(c, n);
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #include<iostream> #include<algorithm> using namespace std; int** readCij(int** c, int n) { for(int i = 0; i < n; i++) { int k = n-i; c[i] = new int[k]; for(int j = 0; j < k; j++) { cin >> c[i][j]; } } return c; } void deleteC(int** c, int n) { for(int i = 0; i < n; i++) { delete [] c[i]; } delete[] c; } int main() { int n; cin >> n; int** c = readCij(new int*[n], n); int s[2*n-1]; int bound = 0; for(int j = n-1 ; j > -1; j--) { const int k = n-j; for(int i = 0; i < k; i++) { s[bound++] = c[i][j]; } sort(s, s + bound); bound = k; } long sum = 0; for(int i = 0; i < n; i++) { sum += s[i]; } cout << sum << endl; deleteC(c, n); } |
English