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;
  const int N = 3e5 + 5, mod = 1e9 + 7;
  struct BIT {
    vector<long long> tree;
    BIT(int n) : tree(n + 2) {}
    void upd(int p, long long v) {
      for (int i = ++p; i < tree.size(); i += i & -i) tree[i] += v;
    }
    long long get(int p) {
      long long sum = 0;
      for (int i = ++p; i; i -= i & -i) sum += tree[i];
      return sum;
    }
    long long query(int l, int r) {
      if (r < l) return 0;
      return get(r) - get(l - 1);
    }
    int get_kth(long long k) {
      int pos = 0;
      long long s = 0;
      for (int i = 1 + log2(tree.size()); i >= 0; i--)
        if (pos + (1 << i) < tree.size() && s + tree[pos + (1 << i)] < k)
          s += tree[pos + (1 << i)], pos += (1 << i);
      return pos;
    }
    int lower_bound(int v) { return get_kth(get(v - 1) + 1); }
    int upper_bound(int v) { return lower_bound(v + 1); }
  };
  void Run() {
    int n;
    cin >> n;
    BIT freq(2 * N), vals(2 * N);
    vector<long long> a(n + 1);
    vector<long long> s(n + 1), t(n + 1);
    long long sum = 0;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) {
      s[i] = s[i - 1] + (i - 1) * a[i] + vals.get(a[i]);
      for (int j = a[i]; j < N; j += a[i]) {
        vals.upd(j, -j);
        vals.upd(j + a[i], j);
      }
      t[i] = t[i - 1] + sum;
      for (int j = a[i]; j < N; j += a[i])
        t[i] -= freq.query(j, j + a[i] - 1) * j;
      sum += a[i];
      freq.upd(a[i], 1);
      cout << s[i] + t[i] << " ";
    }
    cout << '\n';
  }
  int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t;
    t = 1;
    while (t--) Run();
  }
  