#include <iostream>
#include <vector>
#include <algorithm>
struct chunk {
unsigned int weight;
unsigned int price;
};
using namespace std;
int main()
{
unsigned int length = 0, needs = 0, weight = 0, price = 0, before = -1;
vector<struct chunk> meatlist;
cin >> length >> needs;
for (int i = 0; i < length; ++i)
{
cin >> weight >> price;
meatlist.push_back(chunk{ weight, price });
}
sort(meatlist.begin(), meatlist.end(), [](const auto& a, const auto& b) { return ((a.price < b.price) || (a.price == b.price) && (a.weight > b.weight)); });
weight = price = 0;
for (const auto& iter : meatlist)
{
if (weight < needs)
{
if (price > iter.price)
{
before = price = iter.price;
}
else
{
if (before == iter.price)
{
price += iter.price;
}
else
{
before = price = iter.price;
}
}
}
else
{
if ((price > iter.price) && (before != iter.price))
{
before = price = iter.price;
}
}
weight += iter.weight;
}
if (weight < needs)
{
cout << -1 << endl;
}
else
{
cout << price << endl;
}
return 0;
}
구상한 코드가 틀려 몇일 정도 다시 예외 상황이나 문제점을 고민해보도록 하겠습니다.
'개발일지 > Algorithm' 카테고리의 다른 글
20.07.19 개발일지 - 무어 기계(cont) (0) | 2020.07.19 |
---|---|
20.17.16 개발일지 - 정육점(fin) (0) | 2020.07.16 |
20.07.13 개발일지 - 정육점(cont) (0) | 2020.07.13 |
20.06.28 개발일지 - 정육점(실패) (0) | 2020.06.29 |
20.06.27 개발일지 - 謎紛芥索紀 (Large) (0) | 2020.06.27 |