-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJAN18MAXSC.java
More file actions
67 lines (59 loc) · 1.9 KB
/
Copy pathJAN18MAXSC.java
File metadata and controls
67 lines (59 loc) · 1.9 KB
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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author rn-sshawish
*/
public class JAN18MAXSC {
public static int max = -1;
public static int size;
public static void main(String[] args) throws Exception {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(input.readLine());
int[][] data;
StringTokenizer tokn;
for (int i = 0; i < n; i++) {
size = Integer.parseInt(input.readLine());
max = -1;
data = new int[size][size];
for (int k = 0; k < size; k++) {
tokn = new StringTokenizer(input.readLine());
for (int j = 0; j < size; j++) {
data[k][j] = Integer.parseInt(tokn.nextToken());
}
Arrays.sort(data[k]);
}
findMaxSum(data, 0, 0, 0);
System.out.println(max);
}
}
public static boolean findMaxSum(int[][] data, int x, int previousValue, int sum) {
if (x >= size) {
max = sum;
return true;
}
for (int i = size - 1; i > -1; i--) {
if (x == 0) {
if (findMaxSum(data, x + 1, data[x][i], sum + data[x][i])) {
return true;
}
} else {
if (data[x][i] > previousValue) {
if (findMaxSum(data, x + 1, data[x][i], sum + data[x][i])) {
return true;
}
}else{
return false;
}
}
}
return false;
}
}