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 "bits/stdc++.h"
using namespace std;

using VS = vector<string>;    using LL = long long;
using VI = vector<int>;       using VVI = vector<VI>;
using PII = pair<int, int>;   using PLL = pair<LL, LL>;
using VL = vector<LL>;        using VVL = vector<VL>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
#define debug(x) cout << #x << ": " << x << endl
const int INF = 1e9;                          const LL LINF = 1e16;
const LL MOD = 1000000007;                    const double PI = acos(-1.0);
int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 };    int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };

/* -----  2017/09/19  Problem: ACPC2017_day2_b.cpp  / Link:   ----- */
/* ------??????------



-----?????????????????§----- */
/* -----?§£??¬???-----



----?§£??¬???????????§---- */

LL N;

int m[102], s[102], d[102], v[102], day[102];
int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);

	cin >> N;
	FOR(i, 0, N) {
		cin >> m[i] >> d[i] >> v[i] >> s[i];
		m[i]--; v[i]--;
		day[i] = m[i] * 30 + d[i];
	}

	int  res = INF;
	FOR(i, 1, 361) {
		int  ans = -1;
		FOR(j, 0, N) {
			int konza;
			if ((day[j] <= i&&i <= (day[j] + v[j])) || (day[j] - 360 <= i&&i <= day[j] - 360 + v[j]))
				konza = s[j];
			else {
				int startsa = min(abs(i - day[j]), min(abs(i - 360 - day[j]), abs(i + 360 - day[j])));
				int endsa=min(abs(i-day[j]-v[j]),min(abs(i-360-day[j]-v[j]),abs(i+360-day[j]-v[j])));
				konza = max(0, s[j] - min(startsa, endsa));
				//konza = max(0, s[j] - min((min(abs(day[j] - i), 360 - abs(day[j] - i))), min(abs(day[j] + v[j] - i), 360 - abs(day[j] + v[j] - i))));
				//cout << "renkyu out : " << abs(day[j] - i) << ": " << abs(day[j] + v[j] - i) << endl;
				//debug(konza);
			}
			ans = max(konza, ans);
		}
		//cout << i << " " << ans << endl;
		if (ans >= 0)
			res = min(res, ans);
	}

	cout << res << "\n";

	return 0;
}

C++: #include<iostream>
#include<vector>

using namespace std;
#define repeat(i,n) for(int i=0;i<(n);i++)

int f(int v){
  return max(v, -v);
}

int main(){

  int n; cin>> n;
  int m[n], d[n], v[n], s[n];
  repeat(i, n) cin>> m[i]>> d[i]>> v[i]>> s[i];

  int mn=1e9;
  for(int x=1; x<=360; x++){
    int mx=0;
    repeat(i, n){
      int st=(m[i]-1)*30+d[i];
      int ed=st+v[i]-1;
      if(st<=x && x<=ed) mx=max(mx, s[i]);
      else{
        if(x<st && x<=(ed-1)%360+1) mx=max(mx, s[i]);
        mx=max(mx, max(0, s[i]-min(f(x-st)+1, f(x-ed)+1)));
        mx=max(mx, max(0, s[i]-(360-x+st)));
        mx=max(mx, max(0, s[i]-(360-((ed-1)%360+1)+x)));
      }
      // cout<< x<< " "<< mx<< endl;
    }
    mn=min(mn, mx);
  }

  cout<< mn<< endl;

  return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<stack>
using namespace std;

#define REP(i,n) for(int i=0;(i)<(n);(i)++)
#define ll long long

static const int INFTY = (1 << 20);
static const int MOD = 360;

int a, b, D[102], S[102], V[102];
int day[360];

int main() {
	int N;
	
	cin >> N;
	REP(i, N) {
		cin >> a >> b >> V[i] >> S[i];
		D[i] = (a - 1) * 30 + b - 1;//0-indexed
	}

	
	REP(i, 360) day[i] = 0;//0-indexed

	REP(i, 360) {
		REP(j, N) {
			int len1 = min(abs(i - D[j]), min(abs(360 - D[j] + i), abs(360 - i + D[j])));
			int len2 = min(abs((D[j] + V[j] - 1) % MOD - i), min(abs(360 - (D[j] + V[j] - 1) % MOD + i), abs(360 - i + (D[j] + V[j] - 1) % MOD)));
			day[i] = max(day[i], S[j] - min(len1, len2));

			//if(i==359) cout << len1 << " " << len2 << endl;
		}
	}

	REP(i, 360) {
		REP(j, N) {
			if (i < D[j]) {
				if (i + 360 >= D[j] && i + 360 <= D[j] + V[j] - 1) day[i] = max(day[i],S[j]);
			}
			else {
				if (i >= D[j] && i <= D[j] + V[j] - 1) {
					day[i] = max(day[i],S[j]);
				}
			}
			
		}
	}

	int ans = INFTY;
	REP(i, 360) {
		ans = min(ans, day[i]);
		//cout << day[i] << " ";
	}
	//cout << endl;

	cout << ans << endl;
	return 0;
}