Imports Microsoft.VisualBasic
Imports System
Imports System.Collections
Imports System.Text
Imports System.Math
Public Class BuyingFlowers
Public Function buy(ByVal roses As Integer(), ByVal lilies As Integer()) As Integer
Dim t As Integer
Dim t2 As Integer
Dim t3 As Integer
Dim Ret As Long=99999999
Dim i As Integer,j As Integer,k As Integer
For i=0 To 2^(ubound(roses)+1)-1
t=0:t2=0:
For j=0 To ubound(roses)
If (i And 2^j)<>0 Then
t=t+roses(j)
t2+=lilies(j)
End If
Next
If abs(t-t2)<=1 Then
t3=t+t2
For k=1 To int(sqrt(t3))
If (t3 mod k)=0 Then
Ret=Math.Min(Ret,abs(k-(t3/k)))
End If
Next
End If
Next
If ret=99999999 Then
Return -1
Else
return ret
End If
End Function
End Class