CF797F.Mice and Holes

省选/NOI-

通过率:0%

时间限制:1.50s

内存限制:256MB

AC君温馨提醒

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

题目描述

One day Masha came home and noticed n mice in the corridor of her flat. Of course, she shouted loudly, so scared mice started to run to the holes in the corridor.

The corridor can be represeted as a numeric axis with n mice and m holes on it. _i_th mouse is at the coordinate x__i, and _j_th hole — at coordinate p__j. _j_th hole has enough room for c__j mice, so not more than c__j mice can enter this hole.

What is the minimum sum of distances that mice have to go through so that they all can hide in the holes? If _i_th mouse goes to the hole j, then its distance is |x__i - p__j|.

Print the minimum sum of distances.

一天,玛莎回到家,发现公寓走廊里有 n 只老鼠。当然,她大声喊叫,受惊的老鼠们立刻开始朝走廊里的洞穴奔逃。

走廊可建模为一条数轴,其上有 n 只老鼠和 m 个洞穴。第 i 只老鼠位于坐标 x__i 处,第 j 个洞穴位于坐标 p__j 处。第 j 个洞穴最多可容纳 c__j 只老鼠,即至多 c__j 只老鼠能进入该洞穴。

所有老鼠全部躲进洞穴所需经过的最小总距离是多少?若第 i 只老鼠进入第 j 个洞穴,则它所走的距离为 |x__i - p__j|。

请输出该最小总距离。

输入格式

The first line contains two integer numbers n, m (1 ≤ n, m ≤ 5000) — the number of mice and the number of holes, respectively.

The second line contains n integers _x_1, _x_2, ..., x__n ( - 109 ≤ x__i ≤ 109), where x__i is the coordinate of _i_th mouse.

Next m lines contain pairs of integer numbers p__j, c__j ( - 109 ≤ p__j ≤ 109, 1 ≤ c__j ≤ 5000), where p__j is the coordinate of _j_th hole, and c__j is the maximum number of mice that can hide in the hole j.

第一行包含两个整数 nn、mm(1≤n,m≤50001 \leq n, m \leq 5000),分别表示老鼠的数量和洞穴的数量。

第二行包含 nn 个整数 x1, x2, …, xnx_1,\ x_2,\ \dots,\ x_n(−109≤xi≤109-10^9 \leq x_i \leq 10^9),其中 xix_i 表示第 ii 只老鼠的坐标。

接下来的 mm 行每行包含一对整数 pj, cjp_j,\ c_j(−109≤pj≤109-10^9 \leq p_j \leq 10^9,1≤cj≤50001 \leq c_j \leq 5000),其中 pjp_j 表示第 jj 个洞穴的坐标,cjc_j 表示第 jj 个洞穴最多能容纳的老鼠数量。

输出格式

Print one integer number — the minimum sum of distances. If there is no solution, print -1 instead.

输出一个整数——距离之和的最小值。若无解,则输出 -1。

输入输出样例

  • 输入#1

    4 5
    6 2 8 9
    3 6
    2 1
    3 6
    4 7
    4 7

    输出#1

    11
  • 输入#2

    7 2
    10 20 30 40 50 45 35
    -1000000000 10
    1000000000 1

    输出#2

    7000000130

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

首页