結果

問題 No.31 悪のミックスジュース
ユーザー koyumeishikoyumeishi
提出日時 2015-07-17 19:49:41
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,510 bytes
コンパイル時間 647 ms
コンパイル使用メモリ 77,136 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-22 17:00:51
合計ジャッジ時間 1,525 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 WA -
testcase_10 AC 1 ms
4,376 KB
testcase_11 WA -
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 WA -
testcase_16 WA -
権限があれば一括ダウンロードができます

ソースコード

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+1, 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