In this problem there are n bags and each bag contain some coins and chef wants that each bag contain coins divisible by k,
so,the idea is we took money from bag which have some extra money than needed means that let a bag contain coins x,and x is not divisible by k so we take out extra coins and make it divisible to k.
After taking extra coins from every bag we just put them in last bag.
And now take extra coin from last bag.
Sample
for(int i=0;i<n;i++)
{
cin>>a[i];
if(a[i]%k!=0)
{sum = sum + (a[i]%k);}
}
now tkae mod of sum with k and you get the answer.
Code for this problem
/*hard cash*/
/****killing like kamikaze****/
/*tanuj yadav*/
/**********************/
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t,n,k;
cin>>t;
for(int m=0;m<t;m++)
{
cin>>n>>k;
long int a[n];
long long int sum =0;
for(int i=0;i<n;i++)
{
cin>>a[i];
if(a[i]%k!=0)
{sum = sum + (a[i]%k);}
}
cout<<sum%k<<endl;
}
return 0;
}