#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <string>
#include <queue>
#include <unordered_set>
#include <deque>
#include <numeric>
#include <cmath>
#include <unordered_map>
#include <set>
using namespace std;
#pragma GCC optimize("O3")
#pragma GCC optimization("Ofast,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
typedef long long ll;
struct Event {
int x, type; //type = 0 if pref, type = 1 if laser
int level, ind;
Event(int x, int type, int level = -1, int ind = -1) :x(x), type(type), level(level), ind(ind) {}
bool operator <(const Event& other) const {
return make_pair(x, type) < make_pair(other.x, other.type);
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int l, n;
cin >> l >> n;
vector<vector<int>> width(n), pref(n);
vector<int> rem(n);
multiset<int, greater<int>> rem_set;
vector<Event> ev;
for (int i = 0; i < n; ++i) {
int cnt;
cin >> cnt;
width[i].resize(cnt);
pref[i].resize(cnt);
for (int j = 0; j < cnt; ++j) {
cin >> width[i][j];
}
partial_sum(width[i].begin(), width[i].end(), pref[i].begin());
for (int j = 0; j < cnt; ++j) {
ev.push_back(Event(pref[i][j], 0, i, j));
}
rem[i] = pref[i].back();
rem_set.insert(rem[i]);
}
sort(ev.begin(), ev.end());
int ans = 0;
int prv = 0;
for (auto& e : ev) {
if (e.type == 0) {
int beg = min(max(prv, l - *rem_set.begin()), e.x);
ans += (e.x - beg);
rem_set.erase(rem_set.find(rem[e.level]));
rem[e.level] -= width[e.level][e.ind];
rem_set.insert(rem[e.level]);
prv = e.x;
}
}
cout << ans << '\n';
}
/*
11 3
2 2 3
1 7
2 4 1
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |