#include <bits/stdc++.h>
using namespace std;
#define int long long
const long long INF = 1e17 + 420 + 69;
signed main() {
int a, n, m;
cin >> a >> n >> m;
vector<bool> israin(a + 1, 0);
vector<int> is_left_end(a + 1, 0);
for (int i = 0; i < n; i++) {
int l, r;
cin >> l >> r;
is_left_end[l] = 1;
for (int j = l; j <= r; j++) { //changed this
israin[j] = true;
}
}
vector<int> up(a + 1, INF);
vector<array<int, 2>> umb(1, {0, 0});
for (int i = 0; i < m; i++) {
int idx, p;
cin >> idx >> p;
up[idx] = min(up[idx], p);
}
for (int i = 0; i <= a; i++) {
if (up[i] >= INF) {
continue;
}
umb.push_back({i, up[i]});
}
m = umb.size();
vector<vector<int>> dp(a + 1, vector<int> (m, INF));
if (!israin[0]) {
dp[0][0] = 0;
}
if (umb[1][0] == 0) {
dp[0][1] = 0;
}
long long mn[a + 1][2];
mn[0][0] = min(dp[0][0], dp[1][0]);
mn[0][1] = dp[0][1] + umb[1][1];
for (int i = 1; i <= a; i++) {
if (!israin[i]) {
for (int j = 0; j < m; j++) {
dp[i][0] = min(dp[i][0], dp[i - 1][j]);
}
}
for (int j = 1; j < m; j++) {
if (umb[j][0] < i) {
dp[i][j] = min(dp[i][j], dp[i - 1][j] + umb[j][1]);
}
else if (umb[j][0] == i) {
if(!israin[i])
dp[i][j] = min(dp[i][j], dp[i][0]);
else{
if(is_left_end[i]){
// dp[i][j] = min(dp[i][j], mn[i - 1][0]);
for(int k = 0; k < m; ++k){
dp[i][j] = min(dp[i][j], dp[i - 1][k]);
}
// dp[i][j] = min(dp[i][j], dp[i - 1][k] + umb[k][1] * (1 - is_left_end[i]));
}else{
// dp[i][j] = min(dp[i][j], mn[i - 1][1]);
for(int k = 1; k < m; ++k){
dp[i][j] = min(dp[i][j], dp[i - 1][k] + umb[k][1]);
}
}
}
}
// mn[i][0] = mn[i][1] = INF;
// for(int j = 0; j < m; ++j){
// mn[i][0] = min(mn[i][0], dp[i][j]);
// }
// for(int j = 1; j < m; ++j){
// mn[i][1] = min(mn[i][1], dp[i][j] + umb[j][1]);
// }
}
}
// for (int i = 0; i <= a; i++) {
// for (int j = 0; j < m; j++) {
// cerr << dp[i][j] << ' ';
// }
// cerr << "\n";
// }
long long ans = INF;
for(int j = 0; j < m; ++j){
ans = min(ans, (long long)dp[a][j]);
}
cout << (ans >= INF ? -1 : ans) << '\n';
}
36. Valid Sudoku | 557. Reverse Words in a String III |
566. Reshape the Matrix | 167. Two Sum II - Input array is sorted |
387. First Unique Character in a String | 383. Ransom Note |
242. Valid Anagram | 141. Linked List Cycle |
21. Merge Two Sorted Lists | 203. Remove Linked List Elements |
733. Flood Fill | 206. Reverse Linked List |
83. Remove Duplicates from Sorted List | 116. Populating Next Right Pointers in Each Node |
145. Binary Tree Postorder Traversal | 94. Binary Tree Inorder Traversal |
101. Symmetric Tree | 77. Combinations |
46. Permutations | 226. Invert Binary Tree |
112. Path Sum | 1556A - A Variety of Operations |
136. Single Number | 169. Majority Element |
119. Pascal's Triangle II | 409. Longest Palindrome |
1574A - Regular Bracket Sequences | 1574B - Combinatorics Homework |
1567A - Domino Disaster | 1593A - Elections |