이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define endl '\n'
using namespace std;
const int maxn = 2e5 + 3;
int n, k;
pair <int, int> a[maxn];
int t[maxn];
void read()
{
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> a[i].first >> a[i].second;
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].first, a[i].second))
v.push_back(j);
int groups = 1;
for (int j = 0; j < (int)v.size()-1; j++)
for (int p = v[j] + 1; p < v[j+1]; p++)
if (t[p] >= min(a[i].first, a[i].second))
{
groups++;
break;
}
int first = -1, last = -1;
for (int j = 1; j <= k; j++)
if (t[j] >= min(a[i].first, a[i].second))
{
if (t[j] >= max(a[i].first, a[i].second))
first = 2;
else
first = 1;
break;
}
for (int j = k; j >= 1; j--)
if (t[j] >= min(a[i].first, a[i].second))
{
if (t[j] >= max(a[i].first, a[i].second))
last = 2;
else
last = 1;
break;
}
if (first == -1)
{
ans += (long long)a[i].first;
continue;
}
if (a[i].first < a[i].second)
{
if (((int)v.size() - groups) % 2 == 0)
{
if (first == 2)
ans += (long long)a[i].second;
else
{
if (last == 1)
ans += (long long)a[i].second;
else
ans += (long long)a[i].first;
}
}
else
{
if (first == 1)
ans += (long long)a[i].second;
else
{
if (last == 1)
ans += (long long)a[i].second;
else
ans += (long long)a[i].first;
}
}
}
else
{
if (((int)v.size() - groups) % 2 == 0)
{
if (last == 1)
ans += (long long)a[i].first;
else
ans += (long long)a[i].second;
}
else
ans += (long long)a[i].second;
}
}
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |