daspriyajit10's blog

By daspriyajit10, history, 4 years ago, In English

/*package whatever //do not write package name here */

import java.util.*; import java.lang.*; public class GFG { public static void main (String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt();

    while(t-->0){

        int n=sc.nextInt();
        int k=sc.nextInt();
        int z=sc.nextInt();
int k1=k-1;
        int a[]=new int[n];
        for(int i=0;i<n;i++){
            a[i]=sc.nextInt();


            }

            int sum=a[0];

           if(n>1){
               sum=sum+a[1];

           }


            for(int i=1;i<n-1;i++){

                if( a[i+1]<a[i-1] && z>0 &&  k1>0 ){

                   int flag=0;int count=0;int sum1=sum;
                  int res=a[i-1];int re=0;int d=0;
                    for(int j=i+1;j<=k1 &&  j<n;j++){
                    count++;

                        if(a[j]>=res){

                           res=a[j];
                           re=a[j-1];
                           d=j;

                            flag=1;


                        }
                        if(a[j]<res && flag==1){

                            break;
                        }
                    }

                    if(flag==1){
                        for(int l=i+1;l<=d;l++){
                            sum1=sum1+a[l];
                        }
                       int u=0;
                       int v=0;
                        int x=k-(count+i);
                        int x1=x/2;
                        int rem=x-x1;
                       int y1=Math.min(rem,z);
                       if(y1<x1){
                          u=res*y1+sum1+ re*y1;
                       }
                       else{
                           u=res*x1+sum1+ re*y1;
                       }
                        int p=k-i;
                        int q=p/2;
                        int rem1=p-q;
                         int y2=Math.min(rem1,z);
                     if(y2<q){
                       v=a[i]*y2+sum+a[i-1]*y2;
                       }
                       else{
                            v=a[i]*q+sum+a[i-1]*y2;
                       }

                         if(u>v){
                             sum=sum+a[i+1];

                             k1--;
                         }
                         else{
                              if(y2<q){
                          sum=a[i]*y2+sum+a[i-1]*y2;
                           k1=k1-y2;
                             z=z-y2;
                             k1=k1-y2;
                             i--;
                       }
                       else{
                           sum=a[i]*q+sum+a[i-1]*y2;
                            k1=k1-q;
                             z=z-y2;
                             k1=k1-y2;
                       }





                         }


                    }
                    else{
                        int p=k-i;
                        int q=p/2;
                        int rem1=p-q;
                         int y2=Math.min(rem1,z);


                     if(y2<q){
                          sum=a[i]*y2+sum+a[i-1]*y2;
                            k1=k1-y2;
                             z=z-y2;
                             k1=k1-y2;
                             i--;
                       }
                       else{
                           sum=a[i]*q+sum+a[i-1]*y2;
                             k1=k1-q;
                             z=z-y2;
                             k1=k1-y2;
                             i--;
                       }





                    }
                }

                else {
                    if(k1>0){

                        sum+=a[i+1];



                        k1--;
                    }

                }
            }

      System.out.println(sum);    
            }

}

}

  • Vote: I like it
  • -18
  • Vote: I do not like it