C. Rest Stops

http://codeforces.com/group/NVaJtLaLjS/contest/238204/problem/C

C. Rest Stops

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Farmer John and his personal trainer Bessie are hiking up Mount Vancowver. For their purposes (and yours), the mountain can be represented as a long straight trail of length LL meters (1≤L≤1061≤L≤106). Farmer John will hike the trail at a constant travel rate of rFrF seconds per meter (1≤rF≤1061≤rF≤106). Since he is working on his stamina, he will not take any rest stops along the way.

Bessie, however, is allowed to take rest stops, where she might find some tasty grass. Of course, she cannot stop just anywhere! There are NN rest stops along the trail (1≤N≤1051≤N≤105); the ii-th stop is xixi meters from the start of the trail (0<xi<L0<xi<L) and has a tastiness value cici(1≤ci≤1061≤ci≤106). If Bessie rests at stop ii for tt seconds, she receives ci⋅tci⋅t tastiness units.

When not at a rest stop, Bessie will be hiking at a fixed travel rate of rBrB seconds per meter (1≤rB≤1061≤rB≤106). Since Bessie is young and fit, rBrB is strictly less than rFrF.

Bessie would like to maximize her consumption of tasty grass. But she is worried about Farmer John; she thinks that if at any point along the hike she is behind Farmer John on the trail, he might lose all motivation to continue!

Help Bessie find the maximum total tastiness units she can obtain while making sure that Farmer John completes the hike.

Input

The first line of input contains four integers: LL, NN, rFrF, and rBrB. The next NN lines describe the rest stops. For each ii between 11 and NN, the i+1i+1-st line contains two integers xixi and cici, describing the position of the ii-th rest stop and the tastiness of the grass there.

It is guaranteed that rF>rBrF>rB, and 0<x1<...<xN<L0<x1<...<xN<L. Note that rFrF and rBrB are given in seconds per meter!

Output

A single integer: the maximum total tastiness units Bessie can obtain.

Example

input

Copy

10 2 4 3
7 2
8 1

output

Copy

15

Note

In this example, it is optimal for Bessie to stop for 77 seconds at the x=7x=7 rest stop (acquiring 1414 tastiness units) and then stop for an additional 11 second at the x=8x=8 rest stop (acquiring 11 more tastiness unit, for a total of 1515 tastiness units).

题意:F 和B爬山,F以rf分钟一米的速度不休息一直向上爬。B为rb。已知rf>rb.中途有休息站,站中有美味值为c的草,若B 

停留t分钟则收获t*c的美味值,求在一直领先F的情况下,B所能收获的最大美味值。

题目分析:

贪心。先按美味值的大排序,优先选大的。在看次大的,是否在当前位置的后面。

全部要开long long 。不开会出现问题。

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=100005;
typedef long long ll;
struct Node{
	ll x;
	long long c;
}a[N];
bool cmp(Node a,Node b){
	if(a.c==b.c)return a.x<b.x;
	return a.c>b.c;
}
int main()
{
	ll l,n,rf,rb;
	cin>>l>>n>>rf>>rb;
	for(int i=1;i<=n;i++)cin>>a[i].x>>a[i].c;
	sort(a+1,a+1+n,cmp);
	int i,t=1;long long ans=0;
	ans+=(a[1].x)*(rf-rb)*a[1].c;
	for(i=2;i<=n;i++){
		if(a[i].x>a[t].x){
			ans+=(a[i].x-a[t].x)*(rf-rb)*a[i].c;
			t=i;
		}
	}
	cout<<ans;
} 

猜你喜欢

转载自blog.csdn.net/qq_43490894/article/details/87693303
今日推荐