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: us-ascii
Méret: 1 KB
Imports System
Imports Microsoft.VisualBasic
Module Malac
Const InpFile As String = "malac.be"
Const OutFile As String = "malac.ki"
Const MaxErtek As Integer = 200 * 10000
Private N, osszSuly As Integer
Private ertekek(), sulyok(), cache() As Integer
Private Sub Beolvas()
FileOpen(1, InpFile, OpenMode.Input)
Input(1, osszSuly)
Input(1, N)
ReDim ertekek(N), sulyok(N), cache(osszSuly)
For i As Integer = 1 To N
Input(1, ertekek(i))
Input(1, sulyok(i))
Next
FileClose(1)
End Sub
Private Sub Kiir(ByVal osszMin As Integer)
FileOpen(1, OutFile, OpenMode.Output)
WriteLine(1, osszMin)
FileClose(1)
End Sub
Private Function Bont(ByVal suly As Integer) As Integer
If cache(suly) = 0 Then
If suly = 0 Then
Return 0
Else
Dim min As Integer = MaxErtek + 1
For i As Integer = 1 To N
If sulyok(i) <= suly Then
min = Math.Min(min, ertekek(i) + Bont(suly - sulyok(i)))
End If
Next
cache(suly) = min
End If
End If
Return cache(suly)
End Function
Public Sub Main()
Dim minimum As Integer
Beolvas()
minimum = Bont(osszSuly)
If minimum > MaxErtek Then
Kiir(0)
Else
Kiir(minimum)
End If
End Sub
End Module