結果

問題 No.2854 -1 Subsequence
ユーザー tnakao0123
提出日時 2024-08-29 14:14:04
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 87 ms / 2,000 ms
コード長 850 bytes
コンパイル時間 243 ms
コンパイル使用メモリ 41,340 KB
最終ジャッジ日時 2025-02-24 02:42:42
ジャッジサーバーID
(参考情報)
judge1 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 40
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:33:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   33 |   scanf("%d", &n);
      |   ~~~~~^~~~~~~~~~
main.cpp:34:36: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   34 |   for (int i = 0; i < n; i++) scanf("%d", as + i);
      |                               ~~~~~^~~~~~~~~~~~~~

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2854.cc:  No.2854 -1 Subsequence - yukicoder
 */

#include<cstdio>
#include<algorithm>

using namespace std;

/* constant */

const int MAX_N = 200000;
const long long LINF = 1LL << 62;

/* typedef */

using ll = long long;

/* global variables */

int as[MAX_N];
ll dp[MAX_N + 1][3];

/* subroutines */

inline void setmax(ll &a, ll b) { if (a < b) a = b; }

/* main */

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", as + i);
  
  for (int i = 0; i <= n; i++) fill(dp[i], dp[i] + 3, -LINF);
  dp[0][2] = 0;

  for (int i = 0; i < n; i++)
    for (int j = 0; j < 3; j++)
      if (dp[i][j] > -LINF) {
	setmax(dp[i + 1][j], dp[i][j]);
	int k = (j & 1);
	setmax(dp[i + 1][k ^ 1], dp[i][j] + as[i] * (2 * k - 1));
      }

  printf("%lld\n", max(dp[n][0], dp[n][1]));
  
  return 0;
}
0