QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#261712 | #6454. Unbalanced Parentheses | ZhouShang | WA | 1ms | 5680kb | C++14 | 2.8kb | 2023-11-23 09:15:59 | 2023-11-23 09:16:01 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
#define rep(i,a,b) for(int i = a; i < (b); i++)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define PB push_back
#define FS first
#define SD second
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define ary(k) array<int, k>
typedef pair<int, int> pii;
typedef vector<int> vi;
int cost[1000002];
int change[1000002];
signed main() {
cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit);
int n, k;
cin >> n >> k;
string slo;
cin >> slo;
int pocz_min = 0;
priority_queue<pii> pq;
set<pii> used;
for (int i = 0; i < n; i++) {
cin >> cost[i];
if (cost[i] < 0) {
pocz_min -= cost[i];
cost[i] *= -1;
if (slo[i] == ')')
slo[i] = '(';
else
slo[i] = ')';
}
if (slo[i] == ')')
pq.push({-cost[i], i});
}
if (n%2) {
cout << -pocz_min << "\n";
return 0;
}
if (2*k >= n) {
cout << "?\n";
return 0;
}
int k1 = 0, k2 = 0;
for (int i = 0; i < n; i++) {
if (slo[i] == ')') {
k2--;
} else {
k2++;
}
cmx(k1, -k2);
}
int best = 1e18;
int akt = 0, curst = 0, curcost = 0;
while (!pq.empty() && curst + k2/2 <= k) {
auto [c, id] = pq.top();
pq.pop();
used.insert({-c, id});
curst++;
curcost += -c;
change[id] = 1;
}
cerr << best << "\n";
if (curst + k2/2 > k)
cmn(best, curcost);
for (int i = 0; i < n; i++) {
if (slo[i] == ')') {
akt++;
} else {
akt--;
}
if (change[i] == 1) {
curst--;
curcost -= cost[i];
used.erase({-cost[i], i});
}
if (slo[i] == '(') {
pq.push({-cost[i], i});
}
int have = k2/2 + ((akt+1)/2) * 2;
while (have + curst > k) {
auto [c, id] = *used.begin();
if (c < 0) {
curst--;
curcost += c;
used.erase({c, id});
change[id] = 0;
} else {
break;
}
}
cerr << i << " " << have << "\n";
while (!pq.empty() && curst + have <= k) {
auto [c, id] = pq.top();
pq.pop();
used.insert({-c, id});
curst++;
curcost += -c;
change[id] = 1;
}
if (have + curst > k)
cmn(best, curcost);
cerr << curcost << "\n";
}
cout << best - pocz_min << "\n";
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 5680kb
input:
4 1 ((() 480 617 -570 928
output:
527
result:
wrong answer 1st lines differ - expected: '480', found: '527'