Problem:
Knapsack Problem with Limitations

You have N items that you want to put them into a knapsack. Item i has value vi, weight wi and limitation mi.

You want to find a subset of items to put such that:

The total value of the items is as large as possible.

The items have combined weight at most W, that is capacity of the knapsack.

You can select at most mi items for ith item.

Find the maximum total value of items in the knapsack.

Input

N W
v1 w1 m1
v2 w2 m2
:
vN wN mN

The first line consists of the integers N and W. In the following N lines, the value, weight and limitation of the i-th item are given.

Output

Print the maximum total values of the items in a line.

Constraints

1 ≤ N ≤ 100

1 ≤ vi ≤ 1,000

1 ≤ wi ≤ 1,000

1 ≤ mi ≤ 10,000

1 ≤ W ≤ 10,000

Sample Input 1

4 8
4 3 2
2 1 1
1 2 4
3 2 2

Sample Output 1

12

Sample Input 2

2 100
1 1 100
2 1 50

Sample Output 2

150

Solution in Ruby (judge verdict: Accepted)

def get_i() #空白区切の入力を数値(整数)の配列で返す
  return gets.chomp.split(" ").map(&:to_i)
end
def get_f() #空白区切の入力を数値(実数)の配列で返す
  return gets.chomp.split(" ").map(&:to_f)
end
def get() #空白区切の入力を文字列の配列で返す
  return gets.chomp.split(" ")
end
def get_nsp() #入力されたものを一文字ずつに区切った文字列の配列で返す
  return gets.chomp.split("")
end
def yn_judge(bool,y="Yes",n="No") #boolに真偽を投げることで、trueならy、falseならnの値を出力する
  return bool ? y : n 
end
def array(size,n=1,init=nil) #nに配列の次元数、sizeに配列の大きさ、initに初期値を投げることでその配列を返す
  if n==1
    return Array.new(size){init}
  else
    return Array.new(n).map{Array.new(size){init}}
  end
end

def max(a,b)
  return a>b ? a : b
end
def min(a,b)
  return a>b ? b : a
end
n,w=get_i
vwm=array(n,1)
n.times do|i|
  vwm[i]=get_i
end
vwm.sort!{|x,y| y[1]<=>x[1]}
dp=array(w+1,1,0)

n.times do|i|
  k=0
  while vwm[i][2]>0
    x=min(vwm[i][2],2**k)
    vwm[i][2]-=x
    w.downto(x*vwm[i][1]) do|j|
      dp[j]=max(dp[j],dp[j-x*vwm[i][1]]+x*vwm[i][0])
    end
    k+=1
  end
end
puts dp[w]