SPOJ-HOLI WA

I was trying the holi problem on the spoj website.
Problem
My solution

The code implements the idea of maximising the use of each edge which i learnt from this blog. Can someone tell me which part of my implementation is wrong?
cost = ∑ 2*min(ni, N-ni)*wieght(Ei) | for each edge Ei