given a 2d array NxN. You can get N^2 values by adding elements from each row. Find the smallest N of these sums. you are allowed to use only one element from the row each time.
#include <bits/stdc++.h>
#include <iostream>
usingnamespace std;
int main(){
long n;
cin>> n;
long a[n][n] , d[n], r[n];
long s = 0;
long mins = 0;
for(int i = 0 ; i < n ; i++){
for(int j = 0 ; j < n ; j++){
cin>>a[i][j];
}
}
for(int i = 0 ; i < n ; i++){
sort(a[i], a[i] + n);
}
for(int i = 0 ; i < n ; i++){
d[i] = a[i][1] - a[i][0];
}int k = sizeof(d)/sizeof(d[0]);
sort(d, d + k);
for(int i = 0 ; i < n ; i++){
mins = mins + a[i][0];
}r[0] = mins;
for(int i = 1 ; i < n ; i++){
r[i]= mins + d[i-1];
}
for(int i = 0 ; i < n ; i++){
cout<<r[i]<<' ';
}
return 0;
}
what do I need to improve or use?