-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
37 lines (31 loc) · 1019 Bytes
/
Copy pathMain.java
File metadata and controls
37 lines (31 loc) · 1019 Bytes
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
// GIVEN
var br = new BufferedReader(new InputStreamReader(System.in));
var st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int[][] graph = new int[n+1][m+1];
for (int i = 1; i <= n; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 1; j <= m; j++) {
graph[i][j] = Integer.parseInt(st.nextToken());
}
}
// WHEN
int[][] cache = new int[n+1][m+1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cache[i][j] = Math.max(cache[i][j], cache[i-1][j-1] + graph[i][j]);
cache[i][j] = Math.max(cache[i][j], cache[i][j-1] + graph[i][j]);
cache[i][j] = Math.max(cache[i][j], cache[i-1][j] + graph[i][j]);
}
}
// THEN
System.out.println(cache[n][m]);
}
}