usingnamespace std; typedeflonglong ll; constint N = 1e5 + 5; int n; ll d[N];
voidsolve(){ scanf ("%d", &n); int neg = 0; for (int i = 1; i <= n; i++) { scanf ("%lld", &d[i]); if (d[i] < 0) neg ++; } sort (d + 1, d + n + 1); ll ans = 0, sum = 0; if (d[n] > 0) ans += d[n]; neg ++; //第一个>=0的数 for (int i = 1; i <= n; i++) { if (d[i] < 0) ans += d[i]; else { ans -= (d[i] * (i-neg) - sum); sum += d[i]; } }
printf ("%lld\n", ans); }
signedmain(){ int t; scanf ("%d", &t); while (t --) solve (); }
usingnamespace std; typedeflonglong ll; constint N = 1e5 + 5; int n; ll d[N], b[N];
voidsolve(){ scanf ("%d", &n); for (int i = 1; i <= n; i++) scanf ("%lld", &d[i]); sort (d + 1, d + n + 1); ll ans = 0, sum = 0; for (int i = 1; i <= n; i++) b[i] = d[i] - d[i-1], sum += b[i]; for (int i = 1; i <= n; i++) ans += b[i] * (i-1) * (n-i+1); printf ("%lld\n", sum - ans); }
signedmain(){ int t; scanf ("%d", &t); while (t --) solve (); }
usingnamespace std; constint N = 2e5 + 5; int a[N];
intmain(){ int n, m; scanf ("%d%d", &n, &m); if (m == 1 || m == 2*n - 1) { printf ("No\n"); } else { printf ("Yes\n"); set <int> s; for (int i = 1; i < 2*n; i++) { if (i != m && i != m-1 && i != m+1) s.insert (i); } a[n-1] = m-1, a[n] = m, a[n+1] = m+1; for (int i = 1; i < 2*n; i++) { if (i != n && i != n-1 && i != n+1) { a[i] = *s.begin(); s.erase(a[i]); } } for (int i = 1; i < 2*n; i++) printf ("%d\n", a[i]); } }
int n, a[N], b[N]; int f[N][N], c[N][N]; //f[i][j]:还剩n-(j-i)轮时,用了i-j匹马; c[i][j]:a[i]与b[j]对战后田忌会获得多少
intmain(){ while (cin >> n, n) { for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) cin >> b[i]; sort (a+1, a+n+1), sort (b+1, b+n+1);
memset (f, 0, sizeof f); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (a[i] > b[j]) c[i][j] = 200; elseif (a[i] < b[j]) c[i][j] = -200; else c[i][j] = 0; } }
//区间dp for (int len = 1; len <= n; len++) { for (int l = 1; l + len - 1 <= n; l++) { int r = l + len - 1; if (len == 1) f[l][r] = c[l][len]; else f[l][r] = max (f[l+1][r] + c[l][len], f[l][r-1] + c[r][len]); } }
#include<iostream> #include<algorithm> #include<stack> #define int long long
usingnamespace std; constint N = 100005; int a[N], sum[N], n; int l[N], r[N];
signedmain(){ scanf ("%lld", &n); for (int i = 1; i <= n; i++) { scanf ("%lld", &a[i]); sum[i] = sum[i-1] + a[i]; l[i] = 0, r[i] = n + 1; }
stack <int> s; for (int i = n; i >= 1; i--) { while (!s.empty() && a[s.top()] >= a[i]) s.pop(); if (!s.empty()) r[i] = s.top(); s.push (i); } while (!s.empty()) s.pop(); for (int i = 1; i <= n; i++) { while (!s.empty() && a[s.top()] >= a[i]) s.pop(); if (!s.empty()) l[i] = s.top(); s.push (i); }
int ans = -1, L, R; for (int i = 1; i <= n; i++) { int len = (sum[r[i]-1] - sum[l[i]]) * a[i]; //开区间 if (len > ans) { L = l[i] + 1, R = r[i] - 1; ans = len; } }
printf ("%lld\n%lld %lld", ans, L, R); //cout << ans << endl << L << ' ' << R; }
usingnamespace std; constint N = 105; int n, t; int f[N][N]; //f[i][j]:选到第i组,容量为j的最大值 int v[N], w[N];
voidsolve(){ memset (f, 0, sizeof f); for (int i = 1; i <= n; i++) { int m, s; cin >> m >> s; for (int j = 1; j <= m; j++) cin >> v[j] >> w[j]; if (s == 0) { for (int j = 0; j <= t; j++) f[i][j] = -1e9; for (int j = 1; j <= m; j++) { for (int k = t; k >= v[j]; k--) { f[i][k] = max (f[i][k], max (f[i-1][k-v[j]] + w[j], f[i][k-v[j]] + w[j])); } } }
elseif (s == 1) { for (int j = 0; j <= t; j++) f[i][j] = f[i-1][j]; for (int j = 1; j <= m; j++) { for (int k = t; k >= v[j]; k--) { f[i][k] = max (f[i][k], f[i-1][k-v[j]] + w[j]); } } }
else { for (int j = 0; j <= t; j++) f[i][j] = f[i-1][j]; for (int j = 1; j <= m; j++) { for (int k = t; k >= v[j]; k--) { f[i][k] = max (f[i][k], f[i][k-v[j]] + w[j]); } } } } cout << max (-1, f[n][t]) << endl; }
usingnamespace std; typedeflonglong LL; constint N = 1e5 + 10, mod = 998244353;
intbinpow(int b, int k){ int res = 1; while (k) { if (k & 1) res = (LL)res * b % mod; b = (LL)b * b % mod; k >>= 1; } return res; }
intC(int a, int b){ if (a < b) return0; int res = 1, inv = 1; for (int i = a; i > a - b; i--) res = (LL)res * i % mod; for (int i = b; i; i--) inv = (LL)inv * i % mod; return (LL)res * binpow(inv, mod - 2) % mod; }
intlucas(int a, int b){ if (!a || !b) return1; return (LL)C(a % mod, b % mod) * lucas(a / mod, b / mod) % mod; } signedmain(){ int n, k, a1; LL ans = 1; cin >> n >> k >> a1; a1 -= k; for (int i = 1; i < n; i++) { int x; cin >> x; a1 -= x; if (a1 < 0) { cout << 0; return0; } ans = (ans * lucas (x+k-1, k-1)) % mod; } ans = (ans * lucas (a1+k-1, k-1)) % mod; cout << ans; }
for (int i = h[u]; ~i; i = ne[i]) { int j = e[i]; if (!dfn[j]) { tarjan(j); low[u] = min (low[u], low[j]); } elseif (in_stk[j]) low[u] = min (low[u], dfn[j]); }
if (dfn[u] == low[u]) { int cnt = 0, y; scc_cnt ++; do { y = stk.top(); stk.pop(); cnt ++; in_stk[y] = false; } while (y != u);
if (cnt >= 2) { suc = true; return ; } } }
voidsolve(){ scanf ("%d%d%d", &n, &m1, &m2); init (); for (int i = 0; i < m1; i++) { int u, v; scanf ("%d%d", &u, &v); int x = find (u), y = find (v); if (x == y) suc = true; fa[x] = y; }
for (int i = 0; i < m2; i++) { int u, v; scanf ("%d%d", &u, &v); int x = find (u), y = find (v); if (x == y) suc = true; if (suc) continue; add (x, y); }
if (suc) { printf ("YES\n"); return ; }
for (int i = 1; i <= n; i++) { if (suc) break; if (fa[i] == i && !dfn[i]) tarjan (i); }
if (suc) printf ("YES\n"); elseprintf ("NO\n"); }
intmain(){ int t; scanf ("%d", &t); while (t --) solve (); }
usingnamespace std; constint N = 5e5 + 5; int n, m, k, x, y; int a[N];
structtree{ int l, r, sum; int lmax, rmax, tmax;//最大前后缀和, 最大子段和 }st[4 * N];
voidpushup(tree &u, tree &l, tree &r){ u.sum = l.sum + r.sum; u.lmax = max (l.lmax, l.sum + r.lmax);//左半段中的最大 or 左半段和+右半段中的最大 u.rmax = max (r.rmax, r.sum + l.rmax);//右半段中的最大 or 右半段和+左半段中的最大 u.tmax = max (max (l.tmax, r.tmax), l.rmax + r.lmax);//左半段中的最大 or 右半段中的最大 or 左半段后缀和+右半段前缀和 }
voidbuild(int u, int l, int r){ if (l == r){ st[u] = {l, r, a[r], a[r], a[r], a[r]}; return; } st[u] ={l, r}; int mid = l + r >> 1; build (u << 1, l, mid), build (u << 1 | 1, mid + 1, r); pushup (u); }
voidmodify(int u, int x, int v){ if (st[u].l == x && st[u].r == x) st[u] = {x, x, v, v, v, v}; else{ int mid = st[u].l + st[u].r >> 1; if (x <= mid) modify (u << 1, x, v); else modify (u << 1 | 1, x, v); pushup (u); } }
tree query(int u, int l, int r){ if (st[u].l >= l && st[u].r <= r) return st[u]; int mid = st[u].l + st[u].r >> 1; if (r <= mid) returnquery (u << 1, l, r); elseif (l > mid) returnquery (u << 1 | 1, l, r); else{ auto le = query (u << 1, l, r); auto ri = query (u << 1 | 1, l, r); tree ans; pushup (ans, le, ri); return ans; }
}
intmain(){ cin >> n; for (int i = 1; i <= n; i ++) cin >> a[i]; build (1, 1, n); cin >> m; while (m --){ cin >> k >> x >> y; if (k == 0) modify (1, x, y); else{ if (x > y) swap (x, y); cout << query (1, x, y).tmax << endl; } }
usingnamespace std; constint N = 1e5 + 5; int a[N], b[N], n; //后缀min
intmain(){ int n; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; b[n] = a[n]; for (int i = n-1; i >= 1; i--) b[i] = min (b[i+1], a[i]);
for (int i = 1; i <= n; i++) { int l = i, r = n; while (l < r) { int mid = l + r + 1 >> 1; if (b[mid] < a[i]) l = mid; else r = mid - 1; } if (a[r] >= a[i]) cout << "-1 "; else cout << r - i - 1 << ' '; } }
usingnamespace std; constint N = 1e5 + 5; int n, x; double f[N], p; //f[i]:输入第i个字符的期望值
voidsolve(){ double ans = 1e9; cin >> n >> p >> x; //f[i]=p*(1+f[i-1]+f[i])+(1-p)*(1+f[i-1]); for (int i = 1; i <= n; i++) f[i] = (1 + f[i-1]) / (1 - p); for (int i = 1; i <= n; i++) { //i保存次数 int k = n / i, cnt = n % i, res = i - cnt; ans = min (ans, cnt * f[k+1] + res * f[k] + i * x); //cnt个字符分配到r块中,要敲k+1个字符 //剩余的res块则只需要敲k个字符 } cout << fixed << setprecision (6) << ans << endl; }
intmain(){ int t; cin >> t; for (int i = 1; i <= t; i++) { cout << "Case #" << i << ": "; solve (); } }