Minimum energy × delay product

Choose a frequency level for each of P programs in order to minimize the total energy times delay product, including a fixed cost per frequency change.

Medium4Dynamic programmingImplementationInterviewNo attempts yetTime limit2sMemory limit512 MB

Problem

Paulo works for a large company called Ábaco Computadores e Manutenções (ACM). His job is to maintain the computers of ACM clients spread all over the country, so he spends a good number of hours per week inside airplanes. He always carries his laptop and gets work done during the flights.

Laptop batteries do not last long. While looking for ways to stretch the battery during a flight, Paulo learned that modern processors run at several frequency levels and trade performance for power. His first idea was to pin the laptop at the lowest frequency. That turned out to be useless: programs ran so slowly that there was no time to finish every task, and the remaining charge had nothing left to do.

How much a frequency level affects performance differs from program to program, depending on whether memory, the CPU, or I/O is the bottleneck. Since modern processors let software change the frequency level, Paulo plans to pick a level per program so the battery lasts longer while performance stays reasonable. To account for energy and performance together he uses a well known metric, the energy × delay product (EDP, Energy × Delay Product).

Paulo has the list of programs to run in order, the time and the energy each program needs at each frequency level, and the energy a frequency change costs. The problem is that, like most system administrators, he does not enjoy programming, so he asked you, a good friend and an expert in algorithms, for help.

The processor supports FF frequency levels numbered 1 to FF, and it sits at level 1 when a test case starts. The programs run in order from 1 to PP. Running program pp at level ff costs Ep,fE_{p,f} joules and Ap,fA_{p,f} milliseconds, so the EDP of that run is Ep,f×Ap,fE_{p,f} \times A_{p,f}. Every frequency change costs EE joules and AA milliseconds, no matter which level it starts from and which level it ends at, so the EDP of a change is E×AE \times A. The total EDP is the sum over the PP runs and every change. That is, if program pp runs at level fpf_p and CC changes happen, then

EDP=p=1PEp,fp×Ap,fp+C×E×A\text{EDP} = \sum_{p=1}^{P} E_{p,f_p} \times A_{p,f_p} + C \times E \times A

Find the plan that minimizes the total EDP and report that value.

Input

The input contains several test cases. The first line of a test case has four integers FF, PP, EE, and AA: the number of frequency levels the processor supports (1F201 \le F \le 20), the number of programs to run in order (1P50001 \le P \le 5000), the energy in joules to change between any two frequency levels (1E1001 \le E \le 100), and the time in milliseconds to change between any two frequency levels (1A1001 \le A \le 100). Frequency levels are identified by the integers 1 to FF, and programs by the integers 1 to PP.

The next P×FP \times F lines describe the programs, FF lines per program. The first FF lines belong to program 1, the next FF lines to program 2, and so on. The ff-th line among the FF lines of program pp has two integers Ep,fE_{p,f} and Ap,fA_{p,f}, the energy in joules and the time in milliseconds needed to run program pp at frequency level ff, with 1Ep,f10001 \le E_{p,f} \le 1000 and 1Ap,f10001 \le A_{p,f} \le 1000.

At the start of every test case the processor is at frequency level 1. The end of the input is a line with F=P=E=A=0F = P = E = A = 0, which is not a test case.

Output

For each test case, print one line with the minimum EDP for running programs 1 to PP in the order they appear in the input.