結果

問題 No.31 悪のミックスジュース
ユーザー koyumeishikoyumeishi
提出日時 2015-07-17 19:47:03
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 130 ms / 5,000 ms
コード長 1,512 bytes
コンパイル時間 645 ms
コンパイル使用メモリ 77,196 KB
実行使用メモリ 11,032 KB
最終ジャッジ日時 2023-10-12 17:15:09
合計ジャッジ時間 2,421 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 76 ms
8,248 KB
testcase_02 AC 80 ms
8,388 KB
testcase_03 AC 130 ms
10,712 KB
testcase_04 AC 129 ms
10,704 KB
testcase_05 AC 129 ms
10,752 KB
testcase_06 AC 1 ms
4,476 KB
testcase_07 AC 129 ms
10,772 KB
testcase_08 AC 129 ms
10,688 KB
testcase_09 AC 129 ms
11,032 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 3 ms
4,352 KB
testcase_12 AC 32 ms
5,668 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 115 ms
10,296 KB
testcase_15 AC 1 ms
4,352 KB
testcase_16 AC 3 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <set>
using namespace std;

long long gcd(long long a, long long b){
	if(b==0) return a;
	return gcd(b, a%b);
}

long long lcm(long long a, long long b){
	if(a<b) swap(a,b);
	if(b==1) return a;
	return a * (b/gcd(a,b));
}


bool comp(pair<long long,long long>& a, pair<long long,long long>& b){
	long long lcm_ = lcm(a.second, b.second);
	return a.first * (lcm_/a.second) > b.first * (lcm_/b.second);
}

int main(){
	int n;
	long long v;
	cin >> n >> v;
	vector<long long> c(n);
	for(int i=0; i<n; i++){
		cin >> c[i];
	}
	vector<long long> d(n+1, 0);
	for(int i=0; i<n; i++){
		d[i+1] = c[i] + d[i];
	}

	long long ans = d[n];
	v -= n;

	if(v <= 0){
		cout << ans << endl;
		return 0;
	}

	vector<long long> dp(n*n*n, 1LL<<58);
	dp[0] = 0;
	for(int i=0; i<dp.size(); i++){
		for(int k=0; k<=n && i+k<dp.size(); k++){
			dp[i+k] = min(dp[i+k], dp[i] + d[k]);
		}
	}

	pair<long long, long long> best = {d[1],1};
	for(int i=2; i<=n; i++){
		pair<long long,long long> next = {d[i], i};
		if(comp(best, next)){
			best = next;
		}
	}

	long long lb = -1;
	long long ub = v/best.second +1;
	while(ub-lb>1){
		long long mid = (ub+lb)/2;
		long long rem = v - best.second * mid;
		if(rem >= dp.size()){
			lb = mid;
		}else{
			ub = mid;
		}
	}

	ans += best.first * ub;
	v -= best.second * ub;


	ans += dp[v];
	cout << ans << endl;
	return 0;
}
0