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<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;//×Ô¶¯È¡Ä£2^64 
const double pi=acos(-1.0);
const int mod = 1e9+7; // 998244353;
const int inf = 0x7fffffff;//2^31-1	
const ll INF = 0x7fffffffffffffff;
const int dx[]={-1,0,1,0},dy[]={0,1,0,-1};
//double begin_time=clock();    double end_time=clock();printf("time= %lf ms\n",end_time-begin_time);
//bool operator < (const mmp &a) const{return x<a.x||x==a.x&&y<a.y;}       //map<int,map<int,int> >z;
//map<int,int>::iterator it;for(it=z.begin();it!=z.end();it++)printf("%d %d\n",it->first,it->second);
//ll pow(ll a,ll b){ll s=1;while(b){if(b&1){s=(s*a)%mod;b--;}b/=2;a=a*a%mod;}return s;}  //¿ìËÙÃÝÈ¡Ä£ 
//lower_bound(begin,end,num); µÚÒ»¸ö>=numµÄÊý×Ö upper_bound(begin,end,num); µÚÒ»¸ö>numµÄÊý×Ö,·µ»ØµØÖ·
//freopen("input.in","r",stdin);     freopen("output.out","w",stdout);      //ÎÄ¼þ¶ÁÐ´Ìá½» 
//reverse(s.begin(),s.end());   //·­×ª×Ö·û´® ||        floor(); ÏòÏÂÈ¡Õû  ceil(); ÏòÉÏÈ¡Õû
//x&(x-1)==0 //ÊÇ2µÄN´Î·½||         x%9 //x¸÷¸öÎ»µÄºÍ||           log10(x)+1   //xµÄÎ»Êý|| 
//ÊýÖáÉÏµÄn¸öµãÒÆ¶¯µ½Í¬Ò»Î»ÖÃ£¬ÒÆ¶¯µ½¸Ã×éÊý×ÖµÄÖÐÎ»ÊýµÄ×Ü¾àÀë×î¶Ì



int main()
{
	int a,b,c,d,e,T;
//	scanf("%d",&T);
//	while(T--)
	{
		scanf("%d%d%d%d",&a,&b,&c,&d);
		e=0;
		if(d>0)
		{
			if(a>=d)
			{
				e+=d;
				d=0;
			}
			else
			{
				e+=a;
				d-=a;
			}
		}
		if(d>0)
		{
			d-=b;
		}
		if(d>0)
		{
			e-=d;
		}
		printf("%d\n",e);
		

		 
		
	}
}