#include #include #include using namespace std; int main() { int N, D; scanf("%d%d", &N, &D); vector T, K; for (int i = 0; i < N; ++i) { int ti, ki; scanf("%d%d", &ti, &ki); T.push_back(ti); K.push_back(ki); } string current_location = "Tokyo"; int real_tokyo_payment; int real_kyoto_payment; int salary = 0; for (int i = 0; i < N; ++i) { real_tokyo_payment = current_location == "Tokyo" ? T[i] : T[i] - D; real_kyoto_payment = current_location == "Kyoto" ? K[i] : K[i] - D; if (real_tokyo_payment >= real_kyoto_payment) { salary += real_tokyo_payment; current_location = "Tokyo"; } else { salary += real_kyoto_payment; current_location = "Kyoto"; } } printf("%d\n", salary); }