fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13.  
  14. int n,k,x=0,y=0,z;
  15. Scanner s=new Scanner(System.in);
  16. n=s.nextInt();
  17. k=s.nextInt();
  18. z=k*2;
  19. int[] a=new int[n];
  20. int[] b=new int[n];
  21.  
  22. for(int i=0;i<n;i++)
  23. {
  24. a[i]=s.nextInt();
  25. }
  26. for(int i=0;i<n;i++)
  27. {
  28. b[i]=s.nextInt();
  29. }
  30. int c=0,d=0;
  31. for(int i=0;i<n;i++)
  32. {
  33.  
  34. x=(a[i]+z)*b[i];
  35. z=-z;
  36. y=(a[i]+z)*b[i];
  37. if(x<=y && x<=c)
  38. {
  39. c=x;
  40. d=i;
  41. }
  42. else if(y<=x && y<=c )
  43. {
  44. c=y;
  45. d=i;
  46. }
  47. else
  48. {
  49. c=c;
  50. d=d;
  51. }
  52. }
  53. int sum=0;
  54. for(int i=0;i<n;i++)
  55. {
  56. if(i==d)
  57. {
  58. sum=sum+c;
  59. }
  60. else
  61. {
  62. sum=sum+a[i]*b[i];
  63. }
  64. }
  65. System.out.println(sum);
  66. }
  67. }
Success #stdin #stdout 0.06s 4386816KB
stdin
	
3 5
1 2 -3
-2 3 -5
stdout
-31