#include // {{{ // https://codeforces.com/blog/entry/96344 #pragma GCC optimize("O2,unroll-loops") #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") using namespace std; using i32 = int32_t; using u32 = uint32_t; using i64 = int64_t; using u64 = uint64_t; using f64 = double; using f128 = long double; #if __cplusplus >= 201703L template constexpr T MIN = std::numeric_limits::min(); template constexpr T MAX = std::numeric_limits::max(); template [[nodiscard]] T sc(U &&u) { return static_cast(std::forward(u)); } template [[nodiscard]] T sz(C const &c) { return static_cast(c.size()); } #endif #if defined(LOCAL) && __cplusplus >= 201703L #define db(...) std::print(__VA_ARGS__) #define dbln(...) std::println(__VA_ARGS__) #else #define db(...) #define dbln(...) #endif static void NO() { std::cout << "NO\n"; } static void YES() { std::cout << "YES\n"; } #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define ff first #define ss second // }}} struct ac { u32 l, r; i32 p, m; // NOTE: copied from AI - order doesn't matter, just need to consider all // permutations bool operator<(ac const &o) const { return tie(l, r, p, m) < tie(o.l, o.r, o.p, m); } }; void solve() { u32 N, M; cin >> N >> M; vector to_cool(101, 0); u32 l, r, c, m; for (u32 i = 0; i < N; ++i) { cin >> l >> r >> c; for (u32 j = l; j <= r; ++j) { to_cool[j] = c; } } vector acs; for (u32 i = 0; i < M; ++i) { cin >> l >> r >> c >> m; acs.emplace_back(ac{l, r, c, m}); } sort(all(acs)); u32 ans = MAX; for (u32 mask = 0; mask < 1LL << M; ++mask) { u32 tc = MAX; auto temps = to_cool; u32 cost = 0; // subsets != permutation/order // want to track use/lack of use for (u32 bit = 0; bit < M; ++bit) { if (!(mask & (1 << bit))) continue; auto &a = acs[bit]; cost += a.m; for (u32 j = a.l; j <= min((u32)100, a.r); ++j) { temps[j] -= a.p; } if (all_of(all(temps), [](i32 e) { return e <= 0; })) { ans = min(ans, cost); break; } } ans = min(ans, tc); } cout << ans << endl; } int main() { // {{{ cin.tie(nullptr)->sync_with_stdio(false); cin.exceptions(cin.failbit); #define PROBLEM_NAME "air-cownditioning-ii" #ifdef LOCAL freopen("io/" PROBLEM_NAME ".in", "r", stdin); freopen("io/" PROBLEM_NAME ".out", "w", stdout); #endif solve(); return 0; } // }}}