优先队列水过去就好,当然是存在O(n)的,每次比较单价得到当前最小单价再去买即可。
#include <iostream>
#include <string>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#define MAX 105
#define INF 0x3f3f3f3f
using namespace std;
struct cmp {
constexpr bool operator()(
const int& a,
const int& b)
const{
return a > b;
}
};
priority_queue<
int,
vector<int>, cmp > q;
int main() {
freopen(
"a.txt",
"r", stdin);
freopen(
"b.txt",
"w", stdout);
int n, x, y;
cin >> n;
int s =
0;
for (
int i =
1; i <= n; ++i) {
scanf(
"%d%d", &x, &y);
q.push(y);
int t = q.top();
s += x * t;
}
cout << s << endl;
return 0;
}