CF1430F.Realistic Gameplay

省选/NOI-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Recently you've discovered a new shooter. They say it has realistic game mechanics.

Your character has a gun with magazine size equal to kk and should exterminate nn waves of monsters. The ii-th wave consists of aia_i monsters and happens from the lil_i-th moment of time up to the rir_i-th moments of time. All aia_i monsters spawn at moment lil_i and you have to exterminate all of them before the moment rir_i ends (you can kill monsters right at moment rir_i). For every two consecutive waves, the second wave starts not earlier than the first wave ends (though the second wave can start at the same moment when the first wave ends) — formally, the condition ri≤li+1r_i \le l_{i + 1} holds. Take a look at the notes for the examples to understand the process better.

You are confident in yours and your character's skills so you can assume that aiming and shooting are instant and you need exactly one bullet to kill one monster. But reloading takes exactly 11 unit of time.

One of the realistic mechanics is a mechanic of reloading: when you reload you throw away the old magazine with all remaining bullets in it. That's why constant reloads may cost you excessive amounts of spent bullets.

You've taken a liking to this mechanic so now you are wondering: what is the minimum possible number of bullets you need to spend (both used and thrown) to exterminate all waves.

Note that you don't throw the remaining bullets away after eradicating all monsters, and you start with a full magazine.

最近,你发现了一款新的射击游戏。据说它拥有逼真的游戏机制。

你的角色拥有一把弹匣容量为 kk 的枪,需要消灭 nn 波怪物。第 ii 波怪物包含 aia_i 只怪物,出现的时间段为从时刻 lil_i 到时刻 rir_i(含端点)。所有 aia_i 只怪物均在时刻 lil_i 同时生成,而你必须在时刻 rir_i 结束前将它们全部消灭(你可以在时刻 rir_i 精确地击杀怪物)。对于任意两波连续的怪物,后一波的开始时间不早于前一波的结束时间(但后一波可以恰好在前一波结束的同一时刻开始)——形式化地说,满足条件 ri≤li+1r_i \le l_{i + 1}。请参阅示例的注释以更清晰地理解整个过程。

你对自己的操作和角色能力充满信心,因此可假设瞄准与射击均为瞬时动作,且击杀一只怪物恰好需要一发子弹。但装填(reload)恰好耗时 11 单位时间。

其中一项逼真机制是装填机制:当你装填时,会丢弃当前弹匣中所有剩余的子弹。正因如此,频繁装填可能导致大量子弹被无谓浪费。

你十分喜爱这一机制,因此现在你想知道:为消灭所有怪物波次,所需消耗的子弹总数(包括实际击中怪物所用的子弹,以及装填时被丢弃的子弹)的最小可能值是多少?

注意:在消灭全部怪物后,你无需丢弃弹匣中剩余的子弹;初始时你的弹匣是满的。

输入格式

The first line contains two integers nn and kk (1≤n≤20001 \le n \le 2000; 1≤k≤1091 \le k \le 10^9) — the number of waves and magazine size.

The next nn lines contain descriptions of waves. The ii-th line contains three integers lil_i, rir_i and aia_i (1≤li≤ri≤1091 \le l_i \le r_i \le 10^9; 1≤ai≤1091 \le a_i \le 10^9) — the period of time when the ii-th wave happens and the number of monsters in it.

It's guaranteed that waves don't overlap (but may touch) and are given in the order they occur, i. e. ri≤li+1r_i \le l_{i + 1}.

第一行包含两个整数 nn 和 kk(1≤n≤20001 \le n \le 2000;1≤k≤1091 \le k \le 10^9)—— 分别表示波次数量和杂志容量。

接下来的 nn 行描述各波次。第 ii 行包含三个整数 lil_i、rir_i 和 aia_i(1≤li≤ri≤1091 \le l_i \le r_i \le 10^9;1≤ai≤1091 \le a_i \le 10^9)—— 分别表示第 ii 波发生的时间区间以及该波中的怪物数量。

保证各波次互不重叠(但可以相接),且按发生顺序给出,即满足 ri≤li+1r_i \le l_{i + 1}。

输出格式

If there is no way to clear all waves, print −1-1. Otherwise, print the minimum possible number of bullets you need to spend (both used and thrown) to clear all waves.

如果无法清除所有波次,则输出 −1-1。否则,输出清除所有波次所需的最少子弹数量(包括已使用和已丢弃的子弹)。

输入输出样例

  • 输入#1

    2 3
    2 3 6
    3 4 3

    输出#1

    9
  • 输入#2

    2 5
    3 7 11
    10 12 15

    输出#2

    30
  • 输入#3

    5 42
    42 42 42
    42 43 42
    43 44 42
    44 45 42
    45 45 1

    输出#3

    -1
  • 输入#4

    1 10
    100 111 1

    输出#4

    1

说明/提示

In the first example:

  • At the moment 22, the first wave occurs and 66 monsters spawn. You kill 33 monsters and start reloading.
  • At the moment 33, the second wave occurs and 33 more monsters spawn. You kill remaining 33 monsters from the first wave and start reloading.
  • At the moment 44, you kill remaining 33 monsters from the second wave.

In total, you'll spend 99 bullets.

In the second example:

  • At moment 33, the first wave occurs and 1111 monsters spawn. You kill 55 monsters and start reloading.
  • At moment 44, you kill 55 more monsters and start reloading.
  • At moment 55, you kill the last monster and start reloading throwing away old magazine with 44 bullets.
  • At moment 1010, the second wave occurs and 1515 monsters spawn. You kill 55 monsters and start reloading.
  • At moment 1111, you kill 55 more monsters and start reloading.
  • At moment 1212, you kill last 55 monsters.

In total, you'll spend 3030 bullets.

在第一个例子中:

  • 在时刻 22,第一波怪物来袭,生成 66 只怪物。你击杀其中 33 只,并开始换弹。
  • 在时刻 33,第二波怪物来袭,额外生成 33 只怪物。你击杀第一波剩余的 33 只怪物,并开始换弹。
  • 在时刻 44,你击杀第二波剩余的 33 只怪物。

总共,你将消耗 99 发子弹。

在第二个例子中:

  • 在时刻 33,第一波怪物来袭,生成 1111 只怪物。你击杀其中 55 只,并开始换弹。
  • 在时刻 44,你再击杀 55 只怪物,并开始换弹。
  • 在时刻 55,你击杀最后 11 只怪物,并开始换弹,此时丢弃装有 44 发子弹的旧弹匣。
  • 在时刻 1010,第二波怪物来袭,生成 1515 只怪物。你击杀其中 55 只,并开始换弹。
  • 在时刻 1111,你再击杀 55 只怪物,并开始换弹。
  • 在时刻 1212,你击杀最后 55 只怪物。

总共,你将消耗 3030 发子弹。

输入解题思路,AI测评打分。不知道怎么写?

首页