結果
| 問題 |
No.1334 Multiply or Add
|
| コンテスト | |
| ユーザー |
hitonanode
|
| 提出日時 | 2021-01-08 22:15:32 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 27 ms / 2,000 ms |
| コード長 | 1,389 bytes |
| コンパイル時間 | 2,095 ms |
| コンパイル使用メモリ | 197,716 KB |
| 最終ジャッジ日時 | 2025-01-17 12:27:29 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 71 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define REP(i, n) FOR(i,0,n)
template <typename T> bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; }
const int MOD = 1000000007;
bool is_prod_big(vector<int> A) {
long long tmp = 1;
for (auto a : A) {
tmp *= a;
if (tmp >= 500000) return true;
}
return false;
}
int main() {
cin.tie(nullptr), ios::sync_with_stdio(false);
int N;
cin >> N;
vector<int> A(N);
cin >> A;
if (is_prod_big(A)) {
int i = 0, j = N - 1;
while (A[i] == 1) i++;
while (A[j] == 1) j--;
long long ret = 1;
FOR(k, i, j + 1) ret = ret * A[k] % MOD;
ret += i + (N - 1 - j);
cout << ret % MOD << '\n';
return 0;
}
vector<int> AA(N + 1, 1);
REP(i, N) AA[i + 1] = AA[i] * A[i];
vector<int> nonzero;
REP(i, N) if (A[i] > 1) nonzero.push_back(i);
vector<int> dp(N + 1);
REP(i, N) {
chmax(dp[i + 1], dp[i] + A[i]);
for (auto j : nonzero) {
if (j < i) {
chmax(dp[i + 1], dp[j] + AA[i + 1] / AA[j]);
}
}
}
cout << dp.back() << '\n';
}
hitonanode