The Question is from August Cook-Off.
The link to solution is: https://www.codechef.com/problems/CHEFNWRK
The glitch to your solution was taking input. Your code is expected to take all the inputs. Then you are expected to perform any operation while the break statement was ending process of taking input. So the next input of array is treated as the start of a new test case.
The modified solution is:
using namespace std;