def reverse_calc(i, k, nums) tmp = nums[i] (0...i).to_a.reverse.each do |j| break if (tmp + nums[j]) > k tmp += nums[j] end tmp end def solve(line1, line2) n, k = line1.split.map(&:to_i) nums = line2.split.map(&:to_i) tmp = 0 (0...n).each do |i| return 'No' if nums[i] > k next if (i != 0) && (nums[i - 1] == nums[i]) tmp += nums[i] return 'Yes' if tmp == k # Kを超えた場合は再計算 tmp = reverse_calc(i, k, nums) if tmp > k end 'No' end line1 = gets.chomp line2 = gets.chomp puts solve(line1, line2)