Problem:
Score : 400 points

Problem Statement

Snuke has one biscuit and zero Japanese yen (the currency) in his pocket.
He will perform the following operations exactly K times in total, in the order he likes:

Hit his pocket, which magically increases the number of biscuits by one.

Exchange A biscuits to 1 yen.

Exchange 1 yen to B biscuits.

Find the maximum possible number of biscuits in Snuke's pocket after K operations.

Constraints

1 \leq K,A,B \leq 10^9

K,A and B are integers.

Input

Input is given from Standard Input in the following format:

K A B

Output

Print the maximum possible number of biscuits in Snuke's pocket after K operations.

Sample Input 1

4 2 6

Sample Output 1

7

The number of biscuits in Snuke's pocket after K operations is maximized as follows:

Hit his pocket. Now he has 2 biscuits and 0 yen.

Exchange 2 biscuits to 1 yen. his pocket. Now he has 0 biscuits and 1 yen.

Hit his pocket. Now he has 1 biscuits and 1 yen.

Exchange 1 yen to 6 biscuits. his pocket. Now he has 7 biscuits and 0 yen.

Sample Input 2

7 3 4

Sample Output 2

8

Sample Input 3

314159265 35897932 384626433

Sample Output 3

48518828981938099

Solution in Go (1.6) (judge verdict: Accepted)

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

//scanで一行づつ読み込む
var sc = bufio.NewScanner(os.Stdin)
var wtr = bufio.NewWriter(os.Stdout)

//sort
// array := []int{5, 1, 4, 2, 3}
// sort.Sort(sort.IntSlice(array))

func main() {
	// a := make([]int, n)
	// A := strings.Split(nextLine(), " ")

	X := strings.Split(nextLine(), " ")

	// Z := strings.Split(nextLine(), " ")

	// n, _ := strconv.Atoi(input[0])

	// memo = make([]bool, n+1, n+2)

	K, _ := strconv.Atoi(X[0])
	A, _ := strconv.Atoi(X[1])
	B, _ := strconv.Atoi(X[2])

	// ans := K+1
	buiscket := int64(K + 1)
	tmp := int64(1)
	money := int64(0)

	for i := 0; i < K; i++ {

		if money < 1 {

			if i >= K-1 {
				tmp++
			} else if tmp < int64(A) {
				tmp++
			} else {
				tmp, money = exchangeToYen(tmp, int64(A))
			}
		} else {
			tmp, money = exchangeToBuiscket(money, tmp, int64(B))
		}
		// fmt.Printf("tmp: %d , money: %d \n", tmp, money)
	}

	buiscket = max(buiscket, tmp)

	fmt.Fprintln(wtr, buiscket)

	_ = wtr.Flush()
}

func addBuiscket(b int64) int64 {
	return b + 1
}
func exchangeToYen(b int64, rate int64) (rest int64, money int64) {
	return b - rate, 1
}
func exchangeToBuiscket(mone int64, buis int64, rate int64) (b int64, money int64) {
	return buis + rate, mone - 1
}

func nextLine() string {
	sc.Scan()
	return sc.Text()
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func max(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}
func com(a, b int) int {

	if a > b {
		return 0
	}
	return abs(a - b)
}

func abs(a int) int {
	if a < 0 {
		return a * -1
	}
	return a
}

type dishh struct {
	first  int
	second int
}

type dish []dishh

func (p dish) Len() int {
	return len(p)
}

func (p dish) Swap(i, j int) {
	p[i], p[j] = p[j], p[i]
}

func (p dish) Less(i, j int) bool {

	return p[i].first+p[i].second > p[j].first+p[j].second
}