Advertisement
mateuspl

URI: 1310 - Lucro (WA 20%)

Jan 26th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     int numDays;
  8.  
  9.     while (cin >> numDays)
  10.     {
  11.         int cost;
  12.         cin >> cost;
  13.  
  14.         int days[numDays];
  15.  
  16.         for (int i = 0; i < numDays; i++)
  17.         {
  18.             cin >> days[i];
  19.             days[i] -= cost;
  20.         }
  21.  
  22.         int begin = 0, end = numDays - 1;
  23.         while (days[begin] < 0 && begin < end) begin++;
  24.         while (days[end] < 0 && end >= begin) end--;
  25.  
  26.         if (begin > end)
  27.             cout << 0 << endl;
  28.  
  29.         else if (begin == end)
  30.             cout << days[begin] << endl;
  31.        
  32.         else
  33.         {
  34.             int profit[numDays][numDays];
  35.             int bestProfit = 0;
  36.  
  37.             for (int d = end; d >= begin; d--)
  38.             {
  39.                 profit[d][0] = days[d];
  40.            
  41.                 for (int c = 1; c < end - d + 1; c++)
  42.                 {
  43.                     profit[d][c] = days[d] + profit[d + 1][c - 1];
  44.  
  45.                     if (profit[d][c] > bestProfit)
  46.                         bestProfit = profit[d][c];
  47.                 }
  48.             }
  49.  
  50.             cout << bestProfit << endl;
  51.         }
  52.     }
  53.  
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement