Description

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 L meters (1≤L≤10e6). Farmer John will hike the trail at a constant travel rate of rF seconds per meter (1≤rF≤10e6). 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 N rest stops along the trail (1≤N≤10e5); the ii-th stop is xi meters from the start of the trail (0<xi<L) and has a tastiness value ci(1≤ci≤10e6). If Bessie rests at stop i for t seconds, she receives ci⋅t tastiness units.

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

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: L, N, rF, and rB. The next N lines describe the rest stops. For each ii between 1 and N, the i+1-st line contains two integers xi and ci, describing the position of the ii-th rest stop and the tastiness of the grass there.

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

Output

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

Example

input

10 2 4 3
7 2
8 1

output

15

Note

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

解析

Bessie比较快,他需要在休息站吃东西,每个休息站每秒能吃的也不一样。

Farmer John比较慢,但是他不会在休息站停留

现在要求B先生吃的最大值。要保证B停留不会被F超过

因为吃东西是ci⋅t,所以把时间都留在ci最大的地方这样能吃最多!

所以将休息站排序,尽量吃大的。

代码

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
struct stop
{int first;//地点int second;//ci
}a[1000005];
bool cmp(stop &a,stop &b)
{return a.second>b.second;
}
int main()
{int L,N,rf,rb;scanf("%d%d%d%d",&L,&N,&rf,&rb);int i,last=0;long long ans=0;for(i=0;i<N;i++)scanf("%d %d",&a[i].first,&a[i].second);sort(a,a+N,cmp);    //休息站排序for(i=0;i<N;i++){if(a[i].first>last)//保证休息站在路上{ans+=(long long)(a[i].first-last)*(rf-rb)*a[i].second;last=a[i].first;}}printf("%I64d\n",ans);return 0;
}

[USACO18FEB] Rest Stops(贪心)相关推荐

  1. 【POJ - 3616】Milking Time (贪心+dp)

    题干: Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity tha ...

  2. 【CodeForces - 1038C】Gambling (博弈问题,优先队列模拟,贪心)

    题干: Two players A and B have a list of nn integers each. They both want to maximize the subtraction ...

  3. Rest Stops 题解

    题面: 翻译: 奶牛贝西和农夫一起上山. 山的高度一共是L米 农夫每走一米需要RF秒,贝西每走一米需要RB秒. 贝西走路速度一定是比农夫快的. 在这个山上有几个地点可以吃草 每个地点的高度xi和草的价 ...

  4. USACO 2018 February Contest, Silver-Rest Stops

    G: Rest Stops 时间限制: 1 Sec  内存限制: 128 MB 提交: 97  解决: 38 [提交][状态][讨论版][命题人:admin] 题目描述 Farmer John and ...

  5. 【LeetCode】871. Minimum Number of Refueling Stops 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心算法 日期 题目地址:https://leetc ...

  6. LeetCode 871. Minimum Number of Refueling Stops 最少加油次数

    LeetCode 871. Minimum Number of Refueling Stops 本题是LeetCode 871题,最少加油次数. 题目描述 A car travels from a s ...

  7. LeetCode(871):最低加油次数 Minimum Number of Refueling Stops(Java)

    2019.7.25 #程序员笔试必备# LeetCode 从零单刷个人笔记整理(持续更新) 这道智力题让我想起了LeetCode 871:加油站. 可以很容易能够想到用回溯法来做,每次经过加油站选择加 ...

  8. 【贪心算法】poj 2431: Expedition(最优加油方法)

    题目描述(传送门) Description A group of cows grabbed a truck and ventured on an expedition deep into the ju ...

  9. Gym - 101911B Glider 贪心

    A plane is flying at a constant height of hh meters above the ground surface. Let's consider that it ...

最新文章

  1. 哈佛大学惊人发现:新冠对人类未来影响巨大!
  2. magento建立robots.txt文件
  3. metadata model entry in /IWFND/CL_MED_MDL_SVC_GRP
  4. 总是忘记的oracle 函数
  5. 论文浅尝 \ 联合知识图谱实例和本体概念的通用表示学习
  6. 程序员你造吗?2016年加州将建造子弹头火车,你还在CODE?
  7. 升级更新:Oracle关于DB Link在2019年升级的10g版本兼容性
  8. mysql slave_mysql master slave
  9. YCbCr空间中进行色彩调整的方法
  10. 文字不间断横向滚动 代码 IE FireFox兼容代码
  11. 使用jdbc连接数据库的步骤
  12. mysql插入汉子1366_mysql 向列表中添加漢字錯誤 1366
  13. python爬虫验证码的处理(云打码)_python爬虫验证码的处理(云打码)
  14. JAVA 高频算法面试题汇总
  15. SQLExpress数据库类型与AttachDbFilename用法
  16. 三个视频教你如何找到另一半
  17. 模拟点击框架网页内无id无name的按钮
  18. 347. Top K Frequent Elements
  19. 项目管理工作中的一些自我反省
  20. uni-app 微信小程序 + 友盟统计 sdk

热门文章

  1. 6个步骤教你用Python解数独!(含实例代码)
  2. org.apache.hadoop.hdfs.qjournal.client.QuorumException: Unable to check if JNs are ready for formatt
  3. Java实现微信公众号客服功能和本地联调
  4. Linux下rsh服务配置
  5. 深聊性能测试,从入门到放弃之:如何对IO进行性能调优
  6. vue中改变v-html元素样式
  7. ztree树与列表名字获取
  8. 天刀手游pc端显示服务器维护,天刀手游电脑版无法更新网络修复工具
  9. OFDM学习、编程实现
  10. 第一次搭建React项目以及错误:getaddrinfo ENOTFOUND registry.npmjs.org解决办法