import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        Scanner scan = new Scanner(System.in);
        int cards[]  = {1, 0, -1};
        int counts[] = new int[3], n, maxValue;

        for(int i = 0; i < counts.length; i++) {
            counts[i] = scan.nextInt();
        }
        n = scan.nextInt();

        maxValue = 0;
        for(int i = 0; i < counts.length; i++) {
            if(n >= counts[i]) {
                maxValue += cards[i] * counts[i];
                n -= counts[i];
            } else {
                maxValue += cards[i] * n;
                n = 0;
            }
        }

        System.out.println(maxValue);
        scan.close();
        return;
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String args[]) {
        Scanner scan = new Scanner(System.in);
        int cards[]  = {1, 0, -1};
        int counts[] = new int[3], n, maxValue;

        for(int i = 0; i < counts.length; i++) {
            counts[i] = scan.nextInt();
        }
        n = scan.nextInt();

        maxValue = 0;
        for(int i = 0; i < counts.length; i++) {
            if(n >= counts[i]) {
                maxValue += cards[i] * counts[i];
                n -= counts[i];
            } else {
                maxValue += cards[i] * n;
                n = 0;
            }
        }

        System.out.println(maxValue);
        scan.close();
        return;
    }
} Main public static void main(String args[]) {
        Scanner scan = new Scanner(System.in);
        int cards[]  = {1, 0, -1};
        int counts[] = new int[3], n, maxValue;

        for(int i = 0; i < counts.length; i++) {
            counts[i] = scan.nextInt();
        }
        n = scan.nextInt();

        maxValue = 0;
        for(int i = 0; i < counts.length; i++) {
            if(n >= counts[i]) {
                maxValue += cards[i] * counts[i];
                n -= counts[i];
            } else {
                maxValue += cards[i] * n;
                n = 0;
            }
        }

        System.out.println(maxValue);
        scan.close();
        return;
    } main {
        Scanner scan = new Scanner(System.in);
        int cards[]  = {1, 0, -1};
        int counts[] = new int[3], n, maxValue;

        for(int i = 0; i < counts.length; i++) {
            counts[i] = scan.nextInt();
        }
        n = scan.nextInt();

        maxValue = 0;
        for(int i = 0; i < counts.length; i++) {
            if(n >= counts[i]) {
                maxValue += cards[i] * counts[i];
                n -= counts[i];
            } else {
                maxValue += cards[i] * n;
                n = 0;
            }
        }

        System.out.println(maxValue);
        scan.close();
        return;
    } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int cards[]  = {1, 0, -1}; cards {1, 0, -1} 1 0 -1 1 int counts[] = new int[3] counts new int[3] 3 n n maxValue; maxValue for(int i = 0; i < counts.length; i++) {
            counts[i] = scan.nextInt();
        } int i = 0; int i = 0; i 0 i < counts.length i counts.length counts counts.length i++ i++ i {
            counts[i] = scan.nextInt();
        } {
            counts[i] = scan.nextInt();
        } counts[i] = scan.nextInt() counts[i] counts i scan.nextInt() scan.nextInt scan n = scan.nextInt() n scan.nextInt() scan.nextInt scan maxValue = 0 maxValue 0 for(int i = 0; i < counts.length; i++) {
            if(n >= counts[i]) {
                maxValue += cards[i] * counts[i];
                n -= counts[i];
            } else {
                maxValue += cards[i] * n;
                n = 0;
            }
        } int i = 0; int i = 0; i 0 i < counts.length i counts.length counts counts.length i++ i++ i {
            if(n >= counts[i]) {
                maxValue += cards[i] * counts[i];
                n -= counts[i];
            } else {
                maxValue += cards[i] * n;
                n = 0;
            }
        } {
            if(n >= counts[i]) {
                maxValue += cards[i] * counts[i];
                n -= counts[i];
            } else {
                maxValue += cards[i] * n;
                n = 0;
            }
        } if(n >= counts[i]) {
                maxValue += cards[i] * counts[i];
                n -= counts[i];
            } else {
                maxValue += cards[i] * n;
                n = 0;
            } n >= counts[i] n counts[i] counts i {
                maxValue += cards[i] * counts[i];
                n -= counts[i];
            } maxValue += cards[i] * counts[i] maxValue cards[i] * counts[i] cards[i] cards i counts[i] counts i n -= counts[i] n counts[i] counts i {
                maxValue += cards[i] * n;
                n = 0;
            } maxValue += cards[i] * n maxValue cards[i] * n cards[i] cards i n n = 0 n 0 System.out.println(maxValue) System.out.println System.out System System.out maxValue scan.close() scan.close scan return; String args[] args public class Main {
    public static void main(String args[]) {
        Scanner scan = new Scanner(System.in);
        int cards[]  = {1, 0, -1};
        int counts[] = new int[3], n, maxValue;

        for(int i = 0; i < counts.length; i++) {
            counts[i] = scan.nextInt();
        }
        n = scan.nextInt();

        maxValue = 0;
        for(int i = 0; i < counts.length; i++) {
            if(n >= counts[i]) {
                maxValue += cards[i] * counts[i];
                n -= counts[i];
            } else {
                maxValue += cards[i] * n;
                n = 0;
            }
        }

        System.out.println(maxValue);
        scan.close();
        return;
    }
} public class Main {
    public static void main(String args[]) {
        Scanner scan = new Scanner(System.in);
        int cards[]  = {1, 0, -1};
        int counts[] = new int[3], n, maxValue;

        for(int i = 0; i < counts.length; i++) {
            counts[i] = scan.nextInt();
        }
        n = scan.nextInt();

        maxValue = 0;
        for(int i = 0; i < counts.length; i++) {
            if(n >= counts[i]) {
                maxValue += cards[i] * counts[i];
                n -= counts[i];
            } else {
                maxValue += cards[i] * n;
                n = 0;
            }
        }

        System.out.println(maxValue);
        scan.close();
        return;
    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        Scanner scan = new Scanner(System.in);
        int cards[]  = {1, 0, -1};
        int counts[] = new int[3], n, maxValue;

        for(int i = 0; i < counts.length; i++) {
            counts[i] = scan.nextInt();
        }
        n = scan.nextInt();

        maxValue = 0;
        for(int i = 0; i < counts.length; i++) {
            if(n >= counts[i]) {
                maxValue += cards[i] * counts[i];
                n -= counts[i];
            } else {
                maxValue += cards[i] * n;
                n = 0;
            }
        }

        System.out.println(maxValue);
        scan.close();
        return;
    }
}