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 C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Text;
 
public class Program
{
    public void Proc() {
        Reader.IsDebug = false;
        long[] inpt = Reader.ReadLine().Split(' ').Select(a=>long.Parse(a)).ToArray();
        this.SCount = inpt[0];
        this.CCount = inpt[1];

        long s = this.GetSCount(this.SCount, this.CCount / 2);
        long ans = GetCount(s);
        Console.WriteLine(ans);
    }

    private long GetSCount(long min, long max) {

        long mid1 = min + (max-min)/3;
        long mid2 = min + ((max-min)*2/3);

        long[] vals = new long[]{GetCount(min), GetCount(mid1), GetCount(mid2), GetCount(max)};
        long v = vals.Max();

        if(max-min <= 3) {
            if(vals[0] == v) {
                return min;
            } else if(vals[1] == v) {
                return mid1;
            } else if(vals[2] == v) {
                return mid2;
            }
            return max;
        }

        if(vals[0] == v) {
            return this.GetSCount(min, mid1);
        } else if(vals[1] == v) {
            return this.GetSCount(min, mid2);
        } else if(vals[2] == v) {
            return this.GetSCount(mid1, max);
        } else {
            return this.GetSCount(mid2, max);
        }

    }
    private long GetCount(long s) {
        long makeS = s-this.SCount;

        long c = CCount - (makeS*2);


        return Math.Min(s, c/2);

    }

    private long SCount;
    private long CCount;

    public class Reader {
        public static bool IsDebug = true;
        private static System.IO.StringReader SReader;
        private static string InitText = @"



12345 678901




";
        public static string ReadLine() {
            if(IsDebug) {
                if(SReader == null) {
                    SReader = new System.IO.StringReader(InitText.Trim());
                }
                return SReader.ReadLine();
            } else {
                return Console.ReadLine();
            }
        }
    }
    public static void Main(string[] args)
    {
        Program prg = new Program();
        prg.Proc();
    }
}