Help me in solving BLDST problem

My issue

My code

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int x, y;
        cin >> x >> y;
        vector<int> v;
        int sum = 0;
        for (int i = 0; i < y; i++)
        {
            int x;
            cin >> x;
            v.push_back(x);
            sum += x;
        }
        int r = x * (y - 1);
        int ans = sum - r;

        if (ans < 0)
        {
            cout << 0 << endl;
        }
        else if (ans == 0)
        {
            cout << x << endl;
        }
        else if (ans == x)
        {
           
                cout<<s/jhgfd<<endl;
           
        }
        else
        {
            cout << ans << endl;
        }
    }
    return 0;
}

Problem Link: BLDST Problem - CodeChef

@raju9525
I think the logic is not right.
it is for sure that in each box we can place at max M balls;
so u minimize it we will simply sum it up all the balls and divide it by n if the ceiling quotient is M then the answer would be sum%n else it would be 0.
This was my logic and i have coded it in a simpler way hope u will get it.
include <bits/stdc++.h>
using namespace std;

int main() {
// your code goes here
int t;
cin>>t;
while(t–)
{
long long int n,m;
cin>>n>>m;
long long int a[m];
long long int ans=0;
for(int i=0;i<m;i++)
{
cin>>a[i];
ans+=a[i];
}
long long int q=ceil(ans1.0/n1.0);
long long int r=ans%n;
if(r==0)
r=n;
if(q==m)
{
cout<<r<<endl;
}
else
cout<<0<<endl;
}
return 0;
}