-
Notifications
You must be signed in to change notification settings - Fork 0
/
MAXSC.c
65 lines (59 loc) · 1.04 KB
/
MAXSC.c
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<stdio.h>
#include<stdlib.h>
// C function to find maximum in arr[] of size n
long long largest(long long arr[], int n, long long l)
{
int i;
long long max=-1;
// Initialize maximum element
for(i=0;i<n;i++)
{
if(arr[i]<l)
max=arr[i];
}
if(max>-1)
{
// Traverse array elements from second and
// compare every element with current max
for (i = 1; i < n; i++)
if (arr[i] > max && arr[i]<l)
max = arr[i];
}
return max;
}
int main()
{
int T=0;
long long prev=1000000000,temp=0;
scanf("%d",&T);
while(T--)
{
int N=0;
scanf("%d",&N);
int i,j;
long long A[N][N];
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
scanf("%lld",&A[i][j]);
}
}
long long sum=0;
for(i=N-1;i>=0;i--)
{
//printf("Largest = %lld and Previous = %lld\n",temp,prev);
temp = largest(A[i],N,prev);
if(temp==-1)
{
printf("-1\n");
break;
}
prev = temp;
sum += temp;
}
if(temp>-1)
printf("%lld\n",sum);
}
return 0;
}