Advertisement
Pearlfromsu

st170321_27

Mar 24th, 2021 (edited)
816
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. int main() {//00:25
  8.     ifstream f("C:/inp/st170321/27/27-B.txt");
  9.     int n;
  10.     f >> n;
  11.     long zero[3] = {0};
  12.     int z = 0;
  13.     long one[3] = {0};
  14.     int o = 0;
  15.     long two[3] = {0};
  16.     int t = 0;
  17.  
  18.     long a;
  19.     for (int i = 0; i < n; i++) {
  20.         f >> a;
  21.         if (a % 3 == 0) {
  22.             if (a > zero[z]) {
  23.                 z++;
  24.                 if (z > 2) z = 0;
  25.                 zero[z] = a;
  26.             }
  27.         } else if (a % 3 == 1) {
  28.             if (a > one[t]) {
  29.                 o++;
  30.                 if (o > 2) o = 0;
  31.                 two[o] = a;
  32.             }
  33.         } else {
  34.             if (a > two[t]) {
  35.                 t++;
  36.                 if (t > 2) t = 0;
  37.                 two[t] = a;
  38.             }
  39.         }
  40.     }
  41.  
  42.     long maxx = 0;
  43.  
  44.     //Как получить остаток 3:
  45.     //0 + 0 + 0
  46.     //1 + 1 + 1
  47.     //2 + 2 + 2
  48.     //0 + 1 + 2
  49.     //а хотя можно просто перебрать все 27 вариантов + сумму каждого массива
  50.  
  51.     if ((zero[0] + zero[1] + zero[2]) > maxx)
  52.         maxx = zero[0] + zero[1] + zero[2];
  53.     if ((one[0] + one[1] + one[2]) > maxx)
  54.         maxx = one[0] + one[1] + one[2];
  55.     if ((two[0] + two[1] + two[2]) > maxx)
  56.         maxx = two[0] + two[1] + two[2];
  57.  
  58.     for (int i = 0; i < 3; i++) {
  59.         for (int j = 0; j < 3; j++) {
  60.             for (int k = 0; k < 3; k++) {
  61.                 if ((zero[i] + one[j] + two[k]) > maxx)
  62.                     maxx = zero[i] + one[j] + two[k];
  63.             }
  64.         }
  65.     }
  66.     cout << maxx << endl; //00:40
  67.    
  68.     return 0;
  69. }
  70.  
  71.  
  72.  
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement