fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef pair<int, int> ii;
  7.  
  8. const int INF = 1e9;
  9. const ll LINF = 1e18;
  10.  
  11. template<typename T>
  12. void maximize(T& a, const T& b) {
  13. if (a < b) a = b;
  14. }
  15.  
  16. const int N = 1e5 + 5;
  17.  
  18. int n;
  19. int a[N][3]; // a[i][j] = Điểm hạnh phúc nhận được nếu làm hoạt động j ở ngày thứ i
  20. int dp[N][3]; // dp[i][j] = Tổng điểm hạnh phúc lớn nhất đạt được khi xét đến ngày thứ i
  21. // và loại hoạt động đã làm ở ngày thứ i là j (0/1/2)
  22.  
  23. int main() {
  24. ios::sync_with_stdio(false);
  25. cin.tie(nullptr);
  26. cin >> n;
  27. for (int i = 1; i <= n; i++) {
  28. for (int j = 0; j <= 2; j++) cin >> a[i][j];
  29. }
  30.  
  31. // Bài này nếu làm cẩn thận thì ban đầu ta nên khởi tạo giá trị của dp = -INF hết
  32. for (int i = 1; i <= n; i++) {
  33. for (int j = 0; j <= 2; j++) dp[i][j] = -INF;
  34. }
  35.  
  36. // Khởi tạo dp cho ngày thứ 1
  37. for (int j = 0; j <= 2; j++) dp[1][j] = a[1][j];
  38.  
  39. for (int i = 2; i <= n; i++) {
  40. for (int j = 0; j <= 2; j++) {
  41. for (int prev_j = 0; prev_j <= 2; prev_j++) {
  42. if (prev_j == j) continue;
  43. maximize(dp[i][j], dp[i - 1][prev_j] + a[i][j]);
  44. }
  45. }
  46. }
  47.  
  48. int ans = 0;
  49. for (int j = 0; j <= 2; j++) {
  50. maximize(ans, dp[n][j]);
  51. }
  52.  
  53. cout << ans << '\n';
  54. }
Success #stdin #stdout 0.01s 5308KB
stdin
3
10 40 70
20 50 80
30 60 90
stdout
210