Language: C++

#include <iostream>
#include <algorithm>
using namespace std;

int x[1000000];
int distance[1000000];

int main() {
  int all_length;
  cin >> n >> k;
  for (int i=0; i<n; ++i) {
    cin >> x[i];
  }
  if (n==1) {
    cout << 0;
  }
  else {
    all_length = x[n-1] - x[0];
    for (int i=0; i<n-1; ++i) {
      distance[i] = x[i+1] - x[i];
    }
    sort(distance, distance+n);
    for (int i=0; i<k-1; ++i) {
      all_length = all_length - distance[n-1-i];
    }
  }
  cout << all_length;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:10:10: error: 'n' was not declared in this scope
   10 |   cin >> n >> k;
      |          ^
a.cc:10:15: error: 'k' was not declared in this scope
   10 |   cin >> n >> k;
      |               ^
a.cc:20:7: error: reference to 'distance' is ambiguous
   20 |       distance[i] = x[i+1] - x[i];
      |       ^~~~~~~~
In file included from /usr/include/c++/14/string:47,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:148:5: note: candidates are: 'template<class _InputIterator> constexpr typename std::iterator_traits< <template-parameter-1-1> >::difference_type std::distance(_InputIterator, _InputIterator)'
  148 |     distance(_InputIterator __first, _InputIterator __last)
      |     ^~~~~~~~
a.cc:6:5: note:                 'int distance [1000000]'
    6 | int distance[1000000];
      |     ^~~~~~~~
a.cc:22:10: error: reference to 'distance' is ambiguous
   22 |     sort(distance, distance+n);
      |          ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:148:5: note: candidates are: 'template<class _InputIterator> constexpr typename std::iterator_traits< <template-parameter-1-1> >::difference_type std::distance(_InputIterator, _InputIterator)'
  148 |     distance(_InputIterator __first, _InputIterator __last)
      |     ^~~~~~~~
a.cc:6:5: note:                 'int distance [1000000]'
    6 | int distance[1000000];
      |     ^~~~~~~~
a.cc:22:20: error: reference to 'distance' is ambiguous
   22 |     sort(distance, distance+n);
      |                    ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:148:5: note: candidates are: 'template<class _InputIterator> constexpr typename std::iterator_traits< <template-parameter-1-1> >::difference_type std::distance(_InputIterator, _InputIterator)'
  148 |     distance(_InputIterator __first, _InputIterator __last)
      |     ^~~~~~~~
a.cc:6:5: note:                 'int distance [1000000]'
    6 | int distance[1000000];
      |     ^~~~~~~~
a.cc:24:33: error: reference to 'distance' is ambiguous
   24 |       all_length = all_length - distance[n-1-i];
      |                                 ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:148:5: note: candidates are: 'template<class _InputIterator> constexpr typename std::iterator_traits< <template-parameter-1-1> >::difference_type std::distance(_InputIterator, _InputIterator)'
  148 |     distance(_InputIterator __first, _InputIterator __last)
      |     ^~~~~~~~
a.cc:6:5: note:                 'int distance [1000000]'
    6 | int distance[1000000];
      |     ^~~~~~~~