Submission #36446998


Source Code Expand

#include "bits/stdc++.h"
using namespace std;
#define int long long
#ifdef LOCAL
#define watch(x) cout << #x << ':' << x << ' ';
#else
#define watch(x) 0
#endif
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;

void init() {
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	freopen("err.txt", "w", stderr);
#endif
}

void solve() {}

int32_t main()
{
	IOS;
	init();
	int t = 1;
	// cin >> t;

	while (t--) {

		int n, m, sum = 0;
		cin >> n >> m;

		vector<int> v(n);
		map<int, int> mp;
		for (int i = 0; i < n; i++) {
			cin >> v[i];
			sum += v[i];
			mp[v[i]]++;
		}

		int ans = 1e15;
		// sort(all(v));
		// for (int i : v) cout << i << " ";
		// cout << "\n";
		// watch(sum);
		set<int> st;

		int res = 0, last = prev(mp.end())->ff;
		int mx = last;
		// watch(last);
		auto it = prev(mp.end());

		auto jt = it;

		while (1) {
			if (/*jt->ff == (last - 1 + m) % m ||*/ jt->ff == last) {
				res += jt->ff * jt->ss;
				st.insert(jt->ff);
				last = (last - 1) % m;
				// watch(last);

				if (jt == mp.begin()) break;
				jt--;
			}
			else break;
		}

		last = 0;
		jt = mp.begin();
		// watch(mx);

		if (mp.count((mx + 1) % m)) {
			last = (mx + 1) % m;
			// watch(last);

			while (1) {
				if (st.count(jt->ff)) break;

				if (jt->ff == last /*|| jt->ff == (last + 1) % m*/) {
					res += jt->ff * jt->ss;
					st.insert(jt->ff);
					last = (last + 1) % m;

					jt++;
					if (jt == mp.end()) break;
				}
				else break;
			}
		}

		ans = sum - res;

		for (int i : st) {
			// watch(i);
			mp.erase(i);
		}

		// watch(res);

		if (!mp.empty()) {
			jt = mp.begin();
			last = -1;
			res = 0;

			while (jt != mp.end()) {
				if (last == -1) {
					last = jt->ff;
					res = jt->ff * jt->ss;
				}
				else if (jt->ff == last) {
					res += jt->ff * jt->ss;
					last = (last + 1) % m;
				}
				else {
					ans = min(ans, sum - res);
					res = jt->ff * jt->ss;
					last = (jt->ff + 1) % m;
				}

				jt++;
			}

			if (last != -1) {
				ans = min(ans, sum - res);
			}
		}

		cout << ans << "\n";

	}
	return 0;
}

Submission Info

Submission Time
Task D - Takahashi's Solitaire
User vantaablackk
Language C++ (GCC 9.2.1)
Score 400
Code Size 2494 Byte
Status AC
Exec Time 224 ms
Memory 26668 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 3
AC × 40
Set Name Test Cases
Sample example0.txt, example1.txt, example2.txt
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, 035.txt, 036.txt, example0.txt, example1.txt, example2.txt
Case Name Status Exec Time Memory
000.txt AC 8 ms 3524 KiB
001.txt AC 18 ms 4664 KiB
002.txt AC 23 ms 4728 KiB
003.txt AC 192 ms 26668 KiB
004.txt AC 206 ms 26664 KiB
005.txt AC 224 ms 26620 KiB
006.txt AC 209 ms 26620 KiB
007.txt AC 28 ms 4780 KiB
008.txt AC 16 ms 4728 KiB
009.txt AC 18 ms 4684 KiB
010.txt AC 5 ms 3840 KiB
011.txt AC 71 ms 9700 KiB
012.txt AC 145 ms 16344 KiB
013.txt AC 154 ms 17016 KiB
014.txt AC 156 ms 17124 KiB
015.txt AC 156 ms 17092 KiB
016.txt AC 164 ms 17132 KiB
017.txt AC 157 ms 17084 KiB
018.txt AC 156 ms 17176 KiB
019.txt AC 20 ms 4660 KiB
020.txt AC 25 ms 4740 KiB
021.txt AC 35 ms 4744 KiB
022.txt AC 47 ms 5784 KiB
023.txt AC 91 ms 10156 KiB
024.txt AC 153 ms 17928 KiB
025.txt AC 150 ms 17172 KiB
026.txt AC 156 ms 17128 KiB
027.txt AC 149 ms 17012 KiB
028.txt AC 147 ms 17188 KiB
029.txt AC 151 ms 17092 KiB
030.txt AC 175 ms 20524 KiB
031.txt AC 151 ms 17020 KiB
032.txt AC 141 ms 16328 KiB
033.txt AC 149 ms 17172 KiB
034.txt AC 155 ms 17600 KiB
035.txt AC 150 ms 17412 KiB
036.txt AC 151 ms 17016 KiB
example0.txt AC 3 ms 3496 KiB
example1.txt AC 2 ms 3596 KiB
example2.txt AC 3 ms 3492 KiB