Maximum sum subarray

if i am writing int sum=0; at the start of the block then it is showing sum=50(correct answer is sum=10) while if i write int sum=0; just above the loop of k it is showing sum=10 which is correct. why??