結果
| 問題 | 
                            No.561 東京と京都
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2017-09-20 06:49:19 | 
| 言語 | Java  (openjdk 23)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 174 ms / 2,000 ms | 
| コード長 | 1,161 bytes | 
| コンパイル時間 | 5,186 ms | 
| コンパイル使用メモリ | 88,032 KB | 
| 実行使用メモリ | 41,660 KB | 
| 最終ジャッジ日時 | 2024-11-08 02:57:05 | 
| 合計ジャッジ時間 | 8,010 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 17 | 
ソースコード
package yukicoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Scanner;
public class N561 {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int d = sc.nextInt();
		int i, a, b, x;
		ArrayList<Integer> works = new ArrayList<Integer>();
		LinkedList<Integer> visited = new LinkedList<Integer>();
		LinkedList<Integer> queue = new LinkedList<Integer>();
		for(i = 0;i < n;++i){
			works.add(sc.nextInt());
			works.add(sc.nextInt());
			visited.add(0);
			visited.add(0);
		}
		queue.add(0);
		queue.add(1);
		visited.set(0, works.get(0));
		visited.set(1, works.get(1) - d);
		while(queue.size() > 0){
			i = queue.poll();
			x = visited.get(i);
			
			if(i % 2 == 0){
				a = i + 3;
				b = i + 2;
			}else{
				a = i + 1;
				b = i + 2;
			}
			
			if(a < n*2 && works.get(a) + x - d> visited.get(a)){
				visited.set(a, works.get(a) + x - d);
				queue.add(a);
			}
			if(b < n*2 && works.get(b) + x > visited.get(b)){
				visited.set(b, works.get(b) + x);
				queue.add(b);
			}
		}
		
		System.out.println(Collections.max(visited));
	}
}