結果

問題 No.505 カードの数式2
ユーザー ukuku09
提出日時 2017-04-21 22:51:27
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 1,214 bytes
コンパイル時間 1,381 ms
コンパイル使用メモリ 160,920 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-07-20 06:25:02
合計ジャッジ時間 2,149 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19 WA * 10
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) begin(v), end(v)
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for(int i = (int)(s); i < (int)(n); i++)

template<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;}
template<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;}

using pint = pair<int, int>;
using tint = tuple<int, int, int>;
using vint = vector<int>;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

int ans = 0;
int dp[20];

int dfs(int idx, vint& a) {
  if(idx == a.size()-1) return a[idx];

  int& res = dp[idx];
  if(~res) return res;
  res = -inf;
  rep(i, 4) {
    if(i == 0) {
      chmax(res, dfs(idx+1, a) + a[idx]);
    } else if(i == 1) {
      chmax(res, dfs(idx+1, a) - a[idx]);
    } else if(i == 2) {
      chmax(res, dfs(idx+1, a) * a[idx]);
    } else if(i == 3) {
      if(a[idx] != 0) chmax(res, dfs(idx+1, a) / a[idx]);
    }
  }
  return res;
}

signed main()
{
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int N;
  cin >> N;
  vint a(N);
  rep(i, N) cin >> a[i];
  reverse(all(a));
  memset(dp, -1, sizeof(dp));
  cout << dfs(0, a) << endl;

  return 0;
}
0