package chelper; import java.util.ArrayList; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import io.InputReader; import io.OutputWriter; import misc.Couple; import misc.Pair; import misc.SimpleSavingChelperSolution; public class K extends SimpleSavingChelperSolution { public void solve(int testNumber, InputReader in, OutputWriter out) { wrapSolve(testNumber, in, out); } @Override public void solve(int testNumber) { int n = in.nextInt(); int a = in.nextInt(); int b = in.nextInt(); int k = in.nextInt(); int f = in.nextInt(); Map nameToId = new HashMap<>(); List> trips = new ArrayList<>(); Map, Integer> costs = new HashMap<>(); int prev = -1; for (int i = 0; i < n; i++) { String s1 = in.nextString(); String s2 = in.nextString(); if (!nameToId.containsKey(s1)) { nameToId.put(s1, nameToId.size()); } if (!nameToId.containsKey(s2)) { nameToId.put(s2, nameToId.size()); } int id1 = nameToId.get(s1); int id2 = nameToId.get(s2); Couple couple = new Couple<>(id1, id2); trips.add(couple); int cost = a; if (prev == id1) { cost = b; } prev = id2; if (id1 > id2) { int t = id1; id1 = id2; id2 = t; } couple = new Couple<>(id1, id2); costs.putIfAbsent(couple, 0); costs.put(couple, costs.get(couple) + cost); } List, Integer>> coupleCosts = new ArrayList<>(); int total = 0; for (Map.Entry, Integer> entry : costs.entrySet()) { coupleCosts.add(new Pair<>(entry.getKey(), entry.getValue())); total += entry.getValue(); } coupleCosts.sort(new Comparator, Integer>>() { @Override public int compare(Pair, Integer> o1, Pair, Integer> o2) { return -Integer.compare(o1.second, o2.second); } }); for (int i = 0; i < Math.min(k, coupleCosts.size()); i++) { int orig = coupleCosts.get(i).second; if (orig > f) { total += f - orig; } else { break; } } out.println(total); } }