View Code of Problem 3829

    #include<stdio.h>
    int main(){
    	int x=0;
    	int i=0,n,j=0,t=0;
    	int a[100]={0},b[100]={0},rest[100]={0},sum[100]={0};
    //	scanf("%d",&n);
		while(scanf("%d",&n)!=EOF){
    	while(i<=n-1){
    		scanf("%d",&a[i]);
    		i++;
    	}
    	i=0;
    	while(i<=n-1){
    		scanf("%d",&b[i]);
    		i++;
    	}
    	if(a[0]-b[0]>=0){
    		rest[0]=a[0]-b[0];
    		sum[0]=b[0];
    	}else{
    		rest[0]=0;
    		sum[0]=a[0];
    	}
    	i=1;
    	while(i<=n-1){
    		if(a[i]-b[i]>=0){
    			for(j=0;j<i;j++){
    				if(rest[j]>b[i]){
    					sum[i]+=b[i]; 
    					rest[j]-=b[i];
    					}else{
    					sum[i]+=rest[j];
    					rest[j]=0;
    					}
    				}
    			sum[i]+=b[i];
    			rest[i]=a[i]-b[i];
    	}else{
    			for(j=0;j<i;j++){
    				if(rest[j]>b[i]){
    					sum[i]+=b[i];
    					rest[j]-=b[i];
    					}else{
    					sum[i]+=rest[j];
    					rest[j]=0;
    					}
    			}
    			sum[i]+=a[i];
    			rest[i]=0;
			}	
    		i++;
			}	
		for(i=0;i<=n-2;i++)
    		printf("%d ",sum[i]);
    	printf("%d\n",sum[i]);
		}
		return 0;
    }

Double click to view unformatted code.


Back to problem 3829