Skip to content

Instantly share code, notes, and snippets.

@surinoel
Created July 12, 2019 09:22
Show Gist options
  • Save surinoel/b3f080ad3b508ebb1527954051529994 to your computer and use it in GitHub Desktop.
Save surinoel/b3f080ad3b508ebb1527954051529994 to your computer and use it in GitHub Desktop.
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a.begin(), a.end());
long long ans = 0;
for (int i = 0; i < n; i++) {
ans += abs(i + 1 - a[i]);
}
cout << ans << '\n';
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment