CF797F.Mice and Holes

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

One day Masha came home and noticed nn 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 nn mice and mm holes on it. ii th mouse is at the coordinate xix_{i} , and jj th hole — at coordinate pjp_{j} . jj th hole has enough room for cjc_{j} mice, so not more than cjc_{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 ii th mouse goes to the hole jj , then its distance is xipj|x_{i}-p_{j}| .

Print the minimum sum of distances.

输入格式

The first line contains two integer numbers nn , mm ( 1<=n,m<=50001<=n,m<=5000 ) — the number of mice and the number of holes, respectively.

The second line contains nn integers x1,x2,...,xnx_{1},x_{2},...,x_{n} ( 109<=xi<=109-10^{9}<=x_{i}<=10^{9} ), where xix_{i} is the coordinate of ii th mouse.

Next mm lines contain pairs of integer numbers pj,cjp_{j},c_{j} ( 109<=pj<=109,1<=cj<=5000-10^{9}<=p_{j}<=10^{9},1<=c_{j}<=5000 ), where pjp_{j} is the coordinate of jj th hole, and cjc_{j} is the maximum number of mice that can hide in the hole jj .

输出格式

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
    
首页