結果

問題 No.3144 Parentheses Modification and Rotation (01 Ver.)
ユーザー shobonvip
提出日時 2025-05-16 23:12:54
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 22 ms / 2,000 ms
コード長 1,913 bytes
コンパイル時間 3,828 ms
コンパイル使用メモリ 255,592 KB
実行使用メモリ 14,884 KB
最終ジャッジ日時 2025-06-28 17:24:00
合計ジャッジ時間 5,522 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 39
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
	author:  shobonvip
	created: 2025.05.16 23:02:47
**/

#include<bits/stdc++.h>
using namespace std;

//* ATCODER
#include<atcoder/all>
using namespace atcoder;
typedef modint998244353 mint;
//*/

/* BOOST MULTIPRECISION
#include<boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
//*/

typedef long long ll;

#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)
#define all(v) v.begin(), v.end()

template <typename T> bool chmin(T &a, const T &b) {
	if (a <= b) return false;
	a = b;
	return true;
}

template <typename T> bool chmax(T &a, const T &b) {
	if (a >= b) return false;
	a = b;
	return true;
}

template <typename T> T max(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]);
	return ret;
}

template <typename T> T min(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]);
	return ret;
}

template <typename T> T sum(vector<T> &a){
	T ret = 0;
	for (int i=0; i<(int)a.size(); i++) ret += a[i];
	return ret;
}

struct S {
	ll v, rui;	
};

S op(S a, S b) {
	return {a.v + b.v, min(a.rui, a.v + b.rui)};
}

S e() {
	return {0LL, 0LL};
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	int n; cin >> n;
	
	string s; cin >> s;
	ll r, m; cin >> r >> m;
	if (n % 2 == 1) {
		cout << -1 << '\n';
		return 0;
	}

	s += s;


	vector<S> init(2*n);
	rep(i,0,2*n) {
		if (s[i] == '(') {
			init[i] = S{1, 0};
		}else {
			init[i] = S{-1, -1};
		}
	}

	ll left = 0;
	ll right = 0;
	rep(i,0,n) if (s[i] == '(') {left++;} else {right++;}



	segtree<S,op,e> seg(init);
	ll ans = 7e18;
	for (int i=0; i<n; i++) {
		chmin(ans, r * ((n - i) % n)
			+ 2 * m * ((- seg.prod(i,i+n).rui) - min(abs(right-n/2) * 2, (-seg.prod(i,i+n).rui)))
			+ m * abs(right-n/2)
		);
	}
	cout << ans << endl;
}

0