Problem:
Score : 200 points

Problem Statement

Takahashi has many red balls and blue balls. Now, he will place them in a row.

Initially, there is no ball placed.

Takahashi, who is very patient, will do the following operation 10^{100} times:

Place A blue balls at the end of the row of balls already placed. Then, place B red balls at the end of the row.

How many blue balls will be there among the first N balls in the row of balls made this way?

Constraints

1 \leq N \leq 10^{18}

A, B \geq 0

0 < A + B \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the number of blue balls that will be there among the first N balls in the row of balls.

Sample Input 1

8 3 4

Sample Output 1

4

Let b denote a blue ball, and r denote a red ball. The first eight balls in the row will be bbbrrrrb, among which there are four blue balls.

Sample Input 2

8 0 4

Sample Output 2

0

He placed only red balls from the beginning.

Sample Input 3

6 2 4

Sample Output 3

2

Among bbrrrr, there are two blue balls.

Solution in C (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#define SWAP(type, a, b) \
    {                    \
        type tmp = a;    \
        a = b;           \
        b = tmp;         \
    }
#define MAX(a, b) (a > b) ? a : b
#define MIN(a, b) (a < b) ? a : b

typedef struct LISTNODE
{
    int data;
    struct LISTNODE *pNext;
    struct LISTNODE *pPrev;
} ListNode;

ListNode rootNode;

ListNode *getNode(int n);
int getListLength();
void addNode(int n, int val);
void deleteNode(int n);
void swapNode(int n1, int n2);
void sortList();
void showList(int n); //If n>0,you can see n-th Node in the List.Otherwise,you can see whole list.
void freeList();

int main()
{
    rootNode.data = 0;
    rootNode.pNext = rootNode.pPrev = &rootNode;

    long n, i = 0, j, k = 0, a, b;
    scanf("%d %d %d", &n, &a, &b);
    while (1)
    {
        for (j = 0; j < a; j++, i++, k++)
        {
            if (k == n)
                goto end;
        }
        for (j = 0; j < b; j++, k++)
        {
            if (k == n)
                goto end;
        }
    }
end:
    printf("%ld", i);

    return 0;
}

ListNode *getNode(int n)
{
    int i;
    ListNode *pNode = &rootNode;
    for (i = 0; i < n; i++, pNode = pNode->pNext)
        ;
    return pNode;
}

int getListLength()
{
    int i;
    ListNode *pNode = rootNode.pNext;
    for (i = 0; pNode != &rootNode; pNode = pNode->pNext, i++)
        ;
    return i;
}

void addNode(int n, int val)
{
    ListNode *pPrevNode = getNode(n - 1);
    ListNode *pNewNode = (ListNode *)malloc(sizeof(ListNode));
    pNewNode->data = val;
    pNewNode->pNext = pPrevNode->pNext;
    pPrevNode->pNext = pNewNode;
    (pNewNode->pNext)->pPrev = pNewNode;
    pNewNode->pPrev = pPrevNode;
}

void deleteNode(int n)
{
    ListNode *pNode = getNode(n);
    (pNode->pPrev)->pNext = pNode->pNext;
    (pNode->pNext)->pPrev = pNode->pPrev;
    free(pNode);
}

void swapNode(int n1, int n2)
{
    if (n1 > n2)
        SWAP(int, n1, n2)
    ListNode *pNode1 = getNode(n1), *pNode2 = getNode(n2);

    if (n2 - n1 == 1)
    {
        pNode1->pNext = pNode2->pNext;
        pNode2->pPrev = pNode1->pPrev;
        (pNode2->pPrev)->pNext = pNode2;
        (pNode1->pNext)->pPrev = pNode1;
        pNode2->pNext = pNode1;
        pNode1->pPrev = pNode2;
    }
    else if (n1 != n2)
    {
        ListNode *pNextNode1 = pNode1->pNext, *pPrevNode2 = pNode2->pPrev;
        pNode1->pNext = pNode2->pNext;
        pNode2->pPrev = pNode1->pPrev;
        (pNode1->pPrev)->pNext = pNode2;
        (pNode2->pNext)->pPrev = pNode1;
        pNode1->pPrev = pPrevNode2;
        pPrevNode2->pNext = pNode1;
        pNode2->pNext = pNextNode1;
        pNextNode1->pPrev = pNode2;
    }
}

void sortList()
{
    int i, j, n = getListLength();
    ListNode *pNode;
    for (i = 0; i < n - 1; i++)
    {
        for (j = 1; j < n - i; j++)
        {
            pNode = getNode(j);
            if ((pNode->data) > ((pNode->pNext)->data))
                swapNode(j, j + 1);
        }
    }
}

void freeList()
{
    ListNode *pFreeNode, *pTmpNode = rootNode.pNext;
    while (pTmpNode != &rootNode)
    {
        pFreeNode = pTmpNode;
        pTmpNode = pTmpNode->pNext;
        free(pFreeNode);
    }
}

void showList(int n) //If n>0,you can see n-th Node in the List.Otherwise,you can see whole list.
{
    ListNode *pNode;
    if (n > 0)
    {
        pNode = getNode(n);
        printf("%d", pNode->data);
    }
    else
    {
        for (pNode = rootNode.pNext; pNode != &rootNode; pNode = pNode->pNext)
        {
            printf("%d\n", pNode->data);
        }
    }
}