Az alábbi letöltési lehetőségek közül választhatsz: (
segítség)
Típus: text/plain
Tartalmaz szöveget
Karakterkódolás: utf-8
Méret: 3 KB
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace Malac
{
class Coin : IComparable<Coin>
{
public int weight;
public int value;
public int CompareTo(Coin other)
{
if (this.weight < other.weight) return -1;
else
if (this.weight > other.weight) return 1;
else
return 0;
}
public Coin(int _v, int _w)
{
value = _v;
weight = _w;
}
}
class Program
{
private int[] cache;
private int n;
private int k;
private List<Coin> coins;
private int minWeight;
public Program()
{
StreamReader ins = new StreamReader("malac8.be");
StreamWriter outs = new StreamWriter("malac8.ki");
string[] line = ins.ReadLine().Split(' ');
n = int.Parse( line[0] );
line = ins.ReadLine().Split(' ');
k = int.Parse( line[0] );
coins = new List<Coin>(k);
cache = new int[n+1];
for (int i = 0; i < k; i++)
{
line = ins.ReadLine().Split(' ');
coins.Add(new Coin(int.Parse(line[0]), int.Parse(line[1])));
}
coins.Sort();
minWeight = coins[0].weight;
cache[0] = 0;
for (int i = 1; i <= n; i++)
{
cache[i] = -2;
}
outs.WriteLine( getMinValue(n) );
outs.Close();
}
private int getMinValue(int w)
{
if (cache[w] != -2) return cache[w];
if (w < minWeight)
{
cache[w] = -1;
return cache[w];
}
int value, minValue = int.MaxValue, choosenCoin = -1;
for (int i = 0; i < k; i++)
{
if (coins[i].weight > w) break;
value = getMinValue(w - coins[i].weight);
if (value != -1)
{
value += coins[i].value;
if (value < minValue)
{
minValue = value;
choosenCoin = i;
}
}
}
if (choosenCoin == -1)
{
minValue = -1;
}
cache[w] = minValue;
return cache[w];
}
static void Main(string[] args)
{
new Program();
}
}
}