A10910 | Mice and Holes
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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.
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.
输入格式
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}$ ( $-10^{9}<=x_{i}<=10^{9}$ ), where $x_{i}$ is the coordinate of $i$ th mouse.
Next $m$ lines contain pairs of integer numbers $p_{j},c_{j}$ ( $-10^{9}<=p_{j}<=10^{9},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$ .
The second line contains $n$ integers $x_{1},x_{2},...,x_{n}$ ( $-10^{9}<=x_{i}<=10^{9}$ ), where $x_{i}$ is the coordinate of $i$ th mouse.
Next $m$ lines contain pairs of integer numbers $p_{j},c_{j}$ ( $-10^{9}<=p_{j}<=10^{9},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$ .
输出格式
Print one integer number — the minimum sum of distances. If there is no solution, print -1 instead.
输入输出样例
输入 #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
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted