CF1238G.Adilbek and the Watering System

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Adilbek has to water his garden. He is going to do it with the help of a complex watering system: he only has to deliver water to it, and the mechanisms will do all the remaining job.

The watering system consumes one liter of water per minute (if there is no water, it is not working). It can hold no more than cc liters. Adilbek has already poured c0c_0 liters of water into the system. He is going to start watering the garden right now and water it for mm minutes, and the watering system should contain at least one liter of water at the beginning of the ii-th minute (for every ii from 00 to m−1m - 1).

Now Adilbek wonders what he will do if the watering system runs out of water. He called nn his friends and asked them if they are going to bring some water. The ii-th friend answered that he can bring no more than aia_i liters of water; he will arrive at the beginning of the tit_i-th minute and pour all the water he has into the system (if the system cannot hold such amount of water, the excess water is poured out); and then he will ask Adilbek to pay bib_i dollars for each liter of water he has brought. You may assume that if a friend arrives at the beginning of the tit_i-th minute and the system runs out of water at the beginning of the same minute, the friend pours his water fast enough so that the system does not stop working.

Of course, Adilbek does not want to pay his friends, but he has to water the garden. So he has to tell his friends how much water should they bring. Formally, Adilbek wants to choose nn integers k1k_1, k2k_2, ..., knk_n in such a way that:

  • if each friend ii brings exactly kik_i liters of water, then the watering system works during the whole time required to water the garden;
  • the sum ∑i=1nkibi\sum\limits_{i = 1}^{n} k_i b_i is minimum possible.

Help Adilbek to determine the minimum amount he has to pay his friends or determine that Adilbek not able to water the garden for mm minutes.

You have to answer qq independent queries.

阿迪尔别克需要给他的花园浇水。他将借助一套复杂的浇水系统来完成这项工作:他只需向系统供水,其余工作均由系统自动完成。

该浇水系统每分钟消耗 1 升水(若无水,则系统停止工作)。系统容量上限为 cc 升。阿迪尔别克已向系统中注入了 c0c_0 升水。他将立即开始浇水,并持续 mm 分钟;且对每个 ii(其中 ii 从 00 到 m−1m-1),在第 ii 分钟开始时,系统中必须至少剩余 1 升水。

现在阿迪尔别克担心浇水系统会中途缺水。他联系了 nn 位朋友,询问他们是否愿意送水。第 ii 位朋友答复称:他最多可运送 aia_i 升水;他将在第 tit_i 分钟开始时抵达,并将所带全部水量倒入系统(若系统剩余容量不足以容纳该水量,则超出部分被直接倒掉);随后他会向阿迪尔别克收取每升水 bib_i 美元的费用。你可以假设:若某位朋友在第 tit_i 分钟开始时抵达,而系统恰好也在同一时刻耗尽水量,则该朋友倒水速度足够快,确保系统不会因此中断工作。

显然,阿迪尔别克不愿向朋友们付钱,但他又必须完成花园浇水任务。因此,他需要告知每位朋友应运送多少水。形式化地,阿迪尔别克需选择 nn 个整数 k1,k2,…,knk_1, k_2, \dots, k_n,使得:

  • 若每位朋友 ii 恰好运送 kik_i 升水,则整个 mm 分钟浇水过程中,浇水系统始终正常工作;
  • 总费用 ∑i=1nkibi\sum\limits_{i = 1}^{n} k_i b_i 达到最小可能值。

请帮助阿迪尔别克计算他需向朋友们支付的最少金额;若无法保证系统连续工作 mm 分钟,请判定该情况。

你需要回答 qq 个相互独立的查询。

输入格式

The first line contains one integer qq (1≤q≤5⋅1051 \le q \le 5 \cdot 10^5) – the number of queries.

The first line of each query contains four integers n,m,cn, m, c and c0c_0 (0≤n≤5⋅105,2≤m≤109,1≤c0≤c≤1090 \le n \le 5 \cdot 10^5, 2 \le m \le 10^9, 1 \le c_0 \le c \le 10^9) — the number of friends, the number of minutes of watering, the capacity of the watering system and the number of liters poured by Adilbek.

Each of the next nn lines contains three integers ti,ai,bit_i, a_i, b_i ($ 0 \lt t_i \lt m, 1 \le a_i \le c, 1 \le b_i \le 10^9$) — the ii-th friend's arrival time, the maximum amount of water ii-th friend can bring and the cost of 11 liter from ii-th friend.

It is guaranteed that sum of all nn over all queries does not exceed 5⋅1055 \cdot 10^5.

第一行包含一个整数 qq(1≤q≤5⋅1051 \le q \le 5 \cdot 10^5),表示查询次数。

每个查询的第一行包含四个整数 n,m,cn, m, c 和 c0c_0(0≤n≤5⋅1050 \le n \le 5 \cdot 10^5, 2≤m≤1092 \le m \le 10^9, 1≤c0≤c≤1091 \le c_0 \le c \le 10^9),分别表示朋友人数、浇水总时长(分钟)、浇水系统的容量以及阿迪尔别克倒入的水量(升)。

接下来的 nn 行,每行包含三个整数 ti,ai,bit_i, a_i, b_i(0<ti<m0 \lt t_i \lt m, 1≤ai≤c1 \le a_i \le c, 1≤bi≤1091 \le b_i \le 10^9),表示第 ii 位朋友的到达时间、第 ii 位朋友最多能带来的水量(升)以及从第 ii 位朋友处获取 1 升水的花费。

保证所有查询中 nn 的总和不超过 5⋅1055 \cdot 10^5。

输出格式

For each query print one integer — the minimum amount Adilbek has to pay his friends, or −1-1 if Adilbek is not able to water the garden for mm minutes.

对于每个查询,输出一个整数——Adilbek 需要付给朋友们的最少金额;如果 Adilbek 无法让花园持续浇水 mm 分钟,则输出 −1-1。

输入输出样例

  • 输入#1

    4
    1 5 4 2
    2 4 2
    0 4 5 4
    2 5 3 1
    1 2 4
    3 1 3
    2 3 5 1
    2 1 1
    1 4 3

    输出#1

    6
    0
    -1
    4

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

首页