Showing posts with label Donation. Show all posts
Showing posts with label Donation. Show all posts

Wednesday, June 29, 2022

Donation

 

Donation

Problem Statement
After the decimation, all world went into chaos. People had to rebuild the planet so Shield came up with a donation strategy. They feel all the rich guys need to donate more than the poor guys. So, they make a rule. They would make a donation list in which the donation of each person would be shown. But the rule is that a person can’t pay less than what has already been paid before them. Find the extra amount each person will pay, and also, tell shield the total amount of donation.





import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework

// don't change the name of this class
// you can add inner classes if needed
class Main {
    public static void main (String[] args) {
                Scanner sc = new Scanner(System.in);
                int n = sc.nextInt();
                int a[] = new int[n];
                int b[] = new int[n];
                long sum= 0;
                int max = 0;
                
                for(int i=0;i<n;i++){
                    a[i]=sc.nextInt();
                    sum += a[i];
                    max = max<a[i]?a[i]:max ; 

                    if (a[i]>=max){
                        b[i]=0;
                    }
                    else{
                        b[i]= max-a[i];
                        sum += b[i];
                    }   
                }
                for(int i=0;i<n;i++){
                    System.out.print(b[i]+" ");
                }
                System.out.println();
                System.out.print(sum);

            } 
    }

ads vert

Basic HTML Tables - Layout, HTML Tables, Attributes, Aside, Footer, Tr tag, Td tag, Th tag, Tbody

  Basic HTML Tables - Layout, HTML Tables, Attributes, Aside, Footer, Tr tag, Td tag, Th tag, Tbody < table >      < thead >    ...