package yukicoder; import java.util.Random; import java.util.Scanner; public class Main { private static int[] tokyo; private static int[] kyoto; private static int days; private static int exp; public static void main(String[] args) { // 自分の得意な言語で // Let's チャレンジ!! Scanner sc = new Scanner(System.in); days = sc.nextInt(); exp = sc.nextInt(); tokyo = new int[days]; kyoto = new int[days]; for(int i = 0 ; i < days ; i++){ tokyo[i] = sc.nextInt(); kyoto[i] = sc.nextInt(); } int gain = greed(); int gain2 = random(); int maxGain = gain > gain2 ? gain : gain2; System.out.println(maxGain); } private static int random() { int num = 10000*1000/days; int pow[][] = new int[num][days]; for(int i = 0 ; i < num ; i++){ for(int j = 0 ; j < days ; j++){ Random r = new Random((int)(Math.random()*1000)); pow[i][j] = r.nextInt(2); } } int gain = 0; int maxGain = 0; boolean tokyoto = true; for(int i = 0 ; i < num ; i++){ gain = 0; tokyoto = true; for(int j = 0 ; j < days ; j++){ if(pow[i][j] == 0){ if(tokyoto){ gain += tokyo[j]; }else{ gain += tokyo[j] - exp; tokyoto = true; } }else{ if(tokyoto){ gain += kyoto[j] - exp; tokyoto = false; }else{ gain += kyoto[j]; } } } maxGain = gain > maxGain ? gain : maxGain; } return maxGain; } private static int greed() { int gain = 0; boolean tokyoto = true; for(int i = 0 ; i < days ; i++){ if(tokyoto){ if(tokyo[i] >= (kyoto[i]-exp)){ gain += tokyo[i]; }else{ tokyoto = false; gain += kyoto[i] - exp; } }else{ if(kyoto[i] >= (tokyo[i]-exp)){ gain += kyoto[i]; }else{ tokyoto = true; gain += tokyo[i] - exp; } } } return gain; } }