Help me pls

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.
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
 #include <bits/stdc++.h> 
#include <iostream>
using namespace 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?
As I said in the other post, you can do this in C++.
1
2
3
    long n;
    cin>> n;
    long a[n][n] , d[n], r[n];
kbw wrote:
As I said in the other post, you can do this in C++.

Are you sure you can create a dynamic array in C++ at run-time? Your comment in the other post says correctly it can't be done.
It was a silly addition to C99 using alloca() to do the allocation off the stack. C++ wisely hasn't adopted this.
Topic archived. No new replies allowed.