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