-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnumtri.java
More file actions
72 lines (66 loc) · 1.76 KB
/
numtri.java
File metadata and controls
72 lines (66 loc) · 1.76 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
66
67
68
69
70
71
72
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
/*
ID: mryuan01
LANG: JAVA
TASK: numtri
*/
public class numtri {
public static int[][] numT = null;
public static int[][] result = null;
public static void dp(){
for(int i = 0; i < numT.length; i++){
for(int j = 0; j < numT[i].length; j++){
int max = Integer.MIN_VALUE;
if(i - 1 < 0){
max = 0;
}else if(j - 1 < 0){
max = result[i - 1][j];
}else if(j > i - 1){
max = result[i - 1][j - 1];
}else{
if(result[i - 1][j] > result[i - 1][j - 1])
max = result[i - 1][j];
else
max = result[i - 1][j - 1];
}
result[i][j] = numT[i][j] + max;
}
}
}
static int collectOutput(){
int max = Integer.MIN_VALUE;
for(int i = 0; i < result[result.length - 1].length; i++){
if(max < result[result.length - 1][i])
max = result[result.length - 1][i];
}
return max;
}
public static void main(String[] args) throws IOException {
BufferedReader f = new BufferedReader(new FileReader("numtri.in"));
// input file name goes above
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("numtri.out")));
int len = Integer.parseInt(f.readLine().trim());
numT = new int[len][];
result = new int[len][];
for(int i = 0; i < len; i++){
numT[i] = new int[i+1];
result[i] = new int[i+1];
StringTokenizer st = new StringTokenizer(f.readLine().trim());
for(int j = 0; j <= i; j++){
numT[i][j] = Integer.parseInt(st.nextToken());
}
}
dp();
int ret = collectOutput();
out.println(ret);
out.close();
}
}