#define x first #define y second #define int long long
usingnamespace std;
typedef pair<int, int> PII;
constint N = 200010;
int T; int n; int a[N]; int pre[N]; string s;
signedmain() { cin >> T; while (T--) { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; cin >> s; for (int i = 1; i <= n; i++) pre[i] = pre[i - 1] + a[i]; int l = 1, r = n, res = 0; while (l < r) { while (s[l - 1] != 'L' && l <= n) l++; while (s[r - 1] != 'R' && r > 0) r--; if (l < r) res += pre[r] - pre[l - 1]; l++, r--; } cout << res << endl; }
#define x first #define y second #define int long long
usingnamespace std;
typedef pair<int, int> PII;
constint N = 200010;
int T; int n, m, k, l; int a[N]; int c1[N], c2[N];
signedmain() { cin >> T; while (T--) { cin >> n >> m >> k >> l; for (int i = 0; i < l; i++) cin >> a[i]; sort(a, a + l);
int x = min(k, n - k + 1), y = min(k, m - k + 1); c1[x] = n - (x - 1) * 2, c2[y] = m - (y - 1) * 2; for (int i = x - 1; i > 0; i--) c1[i] = 2; for (int i = y - 1; i > 0; i--) c2[i] = 2; map<int, int> p; for (int i = x; i > 0; i--) for (int j = y; j > 0; j--) p[i * j] += c1[i] * c2[j]; int id = l - 1, res = 0; map<int, int>::reverse_iterator it; for (it = p.rbegin(); it!= p.rend(); it++) { if (id < 0) break; int x = it -> x, y = it -> y; while (~id && y) { res += a[id] * x; id--, y--; } } cout << res << endl; }
intcalc(int id, int k) { int x = a[id], y = b[id]; if (x > y) swap(x, y); if (k <= y - x - 1) return k * x; if (k >= x + y - 1) return x * y; int res = (y - x - 1) * x; k -= y - x - 1; res += (x * 2 - k / 2 + 1) * (k / 2); if (k % 2) res += x - k / 2; return res; }
intmain() { cin >> T; while (T--) { cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i] >> b[i]; for (int i = 1; i <= n; i++) for (int j = 0; j <= m; j++) { f[i][j] = 1e9; for (int k = 0; k <= min(a[i] + b[i], j); k++) { if (j - k && !f[i - 1][j - k]) continue; f[i][j] = min(f[i][j], f[i - 1][j - k] + calc(i, k)); } } if (f[n][m] == 1e9) cout << -1 << endl; else cout << f[n][m] << endl; }
structedge { int x, l1, l2; }; int T; int n, m; vector<edge> h[N]; int t0, t1, t2;
intdijkstra() { int d[N], st[N]; for (int i = 1; i <= n; i++) { d[i] = -1e18; st[i] = 0; } d[n] = t0; priority_queue<PII, vector<PII>, less<PII>> heap; heap.push({t0, n});
while (heap.size()) { auto t = heap.top(); heap.pop(); int time = t.first, ver = t.second; if (st[ver]) continue; st[ver] = true; for (int i = 0; i < h[ver].size(); i++) { int x = h[ver][i].x, l1 = h[ver][i].l1, l2 = h[ver][i].l2; int tt = (time - l1 >= t2 || time <= t1)? l1 : l2; if (tt == l2) tt = min(tt, time - t1 + l1); if (d[x] < d[ver] - tt) { d[x] = d[ver] - tt; heap.push({d[x], x}); } } }
if (d[1] < 0) return-1; elsereturn d[1]; }
signedmain() { cin >> T; while (T--) { cin >> n >> m; cin >> t0 >> t1 >> t2; while (m--) { int a, b, c, d; cin >> a >> b >> c >> d; h[a].push_back({b, c, d}), h[b].push_back({a, c, d}); } cout << dijkstra() << endl; for (int i = 1; i <= n; i++) h[i].clear(); }