#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define lowbit(x) (x & (-x))
char __c, __neg, __digit[128] = {0}, __skip[128] = {0};                                                                                                           
void init_io() {
    LL i;
    for (i = 0; i <= 9; i++) __digit[i + '0'] = 1;
    for (i = 0; i < 128; i++) if ((i < '0' || i > '9') && i != '-') __skip[i] = 1;
}
#define getLL(v) do { __neg = 0, v = 0; \
    do { __c = getchar(); } while (__skip[__c]); \
    if (__c == '-') { __neg = 1; __c = getchar(); } \
    while (__digit[__c]) { v = v * 10 + __c - '0'; __c = getchar(); } \
    if (__neg) v = -v; \
} while (0)

const LL inf  = 1e17+9;
const LL mod  = 1e9+7;
const LL maxn = 2e5+8;

map<LL, LL> mp[maxn];
LL n, q, fa[maxn], c[maxn];

LL find(LL u) {
    if (fa[u] == u) return u;
    return fa[u] = find(fa[u]);
}

void solve() {
    LL i, j, opt, a, b, x, y;
    getLL(n); getLL(q);
    for (i = 1; i <= n; i++) {
        getLL(c[i]);
        fa[i] = i; mp[i][c[i]]++;
    }
    for (i = 1; i <= q; i++) {
        getLL(opt);
        if (opt == 1) {
            getLL(a); getLL(b);
            x = find(a), y = find(b);
            if (x != y) {
                fa[x] = y;
                if (mp[x].size() > mp[y].size()) swap(mp[x], mp[y]);
                for (auto u : mp[x]) mp[y][u.first] += u.second;
                mp[x].clear();
            }
        }
        else {
            getLL(x); getLL(y);
            x = find(x);
            printf("%lld\n", mp[x][y]);
        }
    }
}

signed main() {
    init_io();
    solve();
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
#define endl '\n'
#define deb(x) cout << #x << " = " << x << endl;
typedef long long ll;
typedef long double ld;
const int MOD = 1e9 + 7;

void solve() {
  int n;
  cin >> n;
  vector<ll> a(n);
  for(ll &i : a) cin >> i;
  ll ans = 0;
  sort(a.begin(), a.end());
  for(int i = n - 1; i >= 0; i--)
    ans += (i * a[i]) - ((n - i - 1)  * a[i]);
  cout << ans << '\n';
}

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr); cout.tie(nullptr);
  int t = 1, tt = 0;
  //cin >> t;
  while(t--){
    //cout << "Case #" << ++tt << ": ";
    solve();
  }
  return 0;
} //Hajimemashite
