Advertisement
Guest User

Untitled

a guest
Dec 12th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. //using namespace std;
  3.  
  4. typedef __float128 Int;
  5.  
  6. int main() {
  7.     std::ios_base::sync_with_stdio(false);
  8. //    freopen("input.txt", "rt", stdin);
  9.    
  10.     int n;
  11.     std::cin >> n;
  12.    
  13.     std::vector<int> a(n);
  14.     for (auto & it : a) {
  15.         std::cin >> it;
  16.     }
  17.    
  18.     std::map<int, int> count;
  19.     count[a.back()] = 1;
  20.    
  21.     Int answer = 0;
  22.     Int sum = a[n-1];
  23.    
  24.     for (int i = n-2; i >= 0; --i) {
  25.         Int s = sum;
  26.         int c = n - i - 1;
  27.         {
  28.             auto it = count.find(a[i]);
  29.             if (it != count.end()) {
  30.                 s -= (Int) it->second * it->first;
  31.                 c -= it->second;
  32.             }    
  33.         }
  34.        
  35.         {
  36.             auto it = count.find(a[i]-1);
  37.             if (it != count.end()) {
  38.                 s -= (Int) it->second * it->first;
  39.                 c -= it->second;
  40.             }    
  41.         }
  42.        
  43.         {
  44.             auto it = count.find(a[i]+1);
  45.             if (it != count.end()) {
  46.                 s -= (Int) it->second * it->first;
  47.                 c -= it->second;
  48.             }    
  49.         }
  50.        
  51.         answer += s - (Int)c * a[i];
  52.        
  53.         count[a[i]]++;
  54.         sum += a[i];
  55.     }
  56.    
  57.     std::string r;
  58.  
  59.     if (answer < 0) {
  60.         r += '-';
  61.         answer = -answer;
  62.     }
  63.    
  64.     long long high = (long long) (answer / 1000000000000000LL);
  65.     long long low = (long long) (answer - (Int)high * 1000000000000000LL);
  66.     if (high > 0) {
  67.         std::cout << r << high << std::setw(15) << std::setfill('0') << low;
  68.     } else {
  69.         std::cout << r << low;
  70.     }
  71.    
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement