-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA21.php
More file actions
36 lines (30 loc) · 996 Bytes
/
Copy pathA21.php
File metadata and controls
36 lines (30 loc) · 996 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
<?php
// 入力の読み込み
fscanf(STDIN, "%d", $N);
$PA = [];
for ($i = 0; $i < $N; $i++) {
fscanf(STDIN, "%d %d", $P, $A);
$PA[] = [$P - 1, $A]; // 0-index に変換
}
// DPテーブルの作成
$dp = array_fill(0, $N, array_fill(0, $N, 0));
// 区間 DP の計算
for ($di = 0; $di < $N; $di++) { // 区間の長さを増やしていく
for ($i = 0; $i + $di < $N; $i++) {
$j = $i + $di;
// 左端のブロックを削除する場合
if ($i > 0) {
[$pl, $al] = $PA[$i - 1];
$scoreL = ($i <= $pl && $pl <= $j) ? $al : 0;
$dp[$i - 1][$j] = max($dp[$i - 1][$j], $dp[$i][$j] + $scoreL);
}
// 右端のブロックを削除する場合
if ($j < $N - 1) {
[$pr, $ar] = $PA[$j + 1];
$scoreR = ($i <= $pr && $pr <= $j) ? $ar : 0;
$dp[$i][$j + 1] = max($dp[$i][$j + 1], $dp[$i][$j] + $scoreR);
}
}
}
// 結果を出力
echo $dp[0][$N - 1] . PHP_EOL;