F. Rain and Umbrellas(dp)

http://codeforces.com/problemset/problem/988/F

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Polycarp lives on a coordinate line at the point x=0x=0 . He goes to his friend that lives at the point x=ax=a . Polycarp can move only from left to right, he can pass one unit of length each second.

Now it's raining, so some segments of his way are in the rain. Formally, it's raining on nn non-intersecting segments, the ii -th segment which is in the rain is represented as [li,ri][li,ri] (0≤li<ri≤a0≤li<ri≤a ).

There are mm umbrellas lying on the line, the ii -th umbrella is located at point xixi (0≤xi≤a0≤xi≤a ) and has weight pipi . When Polycarp begins his journey, he doesn't have any umbrellas.

During his journey from x=0x=0 to x=ax=a Polycarp can pick up and throw away umbrellas. Polycarp picks up and throws down any umbrella instantly. He can carry any number of umbrellas at any moment of time. Because Polycarp doesn't want to get wet, he must carry at least one umbrella while he moves from xx to x+1x+1 if a segment [x,x+1][x,x+1] is in the rain (i.e. if there exists some ii such that li≤xli≤x and x+1≤rix+1≤ri ).

The condition above is the only requirement. For example, it is possible to go without any umbrellas to a point where some rain segment starts, pick up an umbrella at this point and move along with an umbrella. Polycarp can swap umbrellas while he is in the rain.

Each unit of length passed increases Polycarp's fatigue by the sum of the weights of umbrellas he carries while moving.

Can Polycarp make his way from point x=0x=0 to point x=ax=a ? If yes, find the minimum total fatigue after reaching x=ax=a , if Polycarp picks up and throws away umbrellas optimally.

Input

The first line contains three integers aa , nn and mm (1≤a,m≤2000,1≤n≤⌈a2⌉1≤a,m≤2000,1≤n≤⌈a2⌉ ) — the point at which Polycarp's friend lives, the number of the segments in the rain and the number of umbrellas.

Each of the next nn lines contains two integers lili and riri (0≤li<ri≤a0≤li<ri≤a ) — the borders of the ii -th segment under rain. It is guaranteed that there is no pair of intersecting segments. In other words, for each pair of segments ii and jj either ri<ljri<lj or rj<lirj<li .

Each of the next mm lines contains two integers xixi and pipi (0≤xi≤a0≤xi≤a , 1≤pi≤1051≤pi≤105 ) — the location and the weight of the ii -th umbrella.

Output

Print "-1" (without quotes) if Polycarp can't make his way from point x=0x=0 to point x=ax=a . Otherwise print one integer — the minimum total fatigue after reaching x=ax=a , if Polycarp picks up and throws away umbrellas optimally.

Examples

Input

Copy

10 2 4
3 7
8 10
0 10
3 4
8 1
1 2

Output

Copy

14

Input

Copy

10 1 1
0 9
0 5

Output

Copy

45

Input

Copy

10 1 1
0 9
1 5

Output

Copy

-1

Note

In the first example the only possible strategy is to take the fourth umbrella at the point x=1x=1 , keep it till the point x=7x=7 (the total fatigue at x=7x=7 will be equal to 1212 ), throw it away, move on from x=7x=7 to x=8x=8 without an umbrella, take the third umbrella at x=8x=8 and keep it till the end (the total fatigue at x=10x=10 will be equal to 1414 ).

In the second example the only possible strategy is to take the first umbrella, move with it till the point x=9x=9 , throw it away and proceed without an umbrella till the end.

-------------------------------------------------------------------------

动态规划,dp【i】记录到i点的最少体力,dp【a】就是答案。

如果不下雨,dp【i】=dp【i-1】;

下雨的话(i之前的j点有伞),dp【i】=dp【j】+(i-j)*weight;把i点前所有的伞(j点)都要扫一遍。记录dp【j】值;

---------------------------------------------------------------------------

#include<deque>
#include<queue>  
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<map>
#include<stack>
const int inf = 0x3f3f3f3f;
using namespace std;
int umb[2005],dp[2005];
bool rain[2005];
int main()
{
	int a,n,m;
	scanf("%d%d%d",&a,&n,&m);
	for(int i=0;i<=2004;i++)
	{
		umb[i]=inf;
		dp[i]=inf;
		rain[i]=false;
	}
	dp[0]=0;
	for(int i=1;i<=n;i++)
	{
		int l,r;
		scanf("%d%d",&l,&r);
		for(int j=l;j<r;j++)
			rain[j]=true;
	}
	for(int i=1;i<=m;i++)
	{
		int t,w;
		scanf("%d",&t);
		scanf("%d",&w);
		umb[t]=min(umb[t],w);
	}
	for(int i=1;i<=a;i++)
	{
		if(!rain[i-1])
			dp[i]=dp[i-1];
		else
		{
			for(int j=i-1;j>=0;j--)
			{
				if(umb[j]!=inf)
					dp[i]=min(dp[i],dp[j]+(i-j)*umb[j]);
			}
		}
	}
	if(dp[a]>=inf)
		printf("-1\n");
	else
		printf("%d\n",dp[a]);
	
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41539869/article/details/81111827