Problem:
Score : 200 points

Problem Statement

We have A cards, each of which has an integer 1 written on it. Similarly, we also have B cards with 0s and C cards with -1s.

We will pick up K among these cards. What is the maximum possible sum of the numbers written on the cards chosen?

Constraints

All values in input are integers.

0 \leq A, B, C

1 \leq K \leq A + B + C \leq 2 \times 10^9

Input

Input is given from Standard Input in the following format:

A B C K

Output

Print the maximum possible sum of the numbers written on the cards chosen.

Sample Input 1

2 1 1 3

Sample Output 1

2

Consider picking up two cards with 1s and one card with a 0.
In this case, the sum of the numbers written on the cards is 2, which is the maximum possible value.

Sample Input 2

1 2 3 4

Sample Output 2

0

Sample Input 3

2000000000 0 0 2000000000

Sample Output 3

2000000000

Solution in C# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using static System.Console;
using static Atcoder_Template_NetCore3.MyUtil;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Numerics;
using System.Threading.Tasks;
using System.IO;

namespace Atcoder_Template_NetCore3
{
    class Program
    {
        static void Main()
        {
            var input = ReadintArray();

            var ans = 0;
            var nokori = input[3];

            var n = 1;
            for (int i = 0; nokori > 0; i++)
            {
                Draw(1 - i, input[i], nokori);
            }

            WriteLine(ans);

            void Draw(int value, int count, int target)
            {
                if (count > target)
                {
                    ans += value * target;
                    nokori -= target;
                }
                else
                {
                    ans += value * count;
                    nokori -= count;
                }
            }
        }
    }

    static class MyUtil
    {
        /// <summary>
        /// 指定の桁のビットを取得します。
        /// </summary>
        /// <param name="num"></param>
        /// <param name="count">取得する桁。</param>
        /// <returns></returns>
        public static int GetBit(this int num, int count)
        {
            int mask = (int)Math.Pow(2, count - 1);
            return (num & mask) == 0 ? 0 : 1;
        }

        /// <summary>
        /// <c>int</c>を2進数ビット配列に変換します。
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static int[] ToBitArray(this int num)
        {
            const int size = sizeof(int) * 8;
            var array = new int[size];
            for (int i = 0; i < size; i++)
            {
                array[i] = num.GetBit(size - i);
            }
            return array;
        }

        #region AutoGeneratedCode 整数読み取り系
        public static ValueTuple<int, int> ReadInt2()
        {
            var input = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray();
            return (input[0], input[1]);
        }
        public static ValueTuple<long, long> ReadLong2()
        {
            var input = Console.ReadLine().Split(' ').Select(s => long.Parse(s)).ToArray();
            return (input[0], input[1]);
        }
        public static ValueTuple<int, int, int> ReadInt3()
        {
            var input = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray();
            return (input[0], input[1], input[2]);
        }
        public static ValueTuple<long, long, long> ReadLong3()
        {
            var input = Console.ReadLine().Split(' ').Select(s => long.Parse(s)).ToArray();
            return (input[0], input[1], input[2]);
        }
        public static ValueTuple<int, int, int, int> ReadInt4()
        {
            var input = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray();
            return (input[0], input[1], input[2], input[3]);
        }
        public static ValueTuple<long, long, long, long> ReadLong4()
        {
            var input = Console.ReadLine().Split(' ').Select(s => long.Parse(s)).ToArray();
            return (input[0], input[1], input[2], input[3]);
        }
        public static ValueTuple<int, int, int, int, int> ReadInt5()
        {
            var input = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray();
            return (input[0], input[1], input[2], input[3], input[4]);
        }
        public static ValueTuple<long, long, long, long, long> ReadLong5()
        {
            var input = Console.ReadLine().Split(' ').Select(s => long.Parse(s)).ToArray();
            return (input[0], input[1], input[2], input[3], input[4]);
        }
        public static ValueTuple<int, int, int, int, int, int> ReadInt6()
        {
            var input = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray();
            return (input[0], input[1], input[2], input[3], input[4], input[5]);
        }
        public static ValueTuple<long, long, long, long, long, long> ReadLong6()
        {
            var input = Console.ReadLine().Split(' ').Select(s => long.Parse(s)).ToArray();
            return (input[0], input[1], input[2], input[3], input[4], input[5]);
        }
        public static ValueTuple<int, int, int, int, int, int, int> ReadInt7()
        {
            var input = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray();
            return (input[0], input[1], input[2], input[3], input[4], input[5], input[6]);
        }
        public static ValueTuple<long, long, long, long, long, long, long> ReadLong7()
        {
            var input = Console.ReadLine().Split(' ').Select(s => long.Parse(s)).ToArray();
            return (input[0], input[1], input[2], input[3], input[4], input[5], input[6]);
        }
        #endregion
        /// <summary>
        /// 標準入力からスペース区切りの数値を <see cref="int"/> 配列として読み取ります。
        /// </summary>
        /// <returns></returns>
        public static int[] ReadintArray()
        {
            return ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray();
        }

        /// <summary>
        /// 標準入力からスペース区切りの数値を <see cref="long"/> 配列として読み取ります。
        /// </summary>
        /// <returns></returns>
        public static long[] ReadlongArray()
        {
            return ReadLine().Split(' ').Select(s => long.Parse(s)).ToArray();
        }

        /// <summary>
        /// <see cref="IEnumerable{T}"/>の各要素を並べてコンソールに出力します。各要素の間には半角スペースが一つ入ります。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="ts"></param>
        public static void WriteToConsole<T>(this IEnumerable<T> ts)
        {
            StringBuilder stringBuilder = new StringBuilder();
            foreach (T t in ts)
            {
                stringBuilder.Append(t.ToString());
                stringBuilder.Append(" ");
            }
            stringBuilder.Remove(stringBuilder.Length - 1, 1);
            Console.WriteLine(stringBuilder.ToString());
        }
    }
}