In this HackerEarth Equal Division problem solution, There are N teams in a software company. The ith team has Bi employees in it and a total budget of  units of money. Each team has to divide their budget within their employees equally. But for some teams, it's not possible to divide the budget equally.
Therefore, the company have to perform revisions in the teams' budget sizes.

In one revision, to revise the budget of ith team, the budget of the first i teams has to be increased by 1.

Your task is to find the minimum number of revisions needed so that for each team, equal distribution of their budget among the employees is possible.


HackerEarth Equal Division problem solution


HackerEarth Equal Division problem solution.

#include<iostream>
#include<cstdio>
#include<cstring>

typedef long long ll;
const int N=101000;
int A[N],B[N];
int n;
ll ans;

int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d%d",A+i,B+i),A[i]%=B[i];
ans=0;
for(int i=n;i;i--)
{
A[i]=(ans+A[i])%B[i];
if(A[i]!=0)ans+=B[i]-A[i];
}
printf("%lld\n",ans);
return 0;
}

Second solution

#include <bits/stdc++.h>

using namespace std;
const int MAX = 1e5 + 5;
const int INF = 1e9;
long long a[MAX], b[MAX];

int main(int argc, char* argv[]) {
if(argc == 2 or argc == 3) freopen(argv[1], "r", stdin);
if(argc == 3) freopen(argv[2], "w", stdout);
int n;
long long ans;
assert(cin >> n);
assert(1 <= n and n <= 100000);
for (int i = 0; i < n; i++) {
assert(cin >> a[i] >> b[i]);
assert(0 <= a[i] and a[i] <= INF);
assert(1 <= b[i] and b[i] <= INF);
}
assert(!(cin >> n));
ans = 0;
for (int i = n-1; i >= 0; i--) {
a[i] += ans;
ans += ((b[i] - (a[i] % b[i]))) % b[i];
}
cout << ans << endl;
return 0;
}