코딩테스트/동적계획법
[DP] 백준 2180 소방서의 고민 C++
유(YOO)
2022. 1. 27. 12:31
https://www.acmicpc.net/problem/2180
#include<iostream>
#include<vector>
#include<algorithm>
#define MAX 100001
using namespace std;
int n ,a, b;
int ans;
vector<pair<int, int>> v;
bool cmp(pair<int, int> p, pair<int, int> q) {
int a = p.first, b = p.second, c = q.first, d = q.second;
if (a == 0) return false;
else if (c == 0) return true;
else if ((b == 0) && (d == 0)) return a < c;
return b * c < a * d;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a >> b;
v.push_back({ a,b });
}
sort(v.begin(), v.end(), cmp);
a = 0;
for (int i = 0; i < n; i++) {
a = v[i].first * ans + v[i].second;
ans += a;
ans %= 40000;
}
cout << ans;
}