Problem p03103 (python)

Fixed submission:
# -*- coding: utf-8 -*-                                                                                 
import sys, heapq                                                                                       
# ----------------------------------------------------------------                                      
# Use Solve Function                                                                                    
                                                                                                        
def solve(lines):                                                                                       
    N,M = map(int, lines.pop(0).split(' '))                                                             
    NLIST = []                                                                                          
    for i in range(N):                                                                                  
        A,B = map(int, lines.pop(0).split(' '))                                                         
        heapq.heappush(NLIST, (A, B))                                                                   
                                                                                                        
    res = 0                                                                                             
    while(True):                                                                                        
        p = heapq.heappop(NLIST)                                                                        
        if ( p[1] >= M ):                                                                               
            res += M * p[0]                                                                             
            break                                                                                       
        else:                                                                                           
            res += p[0] * p[1]                                                                          
            M -= p[1]                                                                                   
                                                                                                        
                                                                                                        
    print(res)                                                                                          
                                                                                                        
lines = [x.strip() for x in sys.stdin.readlines()]                                                      
                                                                                                        
#                                                                                                       
# solve !!                                                                                              
#                                                                                                       
solve(lines)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-                                                                                 
import sys, heapq                                                                                       
# ----------------------------------------------------------------                                      
# Use Solve Function                                                                                    
                                                                                                        
def solve(lines):                                                                                       
    N,M = map(int, lines.pop(0).split(' '))                                                             
    NLIST = []                                                                                          
    for i in range(N):                                                                                  
        A,B = map(int, lines.pop(0).split(' '))                                                         
        heapq.heappush(NLIST, (A, B))                                                                   
                                                                                                        
    res = 0                                                                                             
    while(True):                                                                                        
        p = heapq.heappop(NLIST)                                                                        
        if ( p[1] >= M ):                                                                               
            res += M * p[0]                                                                             
            break                                                                                       
        else:                                                                                           
            res += p[0] * p[1]                                                                          
            M -= p[1]                                                                                   
                                                                                                        
                                                                                                        
    print(res)                                                                                          
lines = contents.split('\n')                                                                            
                                                                                                        
# 2. use stdin                                                                                          
                                                                                                        
lines = [x.strip() for x in sys.stdin.readlines()]                                                      
                                                                                                        
#                                                                                                       
# solve !!                                                                                              
#                                                                                                       
solve(lines)