import sys, heapq
input=sys.stdin.readline
INF = int(1e9)
n = int(input())
maps = []
maps.append([INF]*(n+1))
#배열 입력
for i in range(n):
tmp = [INF]+list(map(int,input().split()))
maps.append(tmp)
#비용 초기화
cost = [[0]*(n+1) for _ in range(n+1)]
cost[1][1]=0
#디피
for i in range(1,n+1):
for j in range(1,n+1):
if j-1==0 and i-1==0:
continue
elif j-1==0:
button2 = cost[i-1][j] if maps[i][j]<maps[i-1][j] else maps[i][j]-maps[i-1][j] + 1 + cost[i-1][j]
cost[i][j]+=button2
elif i-1==0:
button1 = cost[i][j-1] if maps[i][j]<maps[i][j-1] else maps[i][j]-maps[i][j-1] + 1 + cost[i][j-1]
cost[i][j]+=button1
else:
button1 = cost[i][j-1] if maps[i][j]<maps[i][j-1] else maps[i][j]-maps[i][j-1] + 1 + cost[i][j-1]
button2 = cost[i-1][j] if maps[i][j]<maps[i-1][j] else maps[i][j]-maps[i-1][j] + 1 + cost[i-1][j]
cost[i][j]+=min(button1,button2)
print(cost[n][n])
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
2924 KB |
Output is correct |
2 |
Correct |
17 ms |
2924 KB |
Output is correct |
3 |
Correct |
19 ms |
2924 KB |
Output is correct |
4 |
Correct |
17 ms |
2924 KB |
Output is correct |
5 |
Correct |
17 ms |
2924 KB |
Output is correct |
6 |
Correct |
17 ms |
2924 KB |
Output is correct |
7 |
Correct |
18 ms |
2924 KB |
Output is correct |
8 |
Correct |
17 ms |
2924 KB |
Output is correct |
9 |
Correct |
17 ms |
2924 KB |
Output is correct |
10 |
Correct |
17 ms |
2924 KB |
Output is correct |
11 |
Correct |
17 ms |
2924 KB |
Output is correct |
12 |
Correct |
17 ms |
2924 KB |
Output is correct |
13 |
Correct |
16 ms |
2924 KB |
Output is correct |
14 |
Correct |
17 ms |
2924 KB |
Output is correct |
15 |
Correct |
17 ms |
3072 KB |
Output is correct |
16 |
Correct |
17 ms |
2924 KB |
Output is correct |
17 |
Correct |
17 ms |
2924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
3200 KB |
Output is correct |
2 |
Correct |
18 ms |
2924 KB |
Output is correct |
3 |
Correct |
17 ms |
2924 KB |
Output is correct |
4 |
Correct |
17 ms |
2924 KB |
Output is correct |
5 |
Correct |
17 ms |
2924 KB |
Output is correct |
6 |
Correct |
18 ms |
2968 KB |
Output is correct |
7 |
Correct |
17 ms |
2924 KB |
Output is correct |
8 |
Correct |
24 ms |
2924 KB |
Output is correct |
9 |
Correct |
23 ms |
2924 KB |
Output is correct |
10 |
Correct |
17 ms |
2924 KB |
Output is correct |
11 |
Correct |
17 ms |
2924 KB |
Output is correct |
12 |
Correct |
17 ms |
2924 KB |
Output is correct |
13 |
Correct |
17 ms |
2924 KB |
Output is correct |
14 |
Correct |
17 ms |
2924 KB |
Output is correct |
15 |
Correct |
22 ms |
3072 KB |
Output is correct |
16 |
Correct |
17 ms |
2924 KB |
Output is correct |
17 |
Correct |
17 ms |
2924 KB |
Output is correct |
18 |
Correct |
17 ms |
2924 KB |
Output is correct |
19 |
Correct |
17 ms |
2924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
133 ms |
5356 KB |
Output is correct |
2 |
Correct |
142 ms |
5356 KB |
Output is correct |
3 |
Correct |
139 ms |
5344 KB |
Output is correct |
4 |
Correct |
137 ms |
5356 KB |
Output is correct |
5 |
Correct |
144 ms |
5484 KB |
Output is correct |
6 |
Correct |
137 ms |
5356 KB |
Output is correct |
7 |
Correct |
134 ms |
5356 KB |
Output is correct |
8 |
Correct |
140 ms |
5588 KB |
Output is correct |
9 |
Correct |
130 ms |
5356 KB |
Output is correct |
10 |
Correct |
141 ms |
5356 KB |
Output is correct |
11 |
Correct |
134 ms |
5356 KB |
Output is correct |
12 |
Correct |
134 ms |
5356 KB |
Output is correct |
13 |
Correct |
136 ms |
5356 KB |
Output is correct |
14 |
Correct |
131 ms |
5228 KB |
Output is correct |
15 |
Correct |
114 ms |
4716 KB |
Output is correct |
16 |
Correct |
148 ms |
5412 KB |
Output is correct |
17 |
Correct |
150 ms |
5356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1152 ms |
65540 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |