This repository was archived by the owner on Mar 11, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathbytesm2.java
More file actions
50 lines (44 loc) · 1.46 KB
/
bytesm2.java
File metadata and controls
50 lines (44 loc) · 1.46 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
import java.io.*;
import java.util.*;
import static java.lang.Integer.parseInt;
public class bytesm2
{
private static BufferedReader f;
public static void main(String[] args) throws IOException {
f = new BufferedReader(new InputStreamReader(System.in));
int T = parseInt(f.readLine());
int w, h, i, j, max; StringTokenizer st;
int[][] grid;
while(T-->0)
{
st = new StringTokenizer(f.readLine());
w = parseInt(st.nextToken());
h = parseInt(st.nextToken());
grid = new int[w][h+2];
for(i = 0; i < w; i++)
{
for(j = 1; j <= h; j++)
{
if(!st.hasMoreTokens()) st = new StringTokenizer(f.readLine());
grid[i][j] = parseInt(st.nextToken());
}
grid[i][0] = grid[i][h+1] = -100000000;
}
for(i = 1; i < w; i++)
for(j = 1; j <= h; j++)
grid[i][j] += max(grid[i-1][j-1], grid[i-1][j], grid[i-1][j+1]);
max = 0;
for(j = 1; j <= h; j++)
max = max(max, grid[w-1][j]);
System.out.println(max);
System.out.flush();
}
System.exit(0);
}
private static int max(int... x)
{
int max = -Integer.MAX_VALUE;
for(int i : x) if(max < i) max = i;
return max;
}
}