Polish version    English version  
  History of OI -> IV OI 1996/1997 -> Problems


 News
 About Olympic
 History of OI
XVII OI 2009/2010
XVI OI 2008/2009
XV OI 2007/2008
XIV OI 2006/2007
XIII OI 2005/2006
XII OI 2004/2005
XI OI 2003/2004
X OI 2002/2003
IX OI 2001/2002
VIII OI 2000/2001
VII OI 1999/2000
VI OI 1998/1999
V OI 1997/1998
IV OI 1996/1997
Stage III - results
Stage II - results
Stage I - results
Problems
Regulations
Organization
Hints
Schedule
III OI 1995/1996
II OI 1994/1995
I OI 1993/1994
 OI books
 National team
 Olympic camps
 Photo gallery
 Links
 SIO
 MAIN
Niebieskie ksi.eczki
IV Olimpiada Informatyczna 1996/1997

Task: PAL
Author: Tomasz Śmigielski
Petrol

II stage contest  
Source filePAL.??? (e.g. PAS,C, CPP)
Executable filePAL.EXE
Input filePAL.IN
Output filePAL.OUT

The haulage contractor carries packages from town A to town B. Packages are transported by cars. Petrol stations along the route offer petrol at various prices. The first station is situated at the beginning of the route. All cars, which the contractor has at his disposal, use one standard unit of petrol per mile but capacities of their fuel tanks can differ. Cost of petrol needed to cover the distance from A to B depends on the capacity of the tank in a car and the chosen plan of refuelling. We assume that each petrol station has enough petrol to fill up a fuel tank of every car. Petrol stations are placed in such a way that each car can travel from A to B.

Task

Write a program that:
  • reads the following data from the text file PAL.IN:
    • the capacity of the fuel tank in a car,
    • the number of petrol stations on the route from A to B,
    • the price of petrol at each station and the distances between consecutive stations,
  • computes the minimal cost of refuelling on the route AB for the car with the given capacity of the fuel tank,
  • writes the result to the text file PAL.OUT.

Input

In the first line of the text file PAL.IN there is written the capacity of the fuel tank of the car. This capacity is an integer p, 1 < p <= 1000000.

In the second line of the same file there is an integer n, 1 < n <= 1000000 - the number of petrol stations on the route from A to B.

In each of the following n lines there is a pair of positive integers ci, di, separated by a single space. The number ci is the price of a standard petrol unit at the i-th station (the numbers of petrol stations increase when we go away from A). The number di is the distance (in miles) between the i-th and the (i+1)-st station (dn is the distance from the last station to the end of the route AB). The numbers meet the following conditions: 1 <= ci <= 1000, 1 <= di <= 1000000.

The length of the route AB (the sum of di) is not greater than 1000000.

Output

In the text file PAL.IN there should be written one integer - the minimal cost of refuelling on the route AB for a car with the fuel tank of the given capacity.

Example

For the file PAL.IN
40
3
2 10
1 15
2 5
the correct solution is the file
40





Print friendly version