Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- //using namespace std;
- typedef __float128 Int;
- int main() {
- std::ios_base::sync_with_stdio(false);
- // freopen("input.txt", "rt", stdin);
- int n;
- std::cin >> n;
- std::vector<int> a(n);
- for (auto & it : a) {
- std::cin >> it;
- }
- std::map<int, int> count;
- count[a.back()] = 1;
- Int answer = 0;
- Int sum = a[n-1];
- for (int i = n-2; i >= 0; --i) {
- Int s = sum;
- int c = n - i - 1;
- {
- auto it = count.find(a[i]);
- if (it != count.end()) {
- s -= (Int) it->second * it->first;
- c -= it->second;
- }
- }
- {
- auto it = count.find(a[i]-1);
- if (it != count.end()) {
- s -= (Int) it->second * it->first;
- c -= it->second;
- }
- }
- {
- auto it = count.find(a[i]+1);
- if (it != count.end()) {
- s -= (Int) it->second * it->first;
- c -= it->second;
- }
- }
- answer += s - (Int)c * a[i];
- count[a[i]]++;
- sum += a[i];
- }
- std::string r;
- if (answer < 0) {
- r += '-';
- answer = -answer;
- }
- long long high = (long long) (answer / 1000000000000000LL);
- long long low = (long long) (answer - (Int)high * 1000000000000000LL);
- if (high > 0) {
- std::cout << r << high << std::setw(15) << std::setfill('0') << low;
- } else {
- std::cout << r << low;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement