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++ (GCC 9.2.1) (judge verdict: Accepted)

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <random>

#ifndef LX_DEBUG
// #define LX_DEBUG
#endif

#if defined(__unix__) && !defined(LX_DEBUG) && !defined(LX_LOCAL)
#define USE_MMAP 1
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#else
#define USE_MMAP 0
#endif

using LL = long long;
using ULL = unsigned long long;
#ifdef __SIZEOF_INT128__
using LLL = __int128;
using ULLL = unsigned __int128;
#else
using LLL = long long;
using ULLL = unsigned long long;
#endif

namespace std {
#define def(type) \
    void swap(type& x, type& y) { x ^= y ^= x ^= y; }
    def(char);
    def(signed char);
    def(unsigned char);
    def(short);
    def(unsigned short);
    def(int);
    def(unsigned int);
    def(LL);
    def(ULL);
#ifdef __SIZEOF_INT128__
    def(LLL);
    def(ULLL);
#endif
#undef def
};  // namespace std

#ifdef LX_LOCAL
#include <ctime>
class Time {
public:
    clock_t begin;
    Time() { begin = clock(); }
    ~Time() {
        double t = (clock() - begin) * 1000.0 / static_cast<double>(CLOCKS_PER_SEC);
        fprintf(stderr, "Time used = ");
        if (t >= 60000)
            fprintf(stderr, "%.2lfmin\n", t / 60000.0);
        else if (t >= 1000)
            fprintf(stderr, "%.2lfs\n", t / 1000.0);
        else
            fprintf(stderr, "%.0lfms\n", t);
    }
} timeout;
#endif

using std::swap;
#if __cplusplus >= 201402L
template <typename T, typename P>
inline constexpr auto max(const T& x, const P& y) { return x < y ? y : x; }
template <typename T, typename P>
inline constexpr auto min(const T& x, const P& y) { return x < y ? x : y; }
template <typename T, typename... Args>
inline constexpr auto max(const T& x, const Args&... args) { return max(x, max(args...)); }
template <typename T, typename... Args>
inline constexpr auto min(const T& x, const Args&... args) { return min(x, min(args...)); }
#else
template <typename T, typename P>
inline constexpr auto max(const T& x, const P& y) -> decltype(x < y ? y : x) { return x < y ? y : x; }
template <typename T, typename P>
inline constexpr auto min(const T& x, const P& y) -> decltype(x < y ? x : y) { return x < y ? x : y; }
template <typename T, typename... Args>
inline constexpr auto max(const T& x, const Args&... args) -> decltype(max(x, max(args...))) { return max(x, max(args...)); }
template <typename T, typename... Args>
inline constexpr auto min(const T& x, const Args&... args) -> decltype(min(x, min(args...))) { return min(x, min(args...)); }
#endif
template <typename T>
inline constexpr T max(const T& x) { return x; }
template <typename T>
inline constexpr T min(const T& x) { return x; }
template <typename T, typename... Args>
inline void chkmax(T& d, const Args&... args) { d = max(d, args...); }
template <typename T, typename... Args>
inline void chkmin(T& d, const Args&... args) { d = min(d, args...); }

#define lc (o << 1)
#define rc (o << 1 | 1)
#define lowbit(x) ((x) & (-(x)))
#define all(x) x.begin(), x.end()
#define SZ(x) int(x.size())

std::mt19937 mrand(std::random_device{}());
std::mt19937_64 mrand64(std::random_device{}());

#pragma region IO
constexpr size_t bufSize = 1 << 20;

class In {
private:
    FILE* inFile = nullptr;
#if USE_MMAP
    struct stat st;
    char* ptr;
    int fd;
    size_t pos;
#elif !defined(LX_DEBUG)
    char buf[bufSize], *p1, *p2;
#endif
public:
#ifdef LX_DEBUG
    inline int getch() { return fgetc(inFile); }
    inline void putback(char c) { ungetc(c, inFile); }
    inline void input(FILE* f) { inFile = f; }
    inline void input(const char* str) { inFile = fopen(str, "rb"); }
#elif USE_MMAP
    inline int getch() { return *(ptr + pos) ? *(ptr + (pos++)) : EOF; }
    inline void putback(char c) { pos--; }
    inline void input(FILE* f) {
        if ((inFile = f))
            fd = fileno(inFile), fstat(fd, &st), ptr = (char*)mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0), pos = 0;
    }
    inline void input(const char* str) { fd = open(str, O_RDONLY), fstat(fd, &st), ptr = (char*)mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0), pos = 0; }
#else
    inline int getch() { return (p1 == p2 ? (p2 = (p1 = buf) + fread(buf, 1, bufSize, inFile)) : 0), (p1 == p2) ? EOF : *p1++; }
    inline void putback(char c) { *(--p1) = c; }
    inline void input(FILE* f) { inFile = f, p1 = p2 = buf; }
    inline void input(const char* str) { inFile = fopen(str, "rb"), p1 = p2 = buf; }
#endif
};
class Out {
private:
    FILE* outFile = nullptr;
#ifndef LX_DEBUG
    char pbuf[bufSize], *pp;
#endif
public:
#ifdef LX_DEBUG
    inline void flush() { outFile ? fflush(outFile) : 0; }
    inline void putch(const char c) { fputc(c, outFile); }
#else
    inline void flush() { (outFile ? fwrite(pbuf, 1, pp - pbuf, outFile), fflush(outFile) : 0), pp = pbuf; }
    inline void putch(const char c) { ((pp - pbuf == bufSize) ? fwrite(pbuf, 1, bufSize, outFile), pp = pbuf : 0), *pp++ = c; }
#endif
    inline void output(const char* str) { flush(), outFile = fopen(str, "wb"); }
    inline void output(FILE* f) { flush(), outFile = f; }
};
class IO : public In, public Out {
private:
    long double eps = 1e-8L;
    LLL pow10 = 1000000;
    size_t precision = 6;

public:
    IO(FILE* in = nullptr, FILE* out = nullptr);
    ~IO();
    inline void flush();
    inline static constexpr bool blank(char ch);
    template <typename T, typename... Args>
    bool read(T&, Args&...);
    template <typename T>
    typename std::enable_if<(std::is_signed<T>::value && std::is_integral<T>::value) || std::is_same<typename std::remove_cv<T>::type, LLL>::value, bool>::type read(T&);
    template <typename T>
    typename std::enable_if<(std::is_unsigned<T>::value && std::is_integral<T>::value) || std::is_same<typename std::remove_cv<T>::type, ULLL>::value, bool>::type read(T&);
    template <typename T>
    typename std::enable_if<std::is_floating_point<T>::value, bool>::type read(T&);
    template <typename T>
    typename std::enable_if<std::is_integral<T>::value || std::is_floating_point<T>::value || std::is_same<typename std::remove_cv<T>::type, LLL>::value || std::is_same<typename std::remove_cv<T>, ULLL>::value, T>::type read();
    bool read(char&);
    bool read(char*);
    bool readline(char*);
    inline void setprecision(size_t = 6u);
    template <typename T, typename... Args>
    void write(const T&, const Args&...);
    inline void write() const {}
    template <typename T>
    typename std::enable_if<(std::is_signed<T>::value && std::is_integral<T>::value) || std::is_same<T, LLL>::value, void>::type write(T);
    template <typename T>
    typename std::enable_if<(std::is_unsigned<T>::value && std::is_integral<T>::value) || std::is_same<T, ULLL>::value, void>::type write(T);
    inline void write(char);
    template <typename T>
    typename std::enable_if<std::is_floating_point<T>::value, void>::type write(T);
    inline void write(bool);
    void write(char*);
    void write(const char*);
    template <typename... Args>
    void writeln(Args...);
} io(stdin, stdout), err(nullptr, stderr);
template <typename... Args>
inline void writeln(Args... x) { io.write(x...), io.putch('\n'); }
#define dbg(x) err.write(#x " = "), err.writeln(x)
#pragma endregion IO

int main() {
    int a, b, c, k;
    io.read(a, b, c, k);
    if (k <= a)
        writeln(k);
    else if (k <= a + b)
        writeln(a);
    else
        writeln(a - (k - a - b));
    return 0;
}

#define isdigit(x) (x >= '0' && x <= '9')
IO::IO(FILE* in, FILE* out) { input(in), output(out); }
IO::~IO() { flush(); }
inline void IO::flush() { Out::flush(); }
inline constexpr bool IO::blank(char ch) { return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; }
template <typename T, typename... Args>
bool IO::read(T& x, Args&... args) { return read(x) && (sizeof...(args) ? read(args...) : true); }
template <typename T>
typename std::enable_if<(std::is_signed<T>::value && std::is_integral<T>::value) || std::is_same<typename std::remove_cv<T>::type, LLL>::value, bool>::type IO::read(T& x) {
    x = 0;
    bool sign = false;
    int ch = getch();
    while (!isdigit(ch) && ~ch)
        sign = (ch == '-'), ch = getch();
    if (!(~ch))
        return false;
    while (isdigit(ch))
        x = (x << 1) + (x << 3) + (ch ^ '0'), ch = getch();
    if (~ch)
        putback(ch);
    x = sign ? (~x + 1) : x;
    return true;
}
template <typename T>
typename std::enable_if<(std::is_unsigned<T>::value && std::is_integral<T>::value) || std::is_same<typename std::remove_cv<T>::type, ULLL>::value, bool>::type IO::read(T& x) {
    x = 0;
    int ch = getch();
    while (!isdigit(ch) && ~ch)
        ch = getch();
    if (!(~ch))
        return false;
    while (isdigit(ch))
        x = (x << 1) + (x << 3) + (ch ^ '0'), ch = getch();
    if (~ch)
        putback(ch);
    return true;
}
template <typename T>
typename std::enable_if<std::is_floating_point<T>::value, bool>::type IO::read(T& x) {
    x = 0;
    T tmp = 1;
    bool sign = false;
    int ch = getch();
    while (!isdigit(ch) && ~ch)
        sign = (ch == '-'), ch = getch();
    if (!(~ch))
        return false;
    while (isdigit(ch))
        x = x * 10 + (ch ^ '0'), ch = getch();
    if (ch == '.')
        for (ch = getch(); isdigit(ch); ch = getch())
            x += (tmp /= 10.0) * (ch ^ '0');
    if (~ch)
        putback(ch);
    x = sign ? (-x) : x;
    return true;
}
template <typename T>
typename std::enable_if<std::is_integral<T>::value || std::is_floating_point<T>::value || std::is_same<typename std::remove_cv<T>::type, LLL>::value || std::is_same<typename std::remove_cv<T>, ULLL>::value, T>::type IO::read() {
    typename std::remove_cv<T>::type x;
    read(x);
    return x;
}
bool IO::read(char& ch) {
    do
        ch = getch();
    while (blank(ch) && ~ch);
    return ~ch;
}
bool IO::read(char* s) {
    int ch = getch();
    while (blank(ch))
        ch = getch();
    if (!(~ch))
        return false;
    while (!blank(ch) && ~ch)
        *s++ = ch, ch = getch();
    *s = 0;
    if (~ch)
        putback(ch);
    return true;
}
bool IO::readline(char* s) {
    int ch = getch();
    while (blank(ch) && (ch ^ '\n'))
        ch = getch();
    if (!(~ch))
        return false;
    while ((ch ^ '\n') && ~ch)
        *s++ = ch, ch = getch();
    *s = 0;
    return true;
}
inline void IO::setprecision(size_t n) {
    precision = n;
    eps = powl(10.0L, -int(precision) - 2);
    pow10 = powl(10.0L, precision) + eps;
}
template <typename T, typename... Args>
void IO::write(const T& x, const Args&... args) {
    write(x);
    if (sizeof...(args))
        write(args...);
}
template <typename T>
typename std::enable_if<(std::is_signed<T>::value && std::is_integral<T>::value) || std::is_same<T, LLL>::value, void>::type IO::write(T x) {
    if (x < 0)
        x = ~x + 1, putch('-');
    static int sta[40], top = 0;
    do
        sta[top++] = x % 10, x /= 10;
    while (x);
    while (top)
        putch(sta[--top] ^ '0');
}
template <typename T>
typename std::enable_if<(std::is_unsigned<T>::value && std::is_integral<T>::value) || std::is_same<T, ULLL>::value, void>::type IO::write(T x) {
    static int sta[40], top = 0;
    do
        sta[top++] = x % 10, x /= 10;
    while (x);
    while (top)
        putch(sta[--top] ^ '0');
}
inline void IO::write(bool x) { putch(x ^ 48); }
inline void IO::write(char c) { putch(c); }
template <typename T>
typename std::enable_if<std::is_floating_point<T>::value>::type IO::write(T x) {
    if (x == 0) {
        putch('0'), putch('.');
        for (size_t i = 1; i <= precision; i++)
            putch('0');
        return;
    }
    if (x < 0)
        putch('-'), x = -x;
    T res = (LLL)(x * pow10 + 0.5) / (pow10 * 1.0);
    LLL y = LLL(res * pow10 + eps) % pow10;
    write(LLL(res + eps));
    if (precision) {
        putch('.');
        static size_t sta[100], p = 0;
        while (p < precision)
            sta[++p] = y % 10, y /= 10;
        for (int i = p; i >= 1; i--)
            putch(sta[i] ^ 48);
    }
}
void IO::write(char* s) {
    while (*s)
        putch(*s++);
}
void IO::write(const char* s) {
    while (*s)
        putch(*s++);
}
template <typename... Args>
void IO::writeln(Args... x) { write(x...), putch('\n'); }
#undef isdigit