結果

問題 No.505 カードの数式2
ユーザー Ut.k
提出日時 2017-04-22 00:05:03
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 686 bytes
コンパイル時間 503 ms
コンパイル使用メモリ 61,336 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-21 06:11:02
合計ジャッジ時間 2,420 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20 WA * 9
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

long N;

long sign(int x) {
  return x < 0 ? -1 : 1;
}

long DFS(long x, const vector<long> &a, long i) {
  if (i == N) {
    return x;
  }

  if (a[i] == 0) {
    return DFS(x, a, i + 1);
  }
  if (sign(x) == sign(a[i])) {
     return max(max(DFS(x + a[i], a, i + 1), DFS(x * a[i], a, i + 1)),
	     DFS(x / a[i], a, i + 1));
  } else  {
    return max(max(DFS(x - a[i], a, i + 1), DFS(x * a[i], a, i + 1)),
	       DFS(x / a[i], a, i + 1));
  }
}

int main() {
  cin >> N;

  vector<long> a(N);
  for (long i = 0; i < N; i++) {
    cin >> a[i];
  }

  cout << DFS(a[0], a, 1) << endl;;
  
  return 0;
}
0