/*
ID: sdjllyh1
PROG: milk
LANG: JAVA
complete date: 2008/10/6
author: LiuYongHui From GuiZhou University Of China
more article: www.cnblogs.com/sdjls
*/


import java.io.*;
import java.util.*;

public class milk
{
    
static private ArrayList farmers = new ArrayList();
    
static private int minimumPrice = 0;
    
static private int n;

    
public static void main(String[] args) throws IOException
    
{
        init();
        run();
        output();
        System.exit(
0);
    }


    
private static void init() throws IOException
    
{
        BufferedReader f 
= new BufferedReader(new FileReader("milk.in"));
        StringTokenizer st 
= new StringTokenizer(f.readLine());
        n 
= Integer.parseInt(st.nextToken());
        
int m = Integer.parseInt(st.nextToken());

        
int p, a;
        
for (int i = 0; i < m; i++)
        
{
            st 
= new StringTokenizer(f.readLine());
            p 
= Integer.parseInt(st.nextToken());
            a 
= Integer.parseInt(st.nextToken());
            farmers.add(
new farmer(p,a));
        }

        f.close();
    }


    
private static void run()
    
{
        
int farmerIndex = 0;
        Collections.sort(farmers);
        
while (n>0)
        
{
            farmer f 
= (farmer)farmers.get(farmerIndex);
            farmerIndex
++;
            
if (f.amount <= n)
            
{
                n 
-= f.amount;
                minimumPrice 
+= f.amount * f.price;
            }

            
else
            
{
                minimumPrice 
+= n * f.price;
                n 
= 0;
            }

        }

    }


    
private static void output() throws IOException
    
{
        PrintWriter out 
= new PrintWriter(new BufferedWriter(new FileWriter("milk.out")));
        out.println(minimumPrice);
        out.close();
    }

}


class farmer implements Comparable
{
    
public int price;
    
public int amount;
    
public farmer(int price, int amount)
    
{
        
this.price = price;
        
this.amount = amount;
    }


    
public int compareTo(Object arg0)
    
{
        farmer f1 
= (farmer)arg0;
        
if (this.price < f1.price)
            
return -1;
        
else if (this.price == f1.price)
            
return 0;
        
else
            
return 1;
    }
    
}
posted on 2008-10-06 17:37  刘永辉  阅读(494)  评论(2)    收藏  举报