USACO Bronze, Ski Design

Hey all,

I am doing this problem:

From my understanding, what I need to do is keep changing the highest and lowest hills (per a given iteration), until the difference between the highest and lowest hill is equal to 17, at which point I stop iterating.

However, I am failing the majority of the tests, which clearly means I have misunderstood this problem. Could someone please hint as to why my approach is not right?

My attempt:

#include <fstream>
#include <string>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <iostream>

using namespace std;

int main(void)
    ifstream fin("");
    ofstream fout("skidesign.out");

    // number of lines of input
    int n;
    fin >> n;

    // if we have a single number, there is no cost
    if (n == 1)
        fout << 0 << "\n";
        return 0;

    vector<int> heights;

    int x;
    for (int i = 0; i < n; i++)
        fin >> x;

    int max_diff = 17;
    int cost = 0;
    sort(heights.begin(), heights.end());

    // loop invariant is that the height vector is in ascending order.
    while (heights[n - 1] - heights[0] != max_diff)
        heights[n - 1]--;
        sort(heights.begin(), heights.end());

    fout << 2 * (cost * cost) << endl;
    // cout << cost << endl;