CSES - Leirikisa 6.3.2017 - Results
Submission details
Task:Aitaus
Sender:Kuha
Submission time:2017-03-06 16:24:39 +0200
Language:C++
Status:READY
Result:21
Feedback
groupverdictscore
#1ACCEPTED21
#20
#30
Test results
testverdicttimegroup
#1ACCEPTED0.05 s1details
#2ACCEPTED0.03 s1details
#3ACCEPTED0.03 s1details
#4ACCEPTED0.05 s1details
#5ACCEPTED0.03 s1details
#60.04 s2details
#70.04 s2details
#80.05 s2details
#90.03 s2details
#100.05 s2details
#110.25 s3details
#120.26 s3details
#130.25 s3details
#140.23 s3details
#150.27 s3details

Code

#include <bits/stdc++.h>

#define ll long long 
#define pii pair<int, int>
#define pllk pair<long long, long long>
#define M 1000000007
#define INF 0x5ADFACE5
#define LINF 0x51DEEFFEC7C0DECALL
#define N (1<<18)

using namespace std;

ll dp[1111][1111];
ll s[111111];

ll brute2 (int n, vector<ll>& v, int l, int r) {
	if (l == r) return 0;
	if (dp[l][r]) return dp[l][r];
	ll b = LINF;
	for (int k = l; k < r; k++) {
		ll a = brute2(n, v, l, k) + brute2(n, v, k + 1, r);
		if (a < b) b = a;
	}
	dp[l][r] = b + s[r] - s[l - 1];
	return dp[l][r];
}

ll brute3 (int n, vector<ll>& v, int l, int r) {
	if (l == r) return 0;
	if (dp[l][r]) return dp[l][r];
	int x = l;
	for (int k = (r - l); k >= 1; k /= 2) {
		while (x + k < r && 2 * s[x + k] <= s[r]) x += k;
	}
	ll b = brute3(n, v, l, x) + brute3(n, v, x + 1, r);
	if (x + 1 != r) b = min(brute3(n, v, l, x + 1) + brute3(n, v, x + 2, r), b);
	dp[l][r] = b + s[r] - s[l - 1];
	return dp[l][r];
}

int main () {
	int n;
	cin>>n;
	vector<ll> v(n + 1);
	for (int i = 1; i <= n; i++) {
		cin>>v[i];
	}
	sort(v.begin() + 1, v.end());
	for (int i = 1; i <= n; i++) s[i] = v[i] + s[i - 1];
	//cout<<brute2(n, v, 1, n)<<endl;
	cout<<brute3(n, v, 1, n)<<endl;
}

Test details

Test 1

Group: 1

Verdict: ACCEPTED

input
10
1 1 1 1 1 1 1 1 1 1

correct output
34

user output
34

Test 2

Group: 1

Verdict: ACCEPTED

input
10
1000 1000 1000 1000 1000 1000 ...

correct output
34000

user output
34000

Test 3

Group: 1

Verdict: ACCEPTED

input
10
713 590 643 971 889 796 972 3 ...

correct output
18501

user output
18501

Test 4

Group: 1

Verdict: ACCEPTED

input
10
991 740 433 558 522 338 240 27...

correct output
15614

user output
15614

Test 5

Group: 1

Verdict: ACCEPTED

input
10
397 775 568 796 632 898 214 84...

correct output
20791

user output
20791

Test 6

Group: 2

Verdict:

input
1000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
9976

user output
86557

Test 7

Group: 2

Verdict:

input
1000
1000000000 1000000000 10000000...

correct output
9976000000000

user output
86557000000000

Test 8

Group: 2

Verdict:

input
1000
377480143 777745434 296992200 ...

correct output
4829974948360

user output
31448497376320

Test 9

Group: 2

Verdict:

input
1000
599885439 985529375 118284730 ...

correct output
4880180545408

user output
31804998429719

Test 10

Group: 2

Verdict:

input
1000
695015028 950574688 862418845 ...

correct output
5089402448969

user output
33738922037434

Test 11

Group: 3

Verdict:

input
100000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
1668928

user output
(empty)

Test 12

Group: 3

Verdict:

input
100000
1000000000 1000000000 10000000...

correct output
1668928000000000

user output
(empty)

Test 13

Group: 3

Verdict:

input
100000
391395666 905124111 713186504 ...

correct output
818091245007558

user output
(empty)

Test 14

Group: 3

Verdict:

input
100000
535008265 825579494 118746814 ...

correct output
819167891088786

user output
(empty)

Test 15

Group: 3

Verdict:

input
100000
386356481 309596857 386341601 ...

correct output
816647450882063

user output
(empty)