This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
int main() {
    int N;
    cin >> N;
    vector<int> sightseeing_spots(N);
    for (int i = 0; i < N; i++) {
        cin >> sightseeing_spots[i];
    }
    int Q;
    cin >> Q;
    for (int q = 0; q < Q; q++) {
        int start_coordinate;
        cin >> start_coordinate;
        long long total_distance = 0;
        int current_position = start_coordinate;
        while (!sightseeing_spots.empty()) {
            int nearest_spot = sightseeing_spots[0];
            int min_distance = abs(current_position - nearest_spot);
            
            for (int spot : sightseeing_spots) {
                int distance = abs(current_position - spot);
                if (distance < min_distance || (distance == min_distance && spot < nearest_spot)) {
                    nearest_spot = spot;
                    min_distance = distance;
                }
            }
            total_distance += min_distance;
            current_position = nearest_spot;
            // Remove the visited spot from the list
            sightseeing_spots.erase(find(sightseeing_spots.begin(), sightseeing_spots.end(), nearest_spot));
        }
        cout << total_distance << endl;
    }
    return 0;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |