結果

問題 No.1288 yuki collection
ユーザー Kite_kumaKite_kuma
提出日時 2020-11-13 22:32:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4,661 ms / 5,000 ms
コード長 14,913 bytes
コンパイル時間 2,290 ms
コンパイル使用メモリ 194,072 KB
実行使用メモリ 48,156 KB
最終ジャッジ日時 2024-07-22 21:32:09
合計ジャッジ時間 54,269 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 1,625 ms
30,428 KB
testcase_14 AC 1,821 ms
30,464 KB
testcase_15 AC 1,168 ms
26,168 KB
testcase_16 AC 1,221 ms
26,732 KB
testcase_17 AC 1,794 ms
30,564 KB
testcase_18 AC 1,816 ms
30,664 KB
testcase_19 AC 1,696 ms
30,436 KB
testcase_20 AC 2,024 ms
31,640 KB
testcase_21 AC 4,253 ms
42,540 KB
testcase_22 AC 4,236 ms
42,632 KB
testcase_23 AC 4,207 ms
42,616 KB
testcase_24 AC 1,816 ms
31,068 KB
testcase_25 AC 1,710 ms
31,656 KB
testcase_26 AC 1,870 ms
31,196 KB
testcase_27 AC 572 ms
18,284 KB
testcase_28 AC 854 ms
25,732 KB
testcase_29 AC 686 ms
27,520 KB
testcase_30 AC 90 ms
28,800 KB
testcase_31 AC 135 ms
29,584 KB
testcase_32 AC 141 ms
28,892 KB
testcase_33 AC 4,493 ms
46,704 KB
testcase_34 AC 2,516 ms
32,052 KB
testcase_35 AC 2,242 ms
30,820 KB
testcase_36 AC 1,032 ms
31,540 KB
testcase_37 AC 1,138 ms
31,012 KB
testcase_38 AC 4,661 ms
48,156 KB
testcase_39 AC 819 ms
46,188 KB
testcase_40 AC 42 ms
28,364 KB
testcase_41 AC 2 ms
5,376 KB
testcase_42 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*	author:  Kite_kuma
	created: 2020.11.13 21:49:00 */

// #ifdef LOCAL
// #define _GLIBCXX_DEBUG
// #endif
#include <bits/stdc++.h>
using namespace std;

#pragma region aliases

#define rep(i, n) for(long long i = 0; i < (n); i++)
#define rrep(i, n) for(long long i = (n)-1; i > -1; i--)
#define Rep(i, m, n) for(long long i = (m); i < (n); i++)
#define rRep(i, m, n) for(long long i = (n)-1; i >= (m); i--)
#define REP(i, m, n, p) for(long long i = m; i < n; i += p)
#define foa(s, v) for(auto &s : v)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define bcnt(n) __builtin_popcountll(n)
#define endk endl
#define ednl endl
#define enld endl

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vb = vector<bool>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using mll = map<long long, long long>;
using pll = pair<long long, long long>;
using qll = queue<long long>;
using sll = set<long long>;
using vpll = vector<pair<long long, long long>>;
template <class T = ll>
using V = vector<T>;
template <class T = ll>
using VV = V<V<T>>;
template <class T = ll>
using VVV = V<V<V<T>>>;
//昇順pq(小さい方から取り出す)
template <class T = ll>
using pqup = priority_queue<T, vector<T>, greater<T>>;
//降順pq(大きい方から取り出す)
template <class T = ll>
using pqdn = priority_queue<T>;

#pragma endregion

#pragma region constants

long long const limLL = 9223372036854775807;  // POW(2,63)-1 ~ 9.22e18
long long const dekai = 3e16;
const long double pi = acos(-1);
const char dl = '\n';
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};

const int mod = 1000000007;
// const int mod = 998244353;

#pragma endregion

#pragma region basic_procedure

template <class T>
inline bool isin(T x, T lef, T rig) {
	return ((lef <= x) && (x < rig));
}

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

void Yes(bool f = 1) { cout << (f ? "Yes" : "No") << "\n"; }
void No() { cout << "No\n"; }
void YES(bool f = 1) { cout << (f ? "YES" : "NO") << "\n"; }
void NO() { cout << "NO\n"; }
template <class T>
void drop(T answer) {
	cout << answer << "\n";
	exit(0);
}
void err() {
	cout << -1 << "\n";
	exit(0);
}

template <class T>
void vout(vector<T> &v, bool tate = 0) {
	if(v.size() > 0) {
		for(auto it = v.begin(); it < v.end(); it++) {
			cout << *it;
			if(it != v.end() - 1) {
				if(tate)
					cout << endl;
				else
					cout << " ";
			}
		}
	}
	cout << endl;
}

template <class T>
void add(vector<T> &v, T val) {	 //ベクトルの各要素に加算
	for(auto &a : v) a += val;
	return;
}

// vectorの中身を数える map<要素,個数>を返す
template <class T>
map<T, long long> cntv(vector<T> v) {
	map<T, long long> m;
	for(auto &g : v) {
		if(m.count(g))
			m[g]++;
		else
			m[g] = 1;
	}
	return m;
}

//配列圧縮
//{1,36,1,3,8,-2,-92}を
//{2, 5,2,3,4, 1,  0}にする
template <class T>
vector<int> press(vector<T> &v) {
	int n = v.size();
	vector<int> w(n);
	map<T, int> m;
	for(T &p : v) m[p] = 0;
	int i = 0;
	for(auto &p : m) {
		p.second = i;
		i++;
	}
	for(int i = 0; i < n; i++) w.at(i) = m[v.at(i)];
	return w;
}

// 切り上げ除算
template <class T>
T dup(T a, T b) {
	assert(b != 0);
	T x = abs(a);
	T y = abs(b);
	T z = (x + y - 1) / y;
	if((a < 0 && b > 0) || (a > 0 && b < 0))
		return -z;
	else if(a == 0)
		return 0;
	else
		return z;
}

long long POW(long long a, long long n) {
	long long res = 1;
	while(n > 0) {
		if(n & 1) res = res * a;
		a = a * a;
		n >>= 1;
	}
	return res;
}

template <class T>
T greatest_lower_multiple(T x, T d) {
	if(d == 0) return 0;
	if(d < 0) d *= -1;
	T y = x % d;
	if(y < 0) y += d;
	return x - y;
}

template <class T>
T least_upper_multiple(T x, T d) {
	return -greatest_lower_multiple(-x, d);
}

long long modpow(long long a, long long n, long long mod) {	 // a^n mod
	assert(n >= 0);
	if(mod == 1) return 0LL;
	long long res = 1;
	while(n > 0) {
		if(n & 1) res = res * a % mod;
		a = a * a % mod;
		n >>= 1;
	}
	return res;
}

// a * x % mod == __gcd(a,mod)なるxを返す
// a が modの倍数でないことが条件
long long modinv(long long a, long long mod) {
	long long b = mod, u = 1, v = 0;
	while(b) {
		long long t = a / b;
		a -= t * b;
		swap(a, b);
		u -= t * v;
		swap(u, v);
	}
	u %= mod;
	if(u < 0) u += mod;
	return u;
}

vvll comb(100, vll(100, -1));
long long com(long long n, long long k) {  //普通の二項計数(overflowに注意)
	assert(n < 100 && k < 100);
	if(n < k || k < 0 || n < 0) return 0;
	if(comb[n][k] != -1) return comb[n][k];
	ll res;
	if(n - k < k)
		res = com(n, n - k);
	else if(k == 0)
		res = 1;
	else
		res = com(n - 1, k - 1) + com(n - 1, k);
	comb[n][k] = res;
	return res;
}

// nCk modを求める
const int MAX = 5100000;
// この値は求める二項計数の値に応じて変える
// MAX=3*10^7のとき1900msほど、ほぼ比例
// MAX=5*10^6程度ならそれほど気にしなくてよい(300ms程)
long long fac[MAX], finv[MAX], inv[MAX];

void cominit() {
	// テーブルを作る前処理
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for(int i = 2; i < MAX; i++) {
		fac[i] = fac[i - 1] * i % mod;
		inv[i] = mod - inv[mod % i] * (mod / i) % mod;
		finv[i] = finv[i - 1] * inv[i] % mod;
	}
}
long long commod(long long n, long long k) {  // 二項係数
	if(n < k) return 0;
	if(n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
long long pmod(long long n, long long k) {	// 順列
	if(n < k) return 0;
	if(n < 0 || k < 0) return 0;
	return fac[n] * finv[n - k] % mod;
}
// n個の区別しないボールを区別するk個の箱に入れる方法の総数
long long hmod(long long n, long long k) {	// 重複組み合わせ
	return commod(n + k - 1, n);
}
#pragma endregion

#pragma region input
#define VEC(type, name, size) \
	vector<type> name(size);  \
	INPUT(name)
#define VVEC(type, name, h, w)                     \
	vector<vector<type>> name(h, vector<type>(w)); \
	INPUT(name)
#define INT(...)     \
	int __VA_ARGS__; \
	INPUT(__VA_ARGS__)
#define LL(...)            \
	long long __VA_ARGS__; \
	INPUT(__VA_ARGS__)
#define STR(...)        \
	string __VA_ARGS__; \
	INPUT(__VA_ARGS__)
#define CHAR(...)     \
	char __VA_ARGS__; \
	INPUT(__VA_ARGS__)
#define DOUBLE(...)     \
	double __VA_ARGS__; \
	INPUT(__VA_ARGS__)
#define LD(...)              \
	long double __VA_ARGS__; \
	INPUT(__VA_ARGS__)

template <class T>
void scan(T &a) {
	cin >> a;
}
template <class T>
void scan(vector<T> &a) {
	for(auto &i : a) scan(i);
}
template <class T, class L>
void scan(pair<T, L> &p) {
	scan(p.first);
	scan(p.second);
}
void INPUT() {}
template <class Head, class... Tail>
void INPUT(Head &head, Tail &... tail) {
	scan(head);
	INPUT(tail...);
}
template <class T>
inline void print(T x) {
	cout << x << '\n';
}

template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
	is >> p.first >> p.second;
	return is;
}
#pragma endregion

#pragma region debug

#pragma region output
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
	os << p.first << " " << p.second;
	return os;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
	for(int i = 0; i < (int)v.size(); i++) {
		if(i) os << " ";
		os << v[i];
	}
	return os;
}
#pragma endregion

#pragma region view

template <typename T>
void view(const T e) {
	std::cerr << e;
}

template <typename T, typename U>
void view(const std::pair<T, U> &p) {
	std::cerr << "(";
	view(p.first);
	cerr << ", ";
	view(p.second);
	cerr << ")";
}

