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() || 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. // cout << "greater: " << reStands[i] << "-" << reStands[i-1] << endl;
  21. currIdx = i-1;
  22. dist += reStands[i]-reStands[i-1];
  23. reStands.erase(reStands.begin()+i);
  24. // cout << reStands.size() << " " << currIdx << endl;
  25. }
  26. else {
  27. // cout << "less: " << reStands[i+1] << "-" << reStands[i] << endl;
  28. currIdx = i;
  29. dist += reStands[i+1] - reStands[i];
  30. reStands.erase(reStands.begin()+i);
  31. // cout << reStands.size() << " " << currIdx << endl;
  32. }
  33. }
  34. }
  35. cout << dist << endl;
  36. }
  37. }
Success #stdin #stdout 0.01s 5460KB
stdin
7
2 5 6 10 12 20 22
stdout
20
37
15
14
12
12
-4