Problem:
Score : 300 points

Problem Statement

Snuke loves puzzles.

Today, he is working on a puzzle using S- and c-shaped pieces.
In this puzzle, you can combine two c-shaped pieces into one S-shaped piece, as shown in the figure below:

Snuke decided to create as many Scc groups as possible by putting together one S-shaped piece and two c-shaped pieces.

Find the maximum number of Scc groups that can be created when Snuke has N S-shaped pieces and M c-shaped pieces.

Constraints

1 ≤ N,M ≤ 10^{12}

Input

The input is given from Standard Input in the following format:

N M

Output

Print the answer.

Sample Input 1

1 6

Sample Output 1

2

Two Scc groups can be created as follows:

Combine two c-shaped pieces into one S-shaped piece

Create two Scc groups, each from one S-shaped piece and two c-shaped pieces

Sample Input 2

12345 678901

Sample Output 2

175897

Solution in Kotlin (1.0.0) (judge verdict: Time Limit Exceeded)

import java.util.*

fun main(args : Array<String>) {
    val sc = Scanner(System.`in`)
    val n = sc.nextLong()
    val m = sc.nextLong()
    println(problem55c(n, m))
}

fun problem55c(s: Long, c: Long): Long {
    var sTmp = s
    var cTmp = c
    var count = Math.min(cTmp / 2, sTmp)
    sTmp -= count
    cTmp -= count * 2
    if (sTmp == 0L && cTmp >= 2) {
        cTmp -= 2
        sTmp++
    }
    if (sTmp * 10 < cTmp / 2) {
        count += cTmp * 2 / 10
        cTmp /= 10 / 2
        if (cTmp % 2 != 0L) {
            cTmp++
        }
    }
    while (sTmp >= 1 && cTmp >= 2) {
        count++
        sTmp--
        cTmp -= 2
        if (sTmp == 0L && cTmp >= 2) {
            cTmp -= 2
            sTmp++
        }
    }
    return count
}