Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace SumProduct
  8. {
  9. class Program
  10. {
  11.  
  12. static void Print(int idx, int[,] dp, int b, int c)
  13. {
  14. for (int i = b; i <= c; i++)
  15. {
  16. Console.Write(dp[idx, i]+" ");
  17. }
  18. Console.WriteLine();
  19. }
  20.  
  21. static void Main(string[] args)
  22. {
  23. int[] input = Console.ReadLine().Split(' ').
  24. Select(int.Parse).ToArray();
  25. int n = input[0];
  26. int b = input[1];
  27. int c = input[2];
  28. int d = input[3];
  29.  
  30. int[] a = Console.ReadLine().Split(' ').
  31. Select(int.Parse).ToArray();
  32.  
  33. int[,] dp = new int[2, 10];
  34.  
  35. for (int i = b; i <= c; i++)
  36. {
  37. dp[0, i] = a[0] * i;
  38. }
  39.  
  40. int max = int.MinValue;
  41.  
  42. for (int i = 1; i < n; i++)
  43. {
  44.  
  45. for (int currentMul = b; currentMul <= c; currentMul++)
  46. {
  47. if (i % 2 != 0)
  48. {
  49. max = int.MinValue;
  50. for (int prevMul = b; prevMul <= c; prevMul++)
  51. {
  52. if (Math.Abs(currentMul - prevMul) <= d
  53. && dp[0, prevMul] > max)
  54. max = dp[0, prevMul];
  55. }
  56. dp[1, currentMul] = max + a[i] * currentMul;
  57. }
  58. else
  59. {
  60. max = int.MinValue;
  61. for (int prevMul = b; prevMul <= c; prevMul++)
  62. {
  63. if (Math.Abs(currentMul - prevMul) <= d
  64. && dp[1, prevMul] > max)
  65. max = dp[1, prevMul];
  66. }
  67. dp[0, currentMul] = max + a[i] * currentMul;
  68. }
  69. }
  70. }
  71.  
  72. int ans = int.MinValue;
  73. if (n % 2 != 0)
  74. {
  75. //0
  76. for (int i = b; i <= c; i++)
  77. {
  78. ans = Math.Max(ans, dp[0, i]);
  79. }
  80. }
  81. else {
  82. //1
  83. for (int i = b; i <= c; i++)
  84. {
  85. ans = Math.Max(ans, dp[1, i]);
  86. }
  87. }
  88.  
  89. Console.WriteLine(ans);
  90. }
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement