Skip to content

Latest commit

 

History

History
66 lines (52 loc) · 1.08 KB

1348.-da-pei-niu-nai.md

File metadata and controls

66 lines (52 loc) · 1.08 KB

1348. 搭配牛奶

{% tabs %} {% tab title="go" %}

package main

import (
    "fmt"
    "sort"
    )
    
    
type Element [][]int

func (p Element) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }
func (p Element) Len() int           { return len(p) }
func (p Element) Less(i, j int) bool {
    if p[i][0] != p[j][0] {
        return p[i][0] < p[j][0] 
    } else {
        return p[i][1] < p[j][1]
    }
}
    
    
func main(){
    var N,M int
    fmt.Scanf("%d %d", &N, &M)
    A := make([][]int, M)
    
    for i:= 0 ; i < M; i ++ {
        var a, b int
        fmt.Scanf("%d %d", &a, &b)
        A[i] = append([]int(nil),[]int{a,b}...)
    }
    // fmt.Println(A)
    sort.Sort(Element(A))
    // fmt.Println(A)
    ans := 0 
    i := 0 
    for N > 0 {
        if  A[i][1] == 0 { 
            i ++ 
            continue 
            
        }
        if N >= A[i][1] {
            ans += A[i][0] * A[i][1]
            N -= A[i][1]
        }else {
            ans += A[i][0] * N
            N = 0 
        }
        i ++
    }
    fmt.Println(ans)

    
}

{% endtab %} {% endtabs %}