結果
問題 | No.258 回転寿司(2) |
ユーザー |
![]() |
提出日時 | 2015-07-31 22:56:21 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 3 ms / 2,000 ms |
コード長 | 892 bytes |
コンパイル時間 | 761 ms |
コンパイル使用メモリ | 78,020 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-06 18:45:28 |
合計ジャッジ時間 | 4,044 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 67 |
ソースコード
#include <iostream>#include <vector>#include <cstdio>#include <sstream>#include <map>#include <string>#include <algorithm>#include <queue>#include <cmath>#include <set>using namespace std;int main(){int n;cin >> n;vector<int> v(n);for(int i=0; i<n; i++){cin >> v[i];}vector<int> dp(n);vector<int> before(n, -1);dp[0] = v[0];if(n>1) dp[1] = v[1];for(int i=2; i<n; i++){for(int j=0; j<i-1; j++){int next = dp[j] + v[i];if( dp[i] < next ){dp[i] = next;before[i] = j;}}}int pos = max_element(dp.begin(), dp.end()) - dp.begin();int ans = dp[pos];vector<int> path;path.push_back(pos);while(before[pos] != -1){pos = before[pos];path.push_back(pos);}cout << ans << endl;reverse(path.begin(), path.end());for(int i=0; i<path.size(); i++){cout << path[i]+1 << (i!=path.size()-1?" ":"\n");}return 0;}