#4 TLE求助
查看原帖
#4 TLE求助
785704
jiawenbo楼主2024/3/9 08:41
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll book[100001];
queue <ll> A,C;
ll read () 
{
    register ll x = 0,f = 1;register char ch;
    ch = getchar();
    while(ch > '9' || ch < '0') if(ch == '-') f = -f,ch = getchar();
    while(ch <= '9' && ch >= '0') x = x * 10 + ch - 48,ch = getchar();
    return x * f;
}
void write (ll x)
{
	if (x < 0) putchar('-'),x = -x;
	if (x > 9) write(x / 10),putchar(x % 10 + '0');
	else putchar(x + '0');
	return; 
}
int main()
{
	ll n = read();
	for (ll i = 1; i <= n; i ++)
	{
		ll x = read();
		book[x] ++;
	}
	ll p = 1;
	for (ll i = 1; i <= 100000; i ++) 
	{
		if (book[i])
		{
			for (ll j = 1; j <= book[i]; j ++)
			{
				A.push(i);
			}
		}
	}
	ll c = 0;
	for (ll i = 1; i < n; i ++)
	{
		ll x,x1;
		if (A.front() < C.front() && A.size() || !C.size()) x = A.front(),A.pop();
		else x = C.front(),C.pop();
		if (A.front() < C.front() && A.size() || !C.size()) x1 = A.front(),A.pop();
		else x1 = C.front(),C.pop();
		c += x + x1;
		C.push(x + x1);
	}
	write(c);
	return 0;
}
2024/3/9 08:41
加载中...