fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int N; cin >> N;
  6. int stands[N];
  7. for (int i=0; i<N; i++) cin >> stands[i];
  8.  
  9. for (int i = 0; i < N; i++) {
  10. int dist = 0, currIdx = i;
  11. vector<int> reStands;
  12. for (int i=0; i<N; i++) reStands.push_back(stands[i]);
  13. while (reStands.size() > 0) {
  14. if (currIdx == 0 || currIdx == reStands.size()-1 || reStands.size() == 2) {
  15. dist += reStands[reStands.size()-1] - reStands[0];
  16. break;
  17. }
  18. else {
  19. if (reStands[i]-reStands[i-1] < reStands[i+1] - reStands[i]) {
  20. currIdx = i-1;
  21. dist += reStands[i]-reStands[i-1];
  22. reStands.erase(reStands.begin()+i);
  23. }
  24. else {
  25. currIdx = i;
  26. dist += reStands[i+1] - reStands[i];
  27. reStands.erase(reStands.begin()+i);
  28. }
  29. }
  30. }
  31. cout << dist << endl;
  32. }
  33. }
Success #stdin #stdout 0.01s 5388KB
stdin
7
2 5 6 10 12 20 22
stdout
20
37
32
30
24
22
20