#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define endl '\n'
using namespace std;
const int maxn = 2e5 + 3;
int n, k;
int a[maxn], b[maxn], t[maxn];
void read()
{
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> a[i] >> b[i];
for (int i = 1; i <= k; i++)
cin >> t[i];
}
void solve()
{
long long ans = 0;
for (int i = 1; i <= n; i++)
{
vector <int> v;
for (int j = 1; j <= k; j++)
{
if (t[j] >= max(a[i], b[i]))
v.push_back(1);
else
if (t[j] >= min(a[i], b[i]))
v.push_back(2);
}
int diff = 1;
for (int j = 0; j < (int)v.size()-1; j++)
if (v[j] != v[j+1])
diff++;
int cnt = 0;
for (auto j: v)
{
if (j == 2)
break;
cnt++;
}
if (cnt == (int)v.size())
{
ans += (cnt & 1) ? b[i] : a[i];
continue;
}
int first;
if (cnt % 2 == 0)
{
if (cnt != 0)
diff--;
first = 2;
}
else
first = 1;
cnt = 0;
for (int j = (int)v.size()-1; j >= 0; j--)
{
if (v[j] == 2)
break;
cnt++;
}
if (cnt % 2 == 0 && cnt != 0)
diff--;
int temp;
if (diff == 0)
temp = 0;
else
if (a[i] >= b[i])
{
if (first == 1)
temp = diff;
else
temp = diff + 1;
}
else
{
if (first == 2)
temp = diff;
else
temp = max(1, diff - 1);
}
// cout << i << " " << temp << endl;
if (temp & 1)
ans += b[i];
else
ans += a[i];
}
cout << ans << endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
read();
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
5 ms |
364 KB |
Output is correct |
3 |
Correct |
8 ms |
364 KB |
Output is correct |
4 |
Correct |
8 ms |
364 KB |
Output is correct |
5 |
Correct |
8 ms |
364 KB |
Output is correct |
6 |
Correct |
8 ms |
364 KB |
Output is correct |
7 |
Correct |
5 ms |
364 KB |
Output is correct |
8 |
Correct |
6 ms |
364 KB |
Output is correct |
9 |
Correct |
5 ms |
364 KB |
Output is correct |
10 |
Correct |
4 ms |
364 KB |
Output is correct |
11 |
Correct |
5 ms |
364 KB |
Output is correct |
12 |
Correct |
5 ms |
364 KB |
Output is correct |
13 |
Correct |
5 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
5 ms |
364 KB |
Output is correct |
3 |
Correct |
8 ms |
364 KB |
Output is correct |
4 |
Correct |
8 ms |
364 KB |
Output is correct |
5 |
Correct |
8 ms |
364 KB |
Output is correct |
6 |
Correct |
8 ms |
364 KB |
Output is correct |
7 |
Correct |
5 ms |
364 KB |
Output is correct |
8 |
Correct |
6 ms |
364 KB |
Output is correct |
9 |
Correct |
5 ms |
364 KB |
Output is correct |
10 |
Correct |
4 ms |
364 KB |
Output is correct |
11 |
Correct |
5 ms |
364 KB |
Output is correct |
12 |
Correct |
5 ms |
364 KB |
Output is correct |
13 |
Correct |
5 ms |
364 KB |
Output is correct |
14 |
Correct |
689 ms |
748 KB |
Output is correct |
15 |
Correct |
2889 ms |
1188 KB |
Output is correct |
16 |
Execution timed out |
3071 ms |
1952 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
5 ms |
364 KB |
Output is correct |
3 |
Correct |
8 ms |
364 KB |
Output is correct |
4 |
Correct |
8 ms |
364 KB |
Output is correct |
5 |
Correct |
8 ms |
364 KB |
Output is correct |
6 |
Correct |
8 ms |
364 KB |
Output is correct |
7 |
Correct |
5 ms |
364 KB |
Output is correct |
8 |
Correct |
6 ms |
364 KB |
Output is correct |
9 |
Correct |
5 ms |
364 KB |
Output is correct |
10 |
Correct |
4 ms |
364 KB |
Output is correct |
11 |
Correct |
5 ms |
364 KB |
Output is correct |
12 |
Correct |
5 ms |
364 KB |
Output is correct |
13 |
Correct |
5 ms |
364 KB |
Output is correct |
14 |
Correct |
689 ms |
748 KB |
Output is correct |
15 |
Correct |
2889 ms |
1188 KB |
Output is correct |
16 |
Execution timed out |
3071 ms |
1952 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |