Nội dung Bài tập
Mã:
ACM2017_WF_D
Tên:
Money for Nothing (ACM-ICPC World Finals 2017)
Dạng thi:
oi
Thang điểm:
10 điểm
Giới hạn thời gian:
7 giây
Giới hạn bộ nhớ:
1024 MB
Được tạo bởi:
admin

In this problem you will be solving one of the most profound challenges of humans across the world since the beginning of time – how to make lots of money.

You are a middleman in the widget market. Your job is to buy widgets from widget producer companies and sell them to widget consumer companies. Each widget consumer company has an open request for one widget per day, until some end date, and a price at which it is willing to buy the widgets. On the other hand, each widget producer company has a start date at which it can start delivering widgets and a price at which it will deliver each widget.

Due to fair competition laws, you can sign a contract with only one producer company and only one consumer company. You will buy widgets from the producer company, one per day, starting on the day it can start delivering, and ending on the date specified by the consumer company. On each of those days you earn the difference between the producer’s selling price and the consumer’s buying price.

Your goal is to choose the consumer company and the producer company that will maximize your profits.

Input

The first line of input contains two integers and (1m,n500000) denoting the number of producer and consumer companies in the market, respectively. It is followed by lines, the ith  of which contains two integers pand d(1≤ pi,di109), the price (in dollars) at which the ith  producer sells one widget and the day on which the first widget will be available from this company. Then follow lines, the jth of which contains two integers qand e(1qj,ej109), the price (in dollars) at which the jth consumer is willing to buy widgets and the day immediately after the day on which the last widget has to be delivered to this company.

Output

Display the maximum total number of dollars you can earn. If there is no way to sign contracts that gives you any profit, display 0.

Sample 1

InputOutput
2 2
1 3
2 1
3 5
7 2
5


Sample 2

InputOutput
1 2
10 10
9 11
11 9
0



    Quảng cáo
       Ngôn ngữ : 

       Theme : 
Mời bạn soạn code



		



      Ai có thể xem bài này : 

Thông tin



Phần thảo luận