2 solutions
-
0
#include <bits/stdc++.h> using namespace std; #define FIO(s) \ freopen(s ".in", "r", stdin); \ freopen(s ".out", "w", stdout); using ll = long long; template <typename T> void chkmn(T &x, T y) { x = min(x, y); } int n, L; ll ans; vector<tuple<int, ll>> vec; int main() { FIO("exit"); ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n >> L; for (int i = 1; i <= n; i++) { int x; ll w; cin >> x >> w; ll c = min(x, L - x); vec.emplace_back(x - c, -w); vec.emplace_back(x, 2 * w); vec.emplace_back(x + c, -w); ans += c * w; } vec.emplace_back(L, 0); sort(vec.begin(), vec.end()); ll sum = ans; ll K = 0; ll lst = 0; for (int i = 0; i < vec.size();) { auto [x, w] = vec[i]; sum += K * (x - lst); chkmn(ans, sum); while (i < vec.size() && get<0>(vec[i]) == x) K += get<1>(vec[i++]); lst = x; } cout << ans << '\n'; return 0; }
- 1
Information
- ID
- 3148
- Time
- 2000ms
- Memory
- 1024MiB
- Difficulty
- 9
- Tags
- (None)
- # Submissions
- 10
- Accepted
- 4
- Uploaded By