結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー k10ck10c
提出日時 2020-06-27 22:25:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 24 ms / 2,000 ms
コード長 2,487 bytes
コンパイル時間 2,435 ms
コンパイル使用メモリ 200,112 KB
実行使用メモリ 5,908 KB
最終ジャッジ日時 2023-09-20 06:15:43
合計ジャッジ時間 6,625 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 3 ms
4,384 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 3 ms
4,380 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 3 ms
4,376 KB
testcase_23 AC 24 ms
5,364 KB
testcase_24 AC 24 ms
5,412 KB
testcase_25 AC 24 ms
5,320 KB
testcase_26 AC 24 ms
5,340 KB
testcase_27 AC 24 ms
5,392 KB
testcase_28 AC 23 ms
5,908 KB
testcase_29 AC 24 ms
5,708 KB
testcase_30 AC 24 ms
5,336 KB
testcase_31 AC 24 ms
5,440 KB
testcase_32 AC 24 ms
5,324 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define reps(i, n) for(int i=1, i##_len=(n); i<=i##_len; ++i)
#define rrep(i, n) for(int i=((int)(n)-1); i>=0; --i)
#define rreps(i, n) for(int i=((int)(n)); i>0; --i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((int)(x).size())
#define pl(s) cout << (s) << "\n";
#define pls(...) {bool space = false; for(auto i : __VA_ARGS__) (cout << (space?" ":"") << i), space = true; cout << "\n";}
#define plexit(s) {cout << (s) << "\n"; exit(0);}
#define yes(s) cout << ((s)?"Yes":"No") << "\n";
#define pb push_back
#define pcnt __builtin_popcountll

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

#ifdef __LOCAL
#include <dump.hpp>
#define dump(...)                                                            \
  DUMPOUT << "  " << string(#__VA_ARGS__) << ": "                            \
          << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]"        \
          << endl                                                            \
          << "    ",                                                         \
      dump_func(__VA_ARGS__)
#else
#define dump(...)
#endif

struct IOInit {
  static constexpr int IOS_PREC = 15;
  static constexpr bool AUTOFLUSH = false;

  IOInit() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(IOS_PREC);
    dump(IOS_PREC);
    if(AUTOFLUSH) cout << unitbuf;
  }
} IO_INIT;

using i64 = std::int_fast64_t;
using i128 = __int128_t;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;
using vi = std::vector<int>;
using vvi = std::vector<vector<int>>;
using vl = std::vector<ll>;
using vp = std::vector<pii>;


signed main(void) {
  int N; cin >> N;
  vi A(N); rep(i,N) cin >> A[i];
  int ans = 1e9+7;

  vi ls(N), rs(N);
  ls.front() = A.front();
  rs.back() = A.back();
  reps(i,N-1) ls[i] = min(ls[i-1],A[i]);
  rrep(i,N-1) rs[i] = min(rs[i+1],A[i]);

  //dump(ls,rs);

  reps(i,N-2) {
    int a = ls[i-1];
    int b = A[i];
    int c = rs[i+1];
    if (max({a,b,c}) != b && min({a,b,c}) != b) continue;
    chmin(ans,a+b+c);
    //dump(ans,a,b,c);
  }

  if (ans == 1e9+7) plexit("-1")
  pl(ans)
  return 0;
}
0