Pagini recente » Cod sursa (job #130524) | Cod sursa (job #1847613) | Cod sursa (job #2630435) | Cod sursa (job #26048) | Cod sursa (job #736582)
Cod sursa(job #736582)
#include <fstream>
#include <algorithm>
const unsigned int SIZE(10001 * 1001 + 1);
unsigned int v [SIZE];
typedef std::pair<unsigned int,unsigned int> generator;
generator gen [5001];
int main (void)
{
unsigned int n,g;
std::ifstream input("energii.in");
input >> n >> g;
generator *ptr(gen),*end(gen + n);
do
{
input >> ptr->first >> ptr->second;
++ptr;
}
while (ptr < end);
input.close();
--ptr;
unsigned int p,c;
unsigned int *it,*limit(v + 5100);
do
{
it = limit;
p = ptr->first;
c = ptr->second;
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;
--ptr;
}
while (ptr >= gen);
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;
}