{-

Ðóêîâîäñòâî ôèíàíñîâîé ïèðàìèäû ðåøèëî ïðèñâîèòü ÷èñëîâîé ðåéòèíã êàæäîìó èç ó÷àñòíèêîâ ïèðàìèäû. Åñëè ó÷àñòíèê äî ñèõ ïîð íèêîãî íå ïðèâåë â ïèðàìèäó, òî åãî ðåéòèíã ðàâíÿåòñÿ 1. Åñëè ó ó÷àñòíèêà óæå åñòü ïîä÷èíåííûå ó÷àñòíèêè, òî åãî ðåéòèíã ðàâíÿåòñÿ 1 + ñóììà ðåéòèíãîâ åãî ïðÿìûõ ïîä÷èíåííûõ.
Çàäà÷à: íàéòè ñóììó ðåéòèíãîâ âñåõ ó÷àñòíèêîâ ôèíàíñîâîé ïèðàìèäû.

Èñõîäíûå äàííûå ïîäàþòñÿ â âèäå ìàññèâà èç n ñòðîê, ãäå êàæäàÿ ñòðîêà ñîäåðæèò n ñèìâîëîâ. Êàæäûé ñèìâîë  ëèáî '0', ëèáî '1'.
j-é ñèìâîë i-é ñòðîêè óêàçûâàåò íà òî, ÿâëÿåòñÿ ëè ó÷àñòíèê ïîä íîìåðîì j ïðÿìûì ïîä÷èíåííûì ó÷àñòíèêà i.
j-é ñèìâîë j-é ñòðîêè âñåãäà ðàâåí '0'. Äåðåâî ó÷àñòíèêîâ íèêîãäà íå èìååò öèêëîâ. Êàæäûé ó÷àñòíèê âñåãäà ÿâëÿåòñÿ ïðÿìûì ïîä÷èíåííûì ìàêñèìóì îäíîãî äðóãîãî ó÷àñòíèêà. Ó äåðåâà ó÷àñòíèêîâ âñåãäà òîëüêî îäèí êîðíåâîé ó÷àñòíèê.

Ïðèìåð èñõîäíûõ äàííûõ:

"0110"
"0000"
"0001"
"0000"


Êîðíåâûì ýëåìåíòîì â äàííîì ñëó÷àå ÿâëÿåòñÿ ó÷àñòíèê 0. Ó íåãî äâîå ïîä÷èíåííûõ: ó÷àñòíèêè 1 è 2. Ó 1 ïîä÷èíåííûõ íåò, ó 2 åñòü ïîä÷èíåííûé ó÷àñòíèê 3.
Ðåéòèíã 3-ãî: 1.
Ðåéòèíã 1-ãî: 1.
Ðåéòèíã 2-ãî: 1 + 1 = 2.
Ðåéòèíã 0-ãî: 1 + 1 + 2 = 4.

Ðåçóëüòàòîì ðàáîòû êîäà äîëæíà ÿâëÿòüñÿ ñóììà ðåéòèíãîâ âñåõ ó÷àñòíèêîâ, â äàííîì ñëó÷àå ýòî: 1 + 1 + 2 + 4 = 8.

-}

import Data.List

treeData = [
		"0110",
		"0000",
		"0001",
		"0000"
		]

employeeSubs = elemIndices '1'
employees = map employeeSubs treeData

calcEmpl d n = foldr (\x -> (+) (calcEmpl d x)) 1 (d !! n)
calcAll = map (calcEmpl employees) [0..(length employees) - 1]

main = putStrLn . show . sum $ calcAll
