This page is still under construction.

Parts of this page are still being built. What you see may change.

Cylinders

Time limit1sMemory limit128 MB

Summary
Two identical cylinders with the same n scale marks start empty; find the minimum number of fill, drain, and pour actions to leave exactly l millilitres in one cylinder, or report it impossible.
Level

Medium7 of 10

Topics
BFS, Graph, Math, Simulation
Solved
No attempts yet

Problem

Byteasar urgently needs to measure exactly ll millilitres of water, but the only glassware he could buy was a pair of identical defective cylinders. Each cylinder has a capacity of xnx_n millilitres and is marked with scales at exactly the same values x1,x2,…,xnx_1, x_2, \dots, x_n (in millilitres); the water level can only be read at these marked heights.

Both cylinders start empty. In one unit of time Byteasar may perform any one of the following actions:

  • fill a cylinder from the tap until its water level reaches one of that cylinder's scales;
  • pour water from a cylinder into the sink until its level reaches one of that cylinder's scales;
  • pour water from one cylinder into the other until the source cylinder's level reaches one of its scales;
  • pour water from one cylinder into the other until the destination cylinder's level reaches one of its scales.

A transfer between cylinders is only possible when the destination does not overflow. Determine the minimum number of actions needed to obtain exactly ll millilitres of water in one of the cylinders, or report that it is impossible.

Input

The first line contains one integer nn (3≤n≤253 \le n \le 25): the number of scales on each cylinder.

The second line contains a strictly increasing sequence of integers x1,x2,…,xnx_1, x_2, \dots, x_n separated by single spaces: the scale values. It is guaranteed that x1=0x_1 = 0 and that xnx_n (xn≤100000x_n \le 100000) equals the capacity of each cylinder.

The third line contains one integer ll (0≤l≤xn0 \le l \le x_n): the amount of water to be measured.

Output

If Byteasar cannot measure exactly ll millilitres, output a single line containing the word NIE. Otherwise, output a single integer: the minimum number of actions required.

Examples1

  1. Example 1

    Input
    4
    0 2 5 15
    9
    
    Expected output
    5