1. 程式人生 > 其它 >P1208 [USACO1.3]混合牛奶 Mixing Milk

P1208 [USACO1.3]混合牛奶 Mixing Milk

// Problem: P1208 [USACO1.3]混合牛奶 Mixing Milk
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P1208
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// User: Pannnn

#include <bits/stdc++.h>

using namespace std;

template<class T>
void debugVector(const T &a) {
    cout << "[ ";
    for (size_t i = 0; i < a.size(); ++i) {
        cout << a[i] << (i == a.size() - 1 ? " " : ", ");
    }
    cout << "]" << endl;
}

template<class T>
void debugMatrix2(const T &a) {
    for (size_t i = 0; i < a.size(); ++i) {
        debugVector(a[i]);
    }
}

template<class T>
using matrix2 = vector<vector<T>>;

template<class T>
vector<vector<T>> getMatrix2(size_t n, size_t m, T init = T()) {
    return vector<vector<T>>(n, vector<T>(m, init));
}

template<class T>
using matrix3 = vector<vector<vector<T>>>;

template<class T>
vector<vector<vector<T>>> getMatrix3(size_t x, size_t y, size_t z, T init = T()) {
    return vector<vector<vector<T>>>(x, vector<vector<T>>(y, vector<T>(z, init)));
}

vector<int> genBigInteger(string a) {
    vector<int> res;
    for (int i = a.size() - 1; i >= 0; --i) {
        res.push_back(a[i] - '0');
    }
    return res;
}

void printBigInteger(vector<int> a) {
    for (size_t i = a.size() - 1; i >= 0; --i) {
        cout << a[i];
    }
}

vector<int> addBigInteger(vector<int> a, vector<int> b) {
    vector<int> res;
    int pre = 0;
    for (size_t i = 0; i < a.size() || i < b.size() || pre; ++i) {
        if (i < a.size()) pre += a[i];
        if (i < b.size()) pre += b[i];
        res.push_back(pre % 10);
        pre /= 10;
    }
    return res;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int n, m;
    cin >> n >> m;
    vector<pair<int, int>> info(m);
    for (int i = 0; i < m; ++i) {
        cin >> info[i].first >> info[i].second;
    }
    
    sort(info.begin(), info.end());
    int res = 0;
    for (int i = 0; i < m && n > 0; ++i) {
        if (info[i].second >= n) {
            res += info[i].first * n;
        } else {
            res += info[i].first * info[i].second;
        }
        n -= info[i].second;
    }
    cout << res << endl;
    return 0;
}