mfgnik

Untitled

Nov 25th, 2020
555
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5.  
  6. int main() {
  7.     int amount;
  8.     std::cin >> amount;
  9.     std::vector<int> coordinates(amount);
  10.     for (auto& coordinate : coordinates) {
  11.         std::cin >> coordinate;
  12.     }
  13.     std::sort(coordinates.begin(), coordinates.end());
  14.     std::vector<int> dynamics{0, 0, coordinates[1] - coordinates[0], coordinates[2] - coordinates[0]};
  15.     for (int index = 4; index <= amount; ++index) {
  16.         dynamics.push_back(std::min(dynamics[index - 1], dynamics[index - 2]) + coordinates[index - 1] - coordinates[index - 2]);
  17.     }
  18.     std::cout << dynamics[amount];
  19. }
RAW Paste Data