1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #include <stdio.h> #include <algorithm> #include <vector> #include <utility> using namespace std; vector<pair<int, int> > company(6000); bool compare(const pair<int, int>& a, const pair<int, int>& b) { if (a.first == b.first) return a.second < b.second; return a.first < b.first; } int main() { int n, m; scanf("%d %d", &m, &n); for (int i = 1; i <= n; i++) { int a, b; scanf("%d %d", &a, &b); company[i] = make_pair(a, b); } sort(&company[1], &company[n + 1], compare); int total = 0; for (int i = 1; i <= n; i++) { if (m >= company[i].second) { m -= company[i].second; total += (company[i].first * company[i].second); } else if (m > 0 && m < company[i].second) { total += (company[i].first * m); break; } } printf("%d", total); } | cs |
'알고리즘 > 그리디 알고리즘' 카테고리의 다른 글
마감시간을 가지는 작업 더블릿 (0) | 2019.02.20 |
---|---|
knapsack 더블릿 (0) | 2019.02.20 |
(복습필요) 11000 강의실 배정 (0) | 2019.02.19 |
1931 회의실 배정 (0) | 2019.02.19 |
11047 동전만들기 (0) | 2019.02.19 |