Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	io := NewIo()
	defer io.Flush()
	seq := make([]int, 360)
	N := io.NextInt()
	for i := 0; i < N; i++ {
		M, D, V, S := io.NextInt(), io.NextInt(), io.NextInt(), io.NextInt()
		M = (M - 1) * 30
		D--
		for j := M + D; j < M+D+V; j++ {
			seq[j%360] = intMax(S, seq[j%360])
		}
		for j := 0; j < S; j++ {
			seq[(M+D-j+360)%360] = intMax(S-j, seq[(M+D-j+360)%360])
			seq[(M+D+V+j-1)%360] = intMax(S-j, seq[(M+D+V+j-1)%360])
		}
	}
	ans := 360
	for _, val := range seq {
		ans = intMin(val, ans)
	}
	io.Println(ans)
}

func intMin(a, b int) int {
	if a > b {
		return b
	}
	return a
}

func intMax(a, b int) int {
	if a < b {
		return b
	}
	return a
}

// I/O template
type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

func NewIo() *Io {
	return &Io{
		reader: bufio.NewReader(os.Stdin),
		writer: bufio.NewWriter(os.Stdout),
	}
}

func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

func (io *Io) NextLine() string {
	var buff []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buff = append(buff, line...)
		if !isPrefix {
			break
		}
	}
	return string(buff)
}

func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	s := io.tokens[io.nextToken]
	io.nextToken++
	return s
}

func (io *Io) NextInt() int {
	i, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) NextFloat() float64 {
	i, err := strconv.ParseFloat(io.Next(), 64)
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) Println(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}

func (io *Io) Printf(format string, a ...interface{}) {
	fmt.Fprintf(io.writer, format, a...)
}

func (io *Io) PrintIntSlice(a []int) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func (io *Io) PrintStringSlice(a []string) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func Log(name string, value interface{}) {
	fmt.Fprintf(os.Stderr, "%s=%+v\n", name, value)
}

func ExampleIo() {
	io := NewIo()
	defer io.Flush()
	a := make([]int, 0, 3)
	a[0], a[1], a[2] = io.NextInt(), io.NextInt(), io.NextInt()
	io.Println(a)
	io.PrintIntSlice(a)
	Log("io", a)
}

C++: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info{
	int start,end,S;
	bool check[361];
};


int main(){

	int N;
	scanf("%d",&N);

	Info info[N];

	for(int i = 0; i < N; i++){
		for(int day = 1; day <= 360; day++){
			info[i].check[day] = false;
		}
	}

	int month,day,length,value;

	for(int i = 0; i < N; i++){
		scanf("%d %d %d %d",&month,&day,&length,&value);
		info[i].start = 30*(month-1)+day;
		info[i].end = info[i].start+(length-1);

		for(int k = info[i].start; k <= info[i].end; k++){
			if(k > 360){
				info[i].check[k-360] = true;
			}else{
				info[i].check[k] = true;
			}
		}

		info[i].S = value;
	}

	int minimum = BIG_NUM,tmp,tmp_max,left_diff,right_diff,tmp_day;

	for(int day = 1; day <= 360; day++){
		tmp_max = 0;
		for(int i = 0; i < N; i++){

			if(info[i].check[day]){
				tmp = info[i].S;
			}else{

				for(tmp_day = day-1,left_diff = 1; ; left_diff++,tmp_day--){
					if(tmp_day <= 0){
						tmp_day = 360;
					}
					if(info[i].check[tmp_day]){
						break;
					}
				}

				for(tmp_day = day+1,right_diff = 1; ; right_diff++,tmp_day++){
					if(tmp_day > 360){
						tmp_day = 1;
					}
					if(info[i].check[tmp_day]){
						break;
					}
				}

				tmp = max(0,info[i].S-min(left_diff,right_diff));
			}

			tmp_max = max(tmp_max,tmp);
		}
		minimum = min(minimum,tmp_max);
	}

	printf("%d\n",minimum);

	return 0;
}

C++: #include <bits/stdc++.h>
#define REP(i, a, n) for(ll i = ((ll) a); i < ((ll) n); i++)
#define MAXCH(a, b) a = max(a, b);
#define INF (1LL << 30)
using namespace std;
typedef long long ll;

ll N, M[100], D[100], V[100], S[100];

int main(void) {
  cin >> N;
  REP(i, 0, N) cin >> M[i] >> D[i] >> V[i] >> S[i];

  vector<ll> crowd(360, 0);
  REP(i, 0, N) {
    ll t = (M[i] - 1) * 30 + (D[i] - 1);
    REP(j, 0, V[i] + 1) MAXCH(crowd[(t + j) % 360], S[i]);
    REP(j, 0, S[i]) MAXCH(crowd[(t + V[i] + j) % 360], S[i] - j - 1);
    REP(j, 0, S[i]) MAXCH(crowd[(t + 360 - j) % 360], S[i] - j - 1);
  }

  // REP(i, 0, 360) cout << crowd[i] << " ";
  // cout << endl;

  cout << *min_element(crowd.begin(), crowd.end()) << endl;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;

int main(){
  
  int n, m, d, v, s, x[1081]={};

  cin>>n;
  
  for(int i=0;i<n;i++){
    cin>>m>>d>>v>>s;
    int y=(m-1)*30+d+360;
    for(int k=1;k<=1080;k++){
      if(k<y){
	x[k]=max(x[k],s-(y-k));
      }else if(y<=k&&k<=y+v-1){
	x[k]=max(x[k],s);
      }else if(y+v-1<k){
	x[k]=max(x[k],max(0,s-(k-(y+v-1))));
      }
    }
  }

  //for(int i=1;i<=360;i++) cout<<x[i]<<" ";

  int mi=360;
  for(int i=1;i<=360;i++) mi=min(mi,max(x[i],max(x[i+360],x[i+720])));

  cout<<mi<<endl;
  
  return 0;
}