QOJ.ac
QOJ
ID | 提交记录ID | 题目 | Hacker | Owner | 结果 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|
#614 | #290034 | #7622. Yet Another Coffee | sw7777 | yllcm | Failed. | 2024-05-04 19:02:25 | 2024-05-04 19:02:27 |
详细
Extra Test:
Accepted
time: 1ms
memory: 7808kb
input:
1 1 1 5 1 2
output:
3
result:
ok 1 number(s): "3"
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#290034 | #7622. Yet Another Coffee | yllcm# | AC ✓ | 153ms | 11740kb | C++14 | 1.6kb | 2023-12-24 10:09:35 | 2023-12-24 10:09:36 |
answer
#include<bits/stdc++.h>
#define ll long long
#define db double
#define ull unsigned long long
#define eb emplace_back
#define pii pair<int, int>
#define FR first
#define SE second
#define int long long
using namespace std;
inline int read() {
int x = 0; bool op = false;
char c = getchar();
while(!isdigit(c))op |= (c == '-'), c = getchar();
while(isdigit(c))x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
return op ? -x : x;
}
const int N = 2e5 + 10;
const int INF = 1e18;
int n, m;
int a[N], b[N], vis[N], p[N], q[N];
void solve() {
n = read(); m = read();
for(int i = 1; i <= n; i++)a[i] = read();
for(int i = 1; i <= n; i++)b[i] = 0;
for(int i = 1; i <= m; i++) {
int r = read(), w = read();
b[r] -= w;
}
for(int i = 1; i <= n; i++)vis[i] = false;
for(int i = n - 1; i; i--)b[i] += b[i + 1];
for(int i = 1; i <= n; i++)b[i] += a[i];
for(int i = 1; i <= n; i++)p[i] = q[i] = i;
sort(p + 1, p + 1 + n, [&](int x, int y) {return a[x] < a[y];});
sort(q + 1, q + 1 + n, [&](int x, int y) {return b[x] < b[y];});
int res = 0, pos = 0;
for(int i = 1, j = 1, k = 1; i <= n; i++) {
if(i == 1)res += b[q[k]], pos = q[k], vis[pos] = true, k++;
else {
while(j <= n && vis[p[j]])j++;
while(k <= n && vis[q[k]])k++;
int v1 = a[p[j]], v2 = a[pos] - b[pos] + b[q[k]];
if(v1 < v2)res += v1, vis[p[j]] = true;
else res += v2, pos = q[k], vis[q[k]] = true;
}
printf("%lld ", res);
}
putchar('\n');
return ;
}
signed main() {
int test = read();
while(test--)solve();
return 0;
}