Codeforces847k travel cards

阅读全文大概需要 4分钟
本文总阅读量
Posted by yjjr's blog on February 6, 2018

标签:贪心,hash

In theevening Polycarp decided to analyze his today's travel expenses on publictransport.

The bussystem in the capital of Berland is arranged in such a way that each bus runsalong the route between two stops. Each bus has no intermediate stops. So eachof the buses continuously runs along the route from one stop to the other andback. There is at most one bus running between a pair of stops.

Polycarpmade n trips on buses. Abouteach trip the stop where he started the trip and the the stop where he finishedare known. The trips follow in the chronological order in Polycarp's notes.

It isknown that one trip on any bus costs a burles. Incase when passenger makes a transshipment the cost of trip decreases to b burles (b < a). Apassenger makes a transshipment if the stop on which he boards the buscoincides with the stop where he left the previous bus. Obviously, the firsttrip can not be made with transshipment.

Forexample, if Polycarp made three consecutive trips: "BerBank"  "University","University"  "BerMall", "University"  "BerBank", then he payed a + b + a = 2a + bburles.From the BerBank he arrived to the University, where he made transshipment tothe other bus and departed to the BerMall. Then he walked to the University andreturned to the BerBank by bus.

AlsoPolycarp can buy no more than k travelcards. Each travel card costs f burles.The travel card for a single bus route makes free of charge any trip by thisroute (in both directions). Once purchased, a travel card can be used anynumber of times in any direction.

What isthe smallest amount of money Polycarp could have spent today if he can buy nomore than k travelcards?

Input

The firstline contains five integers n, a, b, k, f (1 ≤ n ≤ 300, 1 ≤ b < a ≤ 100, 0 ≤ k ≤ 300, 1 ≤ f ≤ 1000) where:

·  n — the number of Polycarp trips,

·  a — the cost of a regualar single trip,

·  b — the cost of a trip after a transshipment,

·  k — the maximum number of travel cards Polycarpcan buy,

·  f — the cost of a single travel card.

Thefollowing n linesdescribe the trips in the chronological order. Each line contains exactly twodifferent words separated by a single space — the name of the start stop andthe name of the finish stop of the trip. All names consist of uppercase andlowercase English letters and have lengths between 1 to 20 lettersinclusive. Uppercase and lowercase letters should be considered different.

Output

Print thesmallest amount of money Polycarp could have spent today, if he can purchase nomore than k travelcards.

Example

Input

3 5 3 1 8
BerBank University
University BerMall
University BerBank

Output

11

Input

4 2 1 300 1000
a A
A aa
aa AA
AA a

Output

5

Note

In thefirst example Polycarp can buy travel card for the route "BerBank University" and spend 8 burles. Note that his second trip"University"  "BerMall" was made aftertransshipment, so for this trip Polycarp payed 3 burles. So the minimum total sum equals to 8 + 3 = 11 burles.

In thesecond example it doesn't make sense to buy travel cards. Note that each ofPolycarp trip (except the first) was made with transshipment. So the minimumtotal sum equals to 2 + 1 + 1 + 1 = 5 burles.

分析:找使用次数*价值最大的前k个替换成通票,如果不值得换为通票,那么就不换

Hash可以用模板,也可以用C++自带的map,最近真的经常碰到hash题

Code

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define P(x,y) make_pair(min(x,y),max(x,y))
using namespace std;
const int maxn=305;
 
map<pair<int,int>,int> s;
map<string,int>Map;
int n,x,y,tx,ty,k,f,cnt=0,last=-1,ans=0,c[maxn],num=0;
string a,b;
 
int main()
{
         cin>>n>>x>>y>>k>>f;
         rep(i,1,n){
                 cin>>a>>b;
                 if(!Map[a])Map[a]=++cnt;
                 if(!Map[b])Map[b]=++cnt;
                 int tx=Map[a],ty=Map[b],cost=(tx==last)?y:x;
                 ans+=cost;s[P(tx,ty)]+=cost;last=ty;
         }
         for(map<pair<int,int>,int>::iterator it=s.begin();it!=s.end();it++)
             c[++num]=it->second;
         sort(c+1,c+num+1,greater<int>());
         rep(i,1,k)
             if(c[i]<=f)break;
                 else ans-=c[i]-f;
         cout<<ans<<endl;
         return 0;
}
 



本文可以转载,但必须附上原文链接,否则你会终生找不到妹子!!!欢迎关注我的CSDN: ahyjjr