import java.util.*;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);

        int a = Integer.parseInt(sc.next());
        int b = Integer.parseInt(sc.next());
        int c = Integer.parseInt(sc.next());
        int k = Integer.parseInt(sc.next());

        int max = 0;

        if(a < k){
            max += a*1;
            if(b <= k-a){
                max += b*0;
                if(c <= k - a - b){
                    max += c*(-1);
                }else{
                    max += (k - a - b)*(-1);
                }
            }else if(b > k-a){
                max += (k-a)*b;
            }
        }else{
            max += k*1;
        }

        System.out.println(max);
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);

        int a = Integer.parseInt(sc.next());
        int b = Integer.parseInt(sc.next());
        int c = Integer.parseInt(sc.next());
        int k = Integer.parseInt(sc.next());

        int max = 0;

        if(a < k){
            max += a*1;
            if(b <= k-a){
                max += b*0;
                if(c <= k - a - b){
                    max += c*(-1);
                }else{
                    max += (k - a - b)*(-1);
                }
            }else if(b > k-a){
                max += (k-a)*b;
            }
        }else{
            max += k*1;
        }

        System.out.println(max);
    }
} Main public static void main(String[] args){
        Scanner sc = new Scanner(System.in);

        int a = Integer.parseInt(sc.next());
        int b = Integer.parseInt(sc.next());
        int c = Integer.parseInt(sc.next());
        int k = Integer.parseInt(sc.next());

        int max = 0;

        if(a < k){
            max += a*1;
            if(b <= k-a){
                max += b*0;
                if(c <= k - a - b){
                    max += c*(-1);
                }else{
                    max += (k - a - b)*(-1);
                }
            }else if(b > k-a){
                max += (k-a)*b;
            }
        }else{
            max += k*1;
        }

        System.out.println(max);
    } main {
        Scanner sc = new Scanner(System.in);

        int a = Integer.parseInt(sc.next());
        int b = Integer.parseInt(sc.next());
        int c = Integer.parseInt(sc.next());
        int k = Integer.parseInt(sc.next());

        int max = 0;

        if(a < k){
            max += a*1;
            if(b <= k-a){
                max += b*0;
                if(c <= k - a - b){
                    max += c*(-1);
                }else{
                    max += (k - a - b)*(-1);
                }
            }else if(b > k-a){
                max += (k-a)*b;
            }
        }else{
            max += k*1;
        }

        System.out.println(max);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int a = Integer.parseInt(sc.next()); a Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int b = Integer.parseInt(sc.next()); b Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int c = Integer.parseInt(sc.next()); c Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int k = Integer.parseInt(sc.next()); k Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int max = 0; max 0 if(a < k){
            max += a*1;
            if(b <= k-a){
                max += b*0;
                if(c <= k - a - b){
                    max += c*(-1);
                }else{
                    max += (k - a - b)*(-1);
                }
            }else if(b > k-a){
                max += (k-a)*b;
            }
        }else{
            max += k*1;
        } a < k a k {
            max += a*1;
            if(b <= k-a){
                max += b*0;
                if(c <= k - a - b){
                    max += c*(-1);
                }else{
                    max += (k - a - b)*(-1);
                }
            }else if(b > k-a){
                max += (k-a)*b;
            }
        } max += a*1 max a*1 a 1 if(b <= k-a){
                max += b*0;
                if(c <= k - a - b){
                    max += c*(-1);
                }else{
                    max += (k - a - b)*(-1);
                }
            }else if(b > k-a){
                max += (k-a)*b;
            } b <= k-a b k-a k a {
                max += b*0;
                if(c <= k - a - b){
                    max += c*(-1);
                }else{
                    max += (k - a - b)*(-1);
                }
            } max += b*0 max b*0 b 0 if(c <= k - a - b){
                    max += c*(-1);
                }else{
                    max += (k - a - b)*(-1);
                } c <= k - a - b c k - a - b k - a - b k a b {
                    max += c*(-1);
                } max += c*(-1) max c*(-1) c (-1) 1 {
                    max += (k - a - b)*(-1);
                } max += (k - a - b)*(-1) max (k - a - b)*(-1) (k - a - b) k - a - b k a b (-1) 1 if(b > k-a){
                max += (k-a)*b;
            } b > k-a b k-a k a {
                max += (k-a)*b;
            } max += (k-a)*b max (k-a)*b (k-a) k a b {
            max += k*1;
        } max += k*1 max k*1 k 1 System.out.println(max) System.out.println System.out System System.out max String[] args args public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);

        int a = Integer.parseInt(sc.next());
        int b = Integer.parseInt(sc.next());
        int c = Integer.parseInt(sc.next());
        int k = Integer.parseInt(sc.next());

        int max = 0;

        if(a < k){
            max += a*1;
            if(b <= k-a){
                max += b*0;
                if(c <= k - a - b){
                    max += c*(-1);
                }else{
                    max += (k - a - b)*(-1);
                }
            }else if(b > k-a){
                max += (k-a)*b;
            }
        }else{
            max += k*1;
        }

        System.out.println(max);
    }
} public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);

        int a = Integer.parseInt(sc.next());
        int b = Integer.parseInt(sc.next());
        int c = Integer.parseInt(sc.next());
        int k = Integer.parseInt(sc.next());

        int max = 0;

        if(a < k){
            max += a*1;
            if(b <= k-a){
                max += b*0;
                if(c <= k - a - b){
                    max += c*(-1);
                }else{
                    max += (k - a - b)*(-1);
                }
            }else if(b > k-a){
                max += (k-a)*b;
            }
        }else{
            max += k*1;
        }

        System.out.println(max);
    }
} Main
import java.util.*;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);

        int a = Integer.parseInt(sc.next());
        int b = Integer.parseInt(sc.next());
        int c = Integer.parseInt(sc.next());
        int k = Integer.parseInt(sc.next());

        int max = 0;

        if(a < k){
            max += a*1;
            if(b <= k-a){
                max += b*0;
                if(c <= k - a - b){
                    max += c*(-1);
                }else{
                    max += (k - a - b)*(-1);
                }
            }else if(b > k-a){
                max += (k-a)*b;
            }
        }else{
            max += k*1;
        }

        System.out.println(max);
    }
}