Advertisement
Derga

Untitled

Sep 12th, 2023
793
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8.     int costs_count;
  9.     cin >> costs_count;
  10.     vector< vector< int > > costs_free_lunches_count(costs_count, vector< int >(costs_count + 1, 1e10));
  11.     vector< int > costs(costs_count, 1e10);
  12.     for (int i = 0; i < costs_count; ++i)
  13.     {
  14.         cin >> costs[i];
  15.     }
  16.     const int b = 100;
  17.     bool is_even = true;
  18.     int cost = costs.front();
  19.     bool is_more_then_100 = (cost >= b);
  20.     costs_free_lunches_count.front()[is_more_then_100] = cost;
  21.     int last_cost = cost;
  22.     for (int i = 1; i < costs_count; ++i)
  23.     {
  24.         int cost = costs[i];
  25.         if (cost >= b) is_even = !is_even;
  26.         for (int j = 0; j < costs_count; ++j)
  27.         {
  28.            
  29.             int n_cost1 = costs_free_lunches_count[i - 1][j] + costs[i];   
  30.             int n_cost2 = costs_free_lunches_count[i - 1][j + 1];
  31.             int n_cost = min(n_cost1, n_cost2);
  32.             if (cost < b)
  33.             {
  34.                 costs_free_lunches_count[i][j] = min(n_cost1, n_cost2);
  35.                 continue;
  36.             } else {
  37.                 if (is_even && j + 1 < costs_count)
  38.                     j++;
  39.                 int n_cost1 = 1e10;
  40.                 if (j > 0)
  41.                 {
  42.                     n_cost1 = costs_free_lunches_count[i - 1][j - 1] + costs[i];
  43.                 }
  44.                 int n_cost2 = costs_free_lunches_count[i - 1][j + 1];
  45.                 int n_cost = min(n_cost1, n_cost2);
  46.                
  47.                 costs_free_lunches_count[i][j] = n_cost;
  48.                 if (!is_even)
  49.                 {
  50.                     j++;
  51.                 }
  52.             }
  53.         }
  54.     }
  55.  
  56.     cout << min(costs_free_lunches_count.back()[0], costs_free_lunches_count.back()[1]);
  57.  
  58.     return 0;
  59. }
  60. /*
  61. test1
  62. 7
  63. 5 6 5 7 5 3 6
  64. 18
  65. */
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement