import java.util.*;
public class Main{

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        int cnt  =0;

        while(true){
            if(a>k){
                cnt+=k;
                k=0;
                break;
            }else{
                cnt+=a;
                k-=a;
            }
            if(k<=0){
                break;

            }
            if(b>k){
                k=0;
                break;
            }else{
                k-=b;
            }
            if(k<=0){
                break;
            }

            if(c>k){
                cnt-=k;
                k=0;
                break;
            }else{
                cnt-=c;
                k-=c;
            }
            if(k<=0){
                break;
            }



        }
        System.out.println(cnt);
    }
}



 import java.util.*; util.* java public class Main{

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        int cnt  =0;

        while(true){
            if(a>k){
                cnt+=k;
                k=0;
                break;
            }else{
                cnt+=a;
                k-=a;
            }
            if(k<=0){
                break;

            }
            if(b>k){
                k=0;
                break;
            }else{
                k-=b;
            }
            if(k<=0){
                break;
            }

            if(c>k){
                cnt-=k;
                k=0;
                break;
            }else{
                cnt-=c;
                k-=c;
            }
            if(k<=0){
                break;
            }



        }
        System.out.println(cnt);
    }
} Main public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        int cnt  =0;

        while(true){
            if(a>k){
                cnt+=k;
                k=0;
                break;
            }else{
                cnt+=a;
                k-=a;
            }
            if(k<=0){
                break;

            }
            if(b>k){
                k=0;
                break;
            }else{
                k-=b;
            }
            if(k<=0){
                break;
            }

            if(c>k){
                cnt-=k;
                k=0;
                break;
            }else{
                cnt-=c;
                k-=c;
            }
            if(k<=0){
                break;
            }



        }
        System.out.println(cnt);
    } main {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        int cnt  =0;

        while(true){
            if(a>k){
                cnt+=k;
                k=0;
                break;
            }else{
                cnt+=a;
                k-=a;
            }
            if(k<=0){
                break;

            }
            if(b>k){
                k=0;
                break;
            }else{
                k-=b;
            }
            if(k<=0){
                break;
            }

            if(c>k){
                cnt-=k;
                k=0;
                break;
            }else{
                cnt-=c;
                k-=c;
            }
            if(k<=0){
                break;
            }



        }
        System.out.println(cnt);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int a = sc.nextInt(); a sc.nextInt() sc.nextInt sc int b = sc.nextInt(); b sc.nextInt() sc.nextInt sc int c = sc.nextInt(); c sc.nextInt() sc.nextInt sc int k = sc.nextInt(); k sc.nextInt() sc.nextInt sc int cnt  =0; cnt 0 while(true){
            if(a>k){
                cnt+=k;
                k=0;
                break;
            }else{
                cnt+=a;
                k-=a;
            }
            if(k<=0){
                break;

            }
            if(b>k){
                k=0;
                break;
            }else{
                k-=b;
            }
            if(k<=0){
                break;
            }

            if(c>k){
                cnt-=k;
                k=0;
                break;
            }else{
                cnt-=c;
                k-=c;
            }
            if(k<=0){
                break;
            }



        } true {
            if(a>k){
                cnt+=k;
                k=0;
                break;
            }else{
                cnt+=a;
                k-=a;
            }
            if(k<=0){
                break;

            }
            if(b>k){
                k=0;
                break;
            }else{
                k-=b;
            }
            if(k<=0){
                break;
            }

            if(c>k){
                cnt-=k;
                k=0;
                break;
            }else{
                cnt-=c;
                k-=c;
            }
            if(k<=0){
                break;
            }



        } if(a>k){
                cnt+=k;
                k=0;
                break;
            }else{
                cnt+=a;
                k-=a;
            } a>k a k {
                cnt+=k;
                k=0;
                break;
            } cnt+=k cnt k k=0 k 0 break; {
                cnt+=a;
                k-=a;
            } cnt+=a cnt a k-=a k a if(k<=0){
                break;

            } k<=0 k 0 {
                break;

            } break; if(b>k){
                k=0;
                break;
            }else{
                k-=b;
            } b>k b k {
                k=0;
                break;
            } k=0 k 0 break; {
                k-=b;
            } k-=b k b if(k<=0){
                break;
            } k<=0 k 0 {
                break;
            } break; if(c>k){
                cnt-=k;
                k=0;
                break;
            }else{
                cnt-=c;
                k-=c;
            } c>k c k {
                cnt-=k;
                k=0;
                break;
            } cnt-=k cnt k k=0 k 0 break; {
                cnt-=c;
                k-=c;
            } cnt-=c cnt c k-=c k c if(k<=0){
                break;
            } k<=0 k 0 {
                break;
            } break; System.out.println(cnt) System.out.println System.out System System.out cnt String[] args args public class Main{

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        int cnt  =0;

        while(true){
            if(a>k){
                cnt+=k;
                k=0;
                break;
            }else{
                cnt+=a;
                k-=a;
            }
            if(k<=0){
                break;

            }
            if(b>k){
                k=0;
                break;
            }else{
                k-=b;
            }
            if(k<=0){
                break;
            }

            if(c>k){
                cnt-=k;
                k=0;
                break;
            }else{
                cnt-=c;
                k-=c;
            }
            if(k<=0){
                break;
            }



        }
        System.out.println(cnt);
    }
} public class Main{

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        int cnt  =0;

        while(true){
            if(a>k){
                cnt+=k;
                k=0;
                break;
            }else{
                cnt+=a;
                k-=a;
            }
            if(k<=0){
                break;

            }
            if(b>k){
                k=0;
                break;
            }else{
                k-=b;
            }
            if(k<=0){
                break;
            }

            if(c>k){
                cnt-=k;
                k=0;
                break;
            }else{
                cnt-=c;
                k-=c;
            }
            if(k<=0){
                break;
            }



        }
        System.out.println(cnt);
    }
} Main
import java.util.*;
public class Main{

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        int cnt  =0;

        while(true){
            if(a>k){
                cnt+=k;
                k=0;
                break;
            }else{
                cnt+=a;
                k-=a;
            }
            if(k<=0){
                break;

            }
            if(b>k){
                k=0;
                break;
            }else{
                k-=b;
            }
            if(k<=0){
                break;
            }

            if(c>k){
                cnt-=k;
                k=0;
                break;
            }else{
                cnt-=c;
                k-=c;
            }
            if(k<=0){
                break;
            }



        }
        System.out.println(cnt);
    }
}




