Cod sursa(job #1470508)

Utilizator bciobanuBogdan Ciobanu bciobanu Data 11 august 2015 16:21:31
Problema Oo Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.1 kb
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define MAX_N 100000
#define MAX(A, B) ((A) > (B) ? (A) : (B))
#pragma warning(disable : 4996)

unsigned char v[MAX_N + 1];
int best[MAX_N + 1];

void updateSolution(int lo, int hi, int *solution) {
  memset(best, 0, sizeof(best));
  best[lo + 1] = v[lo + 1] + v[lo];
  for (int i = lo + 2; i <= hi; i++) {
    best[i] = MAX(best[i - 1], best[i - 3] + v[i] + v[i - 1]);
  }
  *solution = MAX(*solution, best[hi]);
}

template <class T>
inline void read(FILE *f, T *q) {
  char c;
  
  do {
    c = fgetc(f);
  } while (!isdigit(c));
  *q = 0;
  do {
    *q = (*q << 1) + (*q << 3) + (c - '0');
    c = fgetc(f);
  } while (isdigit(c));
}

int main(void) {
  int n;
  int ans;
  FILE *f = fopen("oo.in", "r");

  fscanf(f, "%d", &n);
  for (int i = 0; i < n; i++) {
    read(f, &v[i]);
  }
  fclose(f);
  v[n] = v[0];

  ans = 0;
  updateSolution(0, n - 2, &ans);
  updateSolution(1, n - 1, &ans);
  updateSolution(2, n, &ans);

  f = fopen("oo.out", "w");
  fprintf(f, "%d\n", ans);
  fclose(f);
  return 0;
}