import java.util.Scanner;

class Main{
  public static void main(String args[]){
    Scanner scan;
    int A;
    int B;
    int C;
    int K;
    int Aa;
    int Bb;
    int Cc;

    scan = new Scanner(System.in);
    A = scan.nextInt();
    B = scan.nextInt();
    C = scan.nextInt();
    K = scan.nextInt();

    if (K >= A){
        Aa = A;
        K = K - Aa;
        if (K == 0){
            System.out.println (1*Aa);
        }else{
            if ( K >= B){
                Bb = B;
                K = K - Bb;
                if (K == 0){
                    System.out.println(1*Aa + 0*Bb);
                }else {
                    if (K >= C){
                        Cc = C;
                        K = K - Cc ;
                        if(K == 0){
                            System.out.println(1*Aa + 0*Bb + (-1*Cc));
                        }else{
                            System.out.println(1*Aa + 0*K);
                        }
                        }else{
                            System.out.println(1*Aa + 0*Bb + (-1*K));
                        }
                    }
                }
                }
    }else{
        System.out.println(1*K);
        }

    }
} import java.util.Scanner; Scanner java.util class Main{
  public static void main(String args[]){
    Scanner scan;
    int A;
    int B;
    int C;
    int K;
    int Aa;
    int Bb;
    int Cc;

    scan = new Scanner(System.in);
    A = scan.nextInt();
    B = scan.nextInt();
    C = scan.nextInt();
    K = scan.nextInt();

    if (K >= A){
        Aa = A;
        K = K - Aa;
        if (K == 0){
            System.out.println (1*Aa);
        }else{
            if ( K >= B){
                Bb = B;
                K = K - Bb;
                if (K == 0){
                    System.out.println(1*Aa + 0*Bb);
                }else {
                    if (K >= C){
                        Cc = C;
                        K = K - Cc ;
                        if(K == 0){
                            System.out.println(1*Aa + 0*Bb + (-1*Cc));
                        }else{
                            System.out.println(1*Aa + 0*K);
                        }
                        }else{
                            System.out.println(1*Aa + 0*Bb + (-1*K));
                        }
                    }
                }
                }
    }else{
        System.out.println(1*K);
        }

    }
} Main public static void main(String args[]){
    Scanner scan;
    int A;
    int B;
    int C;
    int K;
    int Aa;
    int Bb;
    int Cc;

    scan = new Scanner(System.in);
    A = scan.nextInt();
    B = scan.nextInt();
    C = scan.nextInt();
    K = scan.nextInt();

    if (K >= A){
        Aa = A;
        K = K - Aa;
        if (K == 0){
            System.out.println (1*Aa);
        }else{
            if ( K >= B){
                Bb = B;
                K = K - Bb;
                if (K == 0){
                    System.out.println(1*Aa + 0*Bb);
                }else {
                    if (K >= C){
                        Cc = C;
                        K = K - Cc ;
                        if(K == 0){
                            System.out.println(1*Aa + 0*Bb + (-1*Cc));
                        }else{
                            System.out.println(1*Aa + 0*K);
                        }
                        }else{
                            System.out.println(1*Aa + 0*Bb + (-1*K));
                        }
                    }
                }
                }
    }else{
        System.out.println(1*K);
        }

    } main {
    Scanner scan;
    int A;
    int B;
    int C;
    int K;
    int Aa;
    int Bb;
    int Cc;

    scan = new Scanner(System.in);
    A = scan.nextInt();
    B = scan.nextInt();
    C = scan.nextInt();
    K = scan.nextInt();

    if (K >= A){
        Aa = A;
        K = K - Aa;
        if (K == 0){
            System.out.println (1*Aa);
        }else{
            if ( K >= B){
                Bb = B;
                K = K - Bb;
                if (K == 0){
                    System.out.println(1*Aa + 0*Bb);
                }else {
                    if (K >= C){
                        Cc = C;
                        K = K - Cc ;
                        if(K == 0){
                            System.out.println(1*Aa + 0*Bb + (-1*Cc));
                        }else{
                            System.out.println(1*Aa + 0*K);
                        }
                        }else{
                            System.out.println(1*Aa + 0*Bb + (-1*K));
                        }
                    }
                }
                }
    }else{
        System.out.println(1*K);
        }

    } Scanner scan; scan int A; A int B; B int C; C int K; K int Aa; Aa int Bb; Bb int Cc; Cc scan = new Scanner(System.in) scan new Scanner(System.in) A = scan.nextInt() A scan.nextInt() scan.nextInt scan B = scan.nextInt() B scan.nextInt() scan.nextInt scan C = scan.nextInt() C scan.nextInt() scan.nextInt scan K = scan.nextInt() K scan.nextInt() scan.nextInt scan if (K >= A){
        Aa = A;
        K = K - Aa;
        if (K == 0){
            System.out.println (1*Aa);
        }else{
            if ( K >= B){
                Bb = B;
                K = K - Bb;
                if (K == 0){
                    System.out.println(1*Aa + 0*Bb);
                }else {
                    if (K >= C){
                        Cc = C;
                        K = K - Cc ;
                        if(K == 0){
                            System.out.println(1*Aa + 0*Bb + (-1*Cc));
                        }else{
                            System.out.println(1*Aa + 0*K);
                        }
                        }else{
                            System.out.println(1*Aa + 0*Bb + (-1*K));
                        }
                    }
                }
                }
    }else{
        System.out.println(1*K);
        } K >= A K A {
        Aa = A;
        K = K - Aa;
        if (K == 0){
            System.out.println (1*Aa);
        }else{
            if ( K >= B){
                Bb = B;
                K = K - Bb;
                if (K == 0){
                    System.out.println(1*Aa + 0*Bb);
                }else {
                    if (K >= C){
                        Cc = C;
                        K = K - Cc ;
                        if(K == 0){
                            System.out.println(1*Aa + 0*Bb + (-1*Cc));
                        }else{
                            System.out.println(1*Aa + 0*K);
                        }
                        }else{
                            System.out.println(1*Aa + 0*Bb + (-1*K));
                        }
                    }
                }
                }
    } Aa = A Aa A K = K - Aa K K - Aa K Aa if (K == 0){
            System.out.println (1*Aa);
        }else{
            if ( K >= B){
                Bb = B;
                K = K - Bb;
                if (K == 0){
                    System.out.println(1*Aa + 0*Bb);
                }else {
                    if (K >= C){
                        Cc = C;
                        K = K - Cc ;
                        if(K == 0){
                            System.out.println(1*Aa + 0*Bb + (-1*Cc));
                        }else{
                            System.out.println(1*Aa + 0*K);
                        }
                        }else{
                            System.out.println(1*Aa + 0*Bb + (-1*K));
                        }
                    }
                }
                } K == 0 K 0 {
            System.out.println (1*Aa);
        } System.out.println (1*Aa) System.out.println System.out System System.out 1*Aa 1 Aa {
            if ( K >= B){
                Bb = B;
                K = K - Bb;
                if (K == 0){
                    System.out.println(1*Aa + 0*Bb);
                }else {
                    if (K >= C){
                        Cc = C;
                        K = K - Cc ;
                        if(K == 0){
                            System.out.println(1*Aa + 0*Bb + (-1*Cc));
                        }else{
                            System.out.println(1*Aa + 0*K);
                        }
                        }else{
                            System.out.println(1*Aa + 0*Bb + (-1*K));
                        }
                    }
                }
                } if ( K >= B){
                Bb = B;
                K = K - Bb;
                if (K == 0){
                    System.out.println(1*Aa + 0*Bb);
                }else {
                    if (K >= C){
                        Cc = C;
                        K = K - Cc ;
                        if(K == 0){
                            System.out.println(1*Aa + 0*Bb + (-1*Cc));
                        }else{
                            System.out.println(1*Aa + 0*K);
                        }
                        }else{
                            System.out.println(1*Aa + 0*Bb + (-1*K));
                        }
                    }
                } K >= B K B {
                Bb = B;
                K = K - Bb;
                if (K == 0){
                    System.out.println(1*Aa + 0*Bb);
                }else {
                    if (K >= C){
                        Cc = C;
                        K = K - Cc ;
                        if(K == 0){
                            System.out.println(1*Aa + 0*Bb + (-1*Cc));
                        }else{
                            System.out.println(1*Aa + 0*K);
                        }
                        }else{
                            System.out.println(1*Aa + 0*Bb + (-1*K));
                        }
                    }
                } Bb = B Bb B K = K - Bb K K - Bb K Bb if (K == 0){
                    System.out.println(1*Aa + 0*Bb);
                }else {
                    if (K >= C){
                        Cc = C;
                        K = K - Cc ;
                        if(K == 0){
                            System.out.println(1*Aa + 0*Bb + (-1*Cc));
                        }else{
                            System.out.println(1*Aa + 0*K);
                        }
                        }else{
                            System.out.println(1*Aa + 0*Bb + (-1*K));
                        }
                    } K == 0 K 0 {
                    System.out.println(1*Aa + 0*Bb);
                } System.out.println(1*Aa + 0*Bb) System.out.println System.out System System.out 1*Aa + 0*Bb 1*Aa 1 Aa 0*Bb 0 Bb {
                    if (K >= C){
                        Cc = C;
                        K = K - Cc ;
                        if(K == 0){
                            System.out.println(1*Aa + 0*Bb + (-1*Cc));
                        }else{
                            System.out.println(1*Aa + 0*K);
                        }
                        }else{
                            System.out.println(1*Aa + 0*Bb + (-1*K));
                        }
                    } if (K >= C){
                        Cc = C;
                        K = K - Cc ;
                        if(K == 0){
                            System.out.println(1*Aa + 0*Bb + (-1*Cc));
                        }else{
                            System.out.println(1*Aa + 0*K);
                        }
                        }else{
                            System.out.println(1*Aa + 0*Bb + (-1*K));
                        } K >= C K C {
                        Cc = C;
                        K = K - Cc ;
                        if(K == 0){
                            System.out.println(1*Aa + 0*Bb + (-1*Cc));
                        }else{
                            System.out.println(1*Aa + 0*K);
                        }
                        } Cc = C Cc C K = K - Cc K K - Cc K Cc if(K == 0){
                            System.out.println(1*Aa + 0*Bb + (-1*Cc));
                        }else{
                            System.out.println(1*Aa + 0*K);
                        } K == 0 K 0 {
                            System.out.println(1*Aa + 0*Bb + (-1*Cc));
                        } System.out.println(1*Aa + 0*Bb + (-1*Cc)) System.out.println System.out System System.out 1*Aa + 0*Bb + (-1*Cc) 1*Aa + 0*Bb + (-1*Cc) 1*Aa 1 Aa 0*Bb 0 Bb (-1*Cc) -1 1 Cc {
                            System.out.println(1*Aa + 0*K);
                        } System.out.println(1*Aa + 0*K) System.out.println System.out System System.out 1*Aa + 0*K 1*Aa 1 Aa 0*K 0 K {
                            System.out.println(1*Aa + 0*Bb + (-1*K));
                        } System.out.println(1*Aa + 0*Bb + (-1*K)) System.out.println System.out System System.out 1*Aa + 0*Bb + (-1*K) 1*Aa + 0*Bb + (-1*K) 1*Aa 1 Aa 0*Bb 0 Bb (-1*K) -1 1 K {
        System.out.println(1*K);
        } System.out.println(1*K) System.out.println System.out System System.out 1*K 1 K String args[] args
import java.util.Scanner;

class Main{
  public static void main(String args[]){
    Scanner scan;
    int A;
    int B;
    int C;
    int K;
    int Aa;
    int Bb;
    int Cc;

    scan = new Scanner(System.in);
    A = scan.nextInt();
    B = scan.nextInt();
    C = scan.nextInt();
    K = scan.nextInt();

    if (K >= A){
        Aa = A;
        K = K - Aa;
        if (K == 0){
            System.out.println (1*Aa);
        }else{
            if ( K >= B){
                Bb = B;
                K = K - Bb;
                if (K == 0){
                    System.out.println(1*Aa + 0*Bb);
                }else {
                    if (K >= C){
                        Cc = C;
                        K = K - Cc ;
                        if(K == 0){
                            System.out.println(1*Aa + 0*Bb + (-1*Cc));
                        }else{
                            System.out.println(1*Aa + 0*K);
                        }
                        }else{
                            System.out.println(1*Aa + 0*Bb + (-1*K));
                        }
                    }
                }
                }
    }else{
        System.out.println(1*K);
        }

    }
}