Skip to content

Instantly share code, notes, and snippets.

@vireshas
Created January 25, 2017 01:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vireshas/14bbe830107043880e4d557f907839ef to your computer and use it in GitHub Desktop.
Save vireshas/14bbe830107043880e4d557f907839ef to your computer and use it in GitHub Desktop.
Fair Rations
You are the benevolent ruler of Rankhacker Castle, and today you're distributing bread to a straight line of subjects. Each subject (where ) already has loaves of bread.
Times are hard and your castle's food stocks are dwindling, so you must distribute as few loaves as possible according to the following rules:
Every time you give a loaf of bread to some person , you must also give a loaf of bread to the person immediately in front of or behind them in the line (i.e., persons or ).
After all the bread is distributed, each person must have an even number of loaves.
Given the number of loaves already held by each citizen, find and print the minimum number of loaves you must distribute to satisfy the two rules above. If this is not possible, print NO.
Input Format
The first line contains an integer, , denoting the number of subjects in the bread line.
The second line contains space-separated integers describing the respective loaves of bread already possessed by each person in the line (i.e., ).
Constraints
, where
Output Format
Print a single integer denoting the minimum number of loaves you must distribute to adjacent people in the line so that every person has an even number of loaves; if it's not possible to do this, print NO.
Sample Input 0
5
2 3 4 5 6
Sample Output 0
4
Sample Input 1
2
1 2
Sample Output 1
NO
#!/bin/ruby
m = gets.strip.to_i
b = gets.strip
b = b.split(' ').map(&:to_i)
t = 0
c = 0
j = 0
p = nil
loop {
i = b[j]
if i % 2 == 1
#puts "i #{i} p #{p} t #{t}"
c += 1
unless p.nil?
t += (j - p) *2
p = nil
else
p = j
end
end
j += 1
break if j == b.size
}
if c % 2 != 0
puts "NO"
else
puts t
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment