Pagini recente » Cod sursa (job #348750) | Cod sursa (job #2849818) | Cod sursa (job #2042115) | Cod sursa (job #2220837) | Cod sursa (job #736554)
Cod sursa(job #736554)
#include <fstream>
#include <cmath>
const unsigned int SIZE(100000);
unsigned int v [SIZE];
int main (void)
{
unsigned int n,g;
std::ifstream input("energii.in");
input >> n >> g;
unsigned int p,c;
input >> p >> c;
unsigned int *it,*limit(v + p);
v[p] = c;
--n;
do
{
input >> p >> c;
it = limit;
do
{
if (*it && (it[p] > *it + c || !it[p]))
it[p] = *it + c;
--it;
}
while (it >= v);
if (!v[p] || v[p] > c)
v[p] = c;
limit += p;
--n;
}
while (n);
input.close();
unsigned int result(-1);
it = v + g - 1;
do
{
if (*it && result > *it)
result = *it;
++it;
}
while (it < limit);
std::ofstream output("energii.out");
if (result & (1 << 31))
output << "-1";
else
output << result;
output.put('\n');
return 0;
}