template <typename T>
void view(const std::set<T> &s) {
	if(s.empty()) {
		cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(auto &t : s) {
		view(t);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::vector<T> &v) {
	if(v.empty()) {
		cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(const auto &e : v) {
		view(e);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::vector<std::vector<T>> &vv) {
	std::cerr << "{\n";
	for(const auto &v : vv) {
		std::cerr << "\t";
		view(v);
		std::cerr << "\n";
	}
	std::cerr << "}";
}

template <typename T, typename U>
void view(const std::vector<std::pair<T, U>> &v) {
	std::cerr << "{\n";
	for(const auto &c : v) {
		std::cerr << "\t(";
		view(c.first);
		cerr << ", ";
		view(c.second);
		cerr << ")\n";
	}
	std::cerr << "}";
}

template <typename T, typename U>
void view(const std::map<T, U> &m) {
	std::cerr << "{\n";
	for(const auto &t : m) {
		std::cerr << "\t[";
		view(t.first);
		cerr << "] : ";
		view(t.second);
		cerr << "\n";
	}
	std::cerr << "}";
}

#pragma endregion

// when debugging : g++ foo.cpp -DLOCAL
#ifdef LOCAL
void debug_out() {}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	view(H);
	cerr << ", ";
	debug_out(T...);
}
#define debug(...)                                           \
	do {                                                     \
		cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : ["; \
		debug_out(__VA_ARGS__);                              \
		cerr << "\b\b]\n";                                   \
	} while(0)
#define dump(x)                                 \
	do {                                        \
		cerr << __LINE__ << " " << #x << " : "; \
		view(x);                                \
		cerr << "\n";                           \
	} while(0)

#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

#pragma endregion

#pragma region mincostflow

namespace atcoder {

template <class Cap, class Cost>
struct mcf_graph {
   public:
	mcf_graph() {}
	mcf_graph(int n) : _n(n), g(n) {}

	int add_edge(int from, int to, Cap cap, Cost cost) {
		assert(0 <= from && from < _n);
		assert(0 <= to && to < _n);
		int m = int(pos.size());
		pos.push_back({from, int(g[from].size())});
		g[from].push_back(_edge{to, int(g[to].size()), cap, cost});
		g[to].push_back(_edge{from, int(g[from].size()) - 1, 0, -cost});
		return m;
	}

	struct edge {
		int from, to;
		Cap cap, flow;
		Cost cost;
	};

	edge get_edge(int i) {
		int m = int(pos.size());
		assert(0 <= i && i < m);
		auto _e = g[pos[i].first][pos[i].second];
		auto _re = g[_e.to][_e.rev];
		return edge{
			pos[i].first, _e.to, _e.cap + _re.cap, _re.cap, _e.cost,
		};
	}
	std::vector<edge> edges() {
		int m = int(pos.size());
		std::vector<edge> result(m);
		for(int i = 0; i < m; i++) {
			result[i] = get_edge(i);
		}
		return result;
	}

	std::pair<Cap, Cost> flow(int s, int t) { return flow(s, t, std::numeric_limits<Cap>::max()); }
	std::pair<Cap, Cost> flow(int s, int t, Cap flow_limit) { return slope(s, t, flow_limit).back(); }
	std::vector<std::pair<Cap, Cost>> slope(int s, int t) { return slope(s, t, std::numeric_limits<Cap>::max()); }
	std::vector<std::pair<Cap, Cost>> slope(int s, int t, Cap flow_limit) {
		assert(0 <= s && s < _n);
		assert(0 <= t && t < _n);
		assert(s != t);
		// variants (C = maxcost):
		// -(n-1)C <= dual[s] <= dual[i] <= dual[t] = 0
		// reduced cost (= e.cost + dual[e.from] - dual[e.to]) >= 0 for all edge
		std::vector<Cost> dual(_n, 0), dist(_n);
		std::vector<int> pv(_n), pe(_n);
		std::vector<bool> vis(_n);
		auto dual_ref = [&]() {
			std::fill(dist.begin(), dist.end(), std::numeric_limits<Cost>::max());
			std::fill(pv.begin(), pv.end(), -1);
			std::fill(pe.begin(), pe.end(), -1);
			std::fill(vis.begin(), vis.end(), false);
			struct Q {
				Cost key;
				int to;
				bool operator<(Q r) const { return key > r.key; }
			};
			std::priority_queue<Q> que;
			dist[s] = 0;
			que.push(Q{0, s});
			while(!que.empty()) {
				int v = que.top().to;
				que.pop();
				if(vis[v]) continue;
				vis[v] = true;
				if(v == t) break;
				// dist[v] = shortest(s, v) + dual[s] - dual[v]
				// dist[v] >= 0 (all reduced cost are positive)
				// dist[v] <= (n-1)C
				for(int i = 0; i < int(g[v].size()); i++) {
					auto e = g[v][i];
					if(vis[e.to] || !e.cap) continue;
					// |-dual[e.to] + dual[v]| <= (n-1)C
					// cost <= C - -(n-1)C + 0 = nC
					Cost cost = e.cost - dual[e.to] + dual[v];
					if(dist[e.to] - dist[v] > cost) {
						dist[e.to] = dist[v] + cost;
						pv[e.to] = v;
						pe[e.to] = i;
						que.push(Q{dist[e.to], e.to});
					}
				}
			}
			if(!vis[t]) {
				return false;
			}

			for(int v = 0; v < _n; v++) {
				if(!vis[v]) continue;
				// dual[v] = dual[v] - dist[t] + dist[v]
				//         = dual[v] - (shortest(s, t) + dual[s] - dual[t]) + (shortest(s, v) + dual[s] - dual[v])
				//         = - shortest(s, t) + dual[t] + shortest(s, v)
				//         = shortest(s, v) - shortest(s, t) >= 0 - (n-1)C
				dual[v] -= dist[t] - dist[v];
			}
			return true;
		};
		Cap flow = 0;
		Cost cost = 0, prev_cost = -1;
		std::vector<std::pair<Cap, Cost>> result;
		result.push_back({flow, cost});
		while(flow < flow_limit) {
			if(!dual_ref()) break;
			Cap c = flow_limit - flow;
			for(int v = t; v != s; v = pv[v]) {
				c = std::min(c, g[pv[v]][pe[v]].cap);
			}
			for(int v = t; v != s; v = pv[v]) {
				auto &e = g[pv[v]][pe[v]];
				e.cap -= c;
				g[v][e.rev].cap += c;
			}
			Cost d = -dual[s];
			flow += c;
			cost += c * d;
			if(prev_cost == d) {
				result.pop_back();
			}
			result.push_back({flow, cost});
			prev_cost = cost;
		}
		return result;
	}

   private:
	int _n;

	struct _edge {
		int to, rev;
		Cap cap;
		Cost cost;
	};

	std::vector<std::pair<int, int>> pos;
	std::vector<std::vector<_edge>> g;
};

}  // namespace atcoder

#pragma endregion

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout << fixed << setprecision(15);
	srand((unsigned)time(NULL));

	INT(n);
	STR(st);

	vll val(n);
	rep(i, n) cin >> val[i];

	int v = 0;
	vi in(n);
	vi out(n);
	rep(i, n) { in[i] = v++; }
	rep(i, n) out[i] = v++;

	ll big = 1e9 + 10;

	int s = v++;
	int t = v++;

	atcoder::mcf_graph<ll, ll> g(v);

	string yuki = "yuki";

	rep(i, n) { g.add_edge(in[i], out[i], 1, big - val[i]); }
	rep(i, n) if(st[i] == 'y') g.add_edge(s, in[i], 1, 0);
	rrep(i, n) if(st[i] == 'i') g.add_edge(out[i], t, 1, 0);

	rep(i, n) {
		int l = 0;
		if(st[i] == 'i') continue;
		if(st[i] == 'y') l = 0;
		if(st[i] == 'u') l = 1;
		if(st[i] == 'k') l = 2;

		Rep(j, i + 1, n) {
			if(st[j] == yuki[l + 1]) {
				g.add_edge(out[i], in[j], 1, 0);
			}
		}
	}

	ll ans = 0;

	ll now = 0;

	// while(1) {
	// 	auto res = g.flow(s, t, 1);
	// 	if(res.first == 0) break;

	// 	now += -res.second + big * 4;
	// 	chmax(ans, now);
	// }

	auto res = g.slope(s, t);

	dump(res);

	foa(t, res) { chmax(ans, big * 4 * t.first - t.second); }

	drop(ans);

	return 0;
}
0