#define x first #define y second #define int long long
usingnamespace std;
constint N = 200010;
int T; int n; int a[N];
signedmain() { cin >> T; while (T--) { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; int res = 0; for (int i = 0; i < n; i++) res += a[i]; int cnt = 0; map<int, int> p; for (int i = 0; i < n; i++) { p[a[i]]++; if (p[a[i]] >= 2) cnt = max(cnt, a[i]); a[i] = cnt; } for (int i = 0; i < n; i++) res += a[i]; cnt = 0; p.clear(); for (int i = 0; i < n; i++) { p[a[i]]++; if (p[a[i]] >= 2) cnt = max(cnt, a[i]); a[i] = cnt; }
int sum = 0; for (int i = 0; i < n; i++) sum += a[i]; for (int i = n - 1; i >= 0; i--) { res += sum; sum -= a[i]; } cout << res << endl; }
#define x first #define y second #define int long long
usingnamespace std;
constint N = 200010;
int T; int n; int a[N];
signedmain() { cin >> T; while (T--) { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i];
int res = 0, st = -1; for (int i = 1; i <= n; i++) { if (!a[i]) st = -1; elseif (~st && st <= a[i]) { if (st == 1 || st == a[i] - 1) { if (a[i] <= 2) st = -1; else { res++; if (a[i] <= 4) st = st == 1? 3 : 1; else st = -1; } } elseif (st == a[i]) { if (a[i] <= 1) st = -1; else { res++; if (a[i] <= 3) st = 1; else st = -1; } } else res++, st = -1; } else { res++; if (a[i] <= 2) st = 1; } }