Given three integers n,g, and b. n is an integer, g denotes the size of the ‘good’ intervals and b denotes the size of the ‘bad’ intervals. Each number in a ‘good’ interval is a ‘good’ integer and each number in a ‘bad’ interval is a ‘bad’ integer. The very first interval in the series (1…infinity) starts with a good interval and a bad interval follows a good interval, a good interval follows a bad interval and so on. Your task is to find the number k such that the series (1….k) (both inclusive) has at least n/2 good integers.
For example, if n=50, g=20 and b = 35, the series looks like the following :
(1,2…19,20) (21,22…55) (56…75) and so on, where the first interval is good and has a size of ‘g’, second interval is bad and has a size of ‘b’,third interval is good and so on. If suppose k = 57. Then we will have 20 + (57-56+1) = 22 good integers between 1 to k i.e 1 to 57, and our target is at least n/2 or 50/2 = 25. (If n is odd, the target is (n-1)/2 + 1).

Examples :
Input :
n=5
g=1
b=1
Output :
k=5
/*Series : 1,2,3,4,5,6....
Good Intervals : {1}, {3}, {5}...
Bad Intervals : {2} , {4}, {6}..
If k =5, we have 1,3 and 5 as our good integers, i.e 3 good integers.
And 3 > = n/2 (which is 5/2 =(5-1)/2 + 1 =3) and so this is the
answer.
*/
Input :
n=50
g=20
b=35
Output : k= 60
A naive method will be to generate all the possible intervals using a loop and keep a count of total good integers included. Once it exceeds n/2, we can use the last interval that we have generated and keep going backwards from the right end of the interval towards the left end, subtracting 1 each time in our total good integers count until the count becomes greater than or equal to n/2. The number in this last interval where we stopped in the above line will be the value ‘k’. But this can be very time consuming for large constraints on n,g and b.
Observe that the intervals are following an arithmetic progression. We can find the right side of any good interval using the arithmetic progression formula, right = g + (m-1)*(g+b), where m is the m’th good interval. Suppose our intervals are like this : (1,2…19,20)G (21,22…55)B (56…75)G and n=50, g= 20 and b=35. Then 75 = 20 + (2-1)*(20+35). Here, m and right are unknowns. But we can find m using an observation. Observe that we need only up to n/2 good integers. So let n/2 or (n-1)/2 + 1 = target (remember the cases for odd and even n ?. So, total good integers encountered upto m’th interval = m*g and this total will be less than or equal to target. Therefore m*g <= target, or m<= target/g. Since we need the minimum k, we take equal sign here and find m = target/g. Once we find m, we find ‘right’ using the above AP formula. Then the final answer k will lie between the interval (left,right), where left is right – g + 1. And k will be therefore right – (total – target), where k + (total – target) = right, from observation.
Implementation :
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define pb push_back
void solve(ll n,ll g, ll b)
{
ll right=g,total=0;
ll target;
if(n%2==0) target = n/2;
else target = n/2 + 1;
ll m=0;
if(target%g==0) m = target/g ;
else m = target/g + 1;
right= g + (m-1)*(g+b);
total = m*g;
cout<<right - (total - target)<<endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n,g,b;
cin>>n>>g>>b;
solve(n,g,b);
}