Polish version    English version  
  About Olympic -> Problems


 News
 About Olympic
About contest
Problems
I OI 1993/1994
II OI 1994/1995
III OI 1995/1996
IV OI 1996/1997
V OI 1997/1998
VI OI 1998/1999
VII OI 1999/2000
VIII OI 2000/2001
IX OI 2001/2002
X OI 2002/2003
XI OI 2003/2004
XII OI 2004/2005
XIII OI 2005/2006
XIV OI 2006/2007
XV OI 2007/2008
Problems archive
 History of OI
 OI books
 National team
 Olympic camps
 Photo gallery
 Links
 SIO
 MAIN
Niebieskie ksi.eczki
III Olympiad in Informatics 1995/1996

Task: MOK
Author: Piotr Chrz±stowski-Wachtel
Containers

I stage contest  

We are given n containers, where 1 <= n <= 4. At the beginning all of them are full of water. The liter capacity of the i-th container is a natural number oi satisfying inequalities 1 <= oi <= 49. 
Three kinds of moves can be made:  

  1. Pouring the whole content of one container into another. This move can be made unless there is too little room in the second container. 
  2. Filling up one container with part of the water from another one
  3. Pouring away the whole content of one container into a drain. 

Task

Write a program that:

  • reads form the text file MOK.IN the number of containers n, the capacity of each container and the requested final amount of water in each container,
  • verifies, whether there exist a series of moves which leads to the requested final situation, and if there is one, the program computes the minimal number of moves leading to the requested situation,
  • writes the result into the text file MOK.OUT. The result should be the minimal number of moves leading to the requested final situation, or one word NIE ("no" in Polish) if there is no such a sequence of moves.

Input

In the first line of the text file MOK.IN one positive integer n is written, n <= 4, this is the number of containers. There are n positive integers written in the second line. These are the capacities of the containers (the i-th integer oi denotes the capacity if the i-th  container,1 <= oi <= 49). In the third line of the input file there are written n numbers. These are the requested final volumes of water in the containers (the i-th integer wi denotes the requested final volume of water in the i-th container, 1 <= wi <= oi). All integers in the second and the third line are separated by single spaces.

Output

If it is not possible to result in requested final situation making only allowed moves, your program should write only one word "NIE" ("no" in Polish ) into the text file MOK.OUT, otherwise only one integer equal to the minimal number of moves which lead to the requested final situation should be written.

Example

For the input file MOK.IN:
3
3 5 5
0 0 4

the correct solution is the following file MOK.OUT:
6

For the input file MOK.IN:
2
20 25
10 16

the correct solution is the following file MOK.OUT:
NIE

Your program should look for the file MOK.IN in the current directory and produce the output file MOK.OUT in the current directory too. The file containing the source code of your program should have a name MOK.???, whereas you should put three-letter abbreviation of the used programming language name instead of ???. The same program in executable form should be written in file MOK.EXE




Print friendly version