list($days, $cost) = explode(" ", trim(fgets(STDIN))); $salary = 0; $now_plase = ""; for ($i=1; $i<=$days; $i++) { list($tokyo, $kyoto) = explode(" ", trim(fgets(STDIN))); // 最初は東京に居る if ($i == 1) { $tokyo_salary = $tokyo; $kyoto_salary = $kyoto - $cost; } else { if ($now_plase == 'tokyo') { $tokyo_salary = $tokyo; $kyoto_salary = $kyoto - $cost; } elseif ($now_plase == 'kyoto') { $tokyo_salary = $tokyo - $cost; $kyoto_salary = $kyoto; } } $now_plase = ($tokyo_salary > $kyoto_salary) ? 'tokyo' : 'kyoto'; if ($tokyo_salary > $kyoto_salary) { $salary = $salary + $tokyo_salary; } elseif ($kyoto_salary > $tokyo_salary) { $salary = $salary + $kyoto_salary; } } echo $salary."\n";