結果

問題 No.1199 お菓子配り-2
ユーザー HaarHaar
提出日時 2020-08-28 21:37:42
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 121 ms / 1,000 ms
コード長 1,945 bytes
コンパイル時間 2,067 ms
コンパイル使用メモリ 206,744 KB
実行使用メモリ 11,196 KB
最終ジャッジ日時 2023-08-08 22:21:17
合計ジャッジ時間 9,428 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 31 ms
5,552 KB
testcase_04 AC 75 ms
8,656 KB
testcase_05 AC 4 ms
4,376 KB
testcase_06 AC 5 ms
4,380 KB
testcase_07 AC 37 ms
5,444 KB
testcase_08 AC 52 ms
6,556 KB
testcase_09 AC 31 ms
5,160 KB
testcase_10 AC 9 ms
4,380 KB
testcase_11 AC 23 ms
4,576 KB
testcase_12 AC 23 ms
4,576 KB
testcase_13 AC 9 ms
4,380 KB
testcase_14 AC 8 ms
4,376 KB
testcase_15 AC 7 ms
4,380 KB
testcase_16 AC 50 ms
6,172 KB
testcase_17 AC 24 ms
5,216 KB
testcase_18 AC 52 ms
6,940 KB
testcase_19 AC 14 ms
4,676 KB
testcase_20 AC 90 ms
9,192 KB
testcase_21 AC 55 ms
7,268 KB
testcase_22 AC 31 ms
5,556 KB
testcase_23 AC 42 ms
6,360 KB
testcase_24 AC 58 ms
6,760 KB
testcase_25 AC 10 ms
4,376 KB
testcase_26 AC 65 ms
7,344 KB
testcase_27 AC 53 ms
6,448 KB
testcase_28 AC 118 ms
11,044 KB
testcase_29 AC 117 ms
10,980 KB
testcase_30 AC 119 ms
10,928 KB
testcase_31 AC 119 ms
10,968 KB
testcase_32 AC 119 ms
11,056 KB
testcase_33 AC 119 ms
10,984 KB
testcase_34 AC 117 ms
11,040 KB
testcase_35 AC 119 ms
11,032 KB
testcase_36 AC 119 ms
11,028 KB
testcase_37 AC 121 ms
11,036 KB
testcase_38 AC 119 ms
10,920 KB
testcase_39 AC 120 ms
10,928 KB
testcase_40 AC 121 ms
10,916 KB
testcase_41 AC 121 ms
11,196 KB
testcase_42 AC 119 ms
10,964 KB
testcase_43 AC 118 ms
10,924 KB
testcase_44 AC 117 ms
10,928 KB
testcase_45 AC 118 ms
10,912 KB
testcase_46 AC 119 ms
11,044 KB
testcase_47 AC 121 ms
10,920 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#ifdef DEBUG
#include <Mylib/Debug/debug.cpp>
#else
#define dump(...) ((void)0)
#endif

template <typename T, typename U>
bool chmin(T &a, const U &b){
  return (a > b ? a = b, true : false);
}

template <typename T, typename U>
bool chmax(T &a, const U &b){
  return (a < b ? a = b, true : false);
}

template <typename T, size_t N, typename U>
void fill_array(T (&a)[N], const U &v){
  std::fill((U*)a, (U*)(a + N), v);
}

template <typename T>
auto make_vector(int n, int m, const T &value){
  return std::vector<std::vector<T>>(n, std::vector<T>(m, value));
}

template <typename T>
std::ostream& operator<<(std::ostream &s, const std::vector<T> &a){
  for(auto it = a.begin(); it != a.end(); ++it){
    if(it != a.begin()) s << " ";
    s << *it;
  }
  return s;
}

template <typename T>
std::istream& operator>>(std::istream &s, std::vector<T> &a){
  for(auto &x : a) s >> x;
  return s;
}



namespace solver{
  void init(){
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);
    std::cout << std::fixed << std::setprecision(12);
    std::cerr << std::fixed << std::setprecision(12);
    std::cin.exceptions(std::ios_base::failbit);
  }
  
  void solve(){
    int N, M; std::cin >> N >> M;
    auto A = make_vector<int64_t>(N, M, 0);
    std::cin >> A;

    std::deque<int64_t> B(N);
    for(int i = 0; i < N; ++i) B[i] = std::accumulate(A[i].begin(), A[i].end(), 0LL);

    auto dp = make_vector<int64_t>(2, N + 1, -(1LL << 50));

    dp[0][0] = 0;

    for(int i = 0; i < N; ++i){
      chmax(dp[0][i + 1], dp[0][i]);
      chmax(dp[1][i + 1], dp[0][i] + B[i]);
      chmax(dp[1][i + 1], dp[1][i]);
      chmax(dp[0][i + 1], dp[1][i] - B[i]);
    }

    int64_t ans = std::max(dp[0][N], dp[1][N]);    
    
    std::cout << ans << "\n";
  }
}

int main(){
  solver::init();
  while(true){
    try{
      solver::solve();
    }catch(const std::istream::failure &e){
      break;
    }
  }
  return 0;
}
0