description: You have an array a consisting of n distinct positive integers, numbered from 1 to n. Define p_k as $$$p_k = ∑_{1 ≤ i, j ≤ k} a_i mod a_j, where x \bmod y denotes the remainder when x is divided by y. You have to find and print p_1, p_2, \ldots, p_n$$$. 
  
  Input
  
  The first line contains n — the length of the array (2 ≤ n ≤ 2 ⋅ 10^5).
  
  The second line contains n space-separated distinct integers a_1, …, a_n (1 ≤ a_i ≤ 3 ⋅ 10^5, a_i ≠ a_j if i ≠ j). 
  
  Output
  
  Print n integers p_1, p_2, …, p_n. 
  
  Examples
  
  Input
  
  
  4
  6 2 7 3
  
  
  Output
  
  
  0 2 12 22
  
  
  Input
  
  
  3
  3 2 1
  
  
  Output
  
  
  0 3 5
solution: #include <bits/stdc++.h>
  using namespace std;
  using ll = long long;
  using pii = pair<int, int>;
  using vi = vector<int>;
  const int N = 3e5 + 3;
  const int mod = 1e9 + 7;
  int n;
  ll a[N];
  template <class T>
  struct FT {
    vector<T> tree;
    int sz;
    FT(int n) { sz = n, tree.resize(n + 1); }
    T get(int i) {
      if (i <= 0 || i > sz) return T(0);
      T sum = T(0);
      for (; i; i -= (i & -i)) sum += tree[i];
      return sum;
    }
    void upd(int i, T val) {
      for (; i <= sz; i += (i & -i)) tree[i] += val;
    }
    void init(T *arr, int n) {
      sz = n;
      for (int i = 1; i <= n; i++) upd(i, arr[i - 1]);
    }
  };
  ll ans[N];
  int main() {
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
      scanf("%d", &a[i]);
    }
    FT<ll> tree1(N);
    FT<ll> tree2(N);
    ll sum = 0;
    for (int i = 0; i < n; i++) {
      ans[i] = 1ll * i * a[i] + sum;
      ans[i] -= tree1.get(a[i]);
      sum += a[i];
      int mul = a[i];
      while (mul < N) {
        tree1.upd(mul, 1ll * a[i]);
        mul += a[i];
      }
      mul = (300000 / a[i]) * a[i];
      ll cnt = 0;
      while (mul != 0) {
        ll cur = i - tree2.get(mul - 1);
        ll how_many = cur - cnt;
        ans[i] -= 1ll * mul * how_many;
        cnt = cur;
        mul -= a[i];
      }
      tree2.upd(a[i], 1ll);
    }
    for (int i = 0; i < n; i++) {
      printf("%lld ", ans[i]);
      ans[i + 1] += ans[i];
    }
    return 0;
  }
  